I'm having a problem, whereby calling a variable all_sprites as a pygame.Group() does not return any values, and I cannot work out why.
class game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((screenwidth, screenheight))
pg.display.set_caption("Gammee")
self.clock = pg.time.Clock()
def run(self):
while True:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.update()
self.draw()
def new(self):
self.othersprites = pg.sprite.Group()
def draw(self):
self.screen.fill(BGCOLOR)
self.othersprites.draw(self.screen)
pg.display.flip()
def quit(self):
pg.quit()
sys.exit()
def update(self):
self.othersprites.update()
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
g = game()
while True:
g.new()
g.run()
this is the code for the game
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((30, 30), SRCALPHA)
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.vx, self.vy = 0, 0
self.x = x
self.y = y
def get_keys(self):
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.vx = -PLAYER_SPEED
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.vx = PLAYER_SPEED
if keys[pg.K_UP] or keys[pg.K_w]:
self.vy = -PLAYER_SPEED
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.vy = PLAYER_SPEED
if len(keys) != 0:
self.vx *= 0.9
self.vy *= 0.9
def update(self):
self.get_keys()
self.x += self.vx * self.game.dt
self.y += self.vy * self.game.dt
self.rect.x = self.x
self.rect.y = self.y
however nothing is drawn to the scree. I have also tested by putting
for sprite in self.all_sprites:
print(sprite)
which used to output info on the sprite, but now does nothing,
any ideas as to what I'm doing wrong?
You have to create an instance of the Player in the new method of the game class. Also, the sprite group is called othersprites in the game class and all_sprites in the Player class, but the names have to match.
class game:
def new(self):
self.othersprites = pg.sprite.Group()
# This is how you create an instance/object.
self.player = Player(self, 200, 300)
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.othersprites
I think it would be nicer and simpler to add the self.player sprite to self.othersprites in the new method instead of passing the whole game instance to the player.
class game:
def new(self):
self.othersprites = pg.sprite.Group()
self.player = Player(200, 300)
self.othersprites.add(self.player)
Related
I simply just want to delete the zombie sprite from my sprite group when the bullet collides with the zombie to create the sense the zombie is dying.
I already tried the Zombie(1,1).kill() but it did not work unfortunatley.
here is my code if it will be helpful...
import pygame
from sys import exit
from random import randint
import math
from pygame.math import Vector2
from pygame.constants import K_LSHIFT, K_SPACE, MOUSEBUTTONDOWN, MOUSEBUTTONUP, K_e
pygame.init()
import time
pygame.mouse.set_visible(True)
class Player(pygame.sprite.Sprite):
def __init__(self, x , y):
super().__init__()
self.x = x
self.y = y
self.image = pygame.image.load('graphics/Robot 1/robot1_gun.png').convert_alpha()
self.rect = self.image.get_rect()
self.orig_image = pygame.image.load('graphics/Robot 1/robot1_gun.png').convert_alpha()
self.rotate_vel = 1
self.cross_image = pygame.image.load('graphics/crosshair049.png')
def draw(self, surface):
""" Draw on surface """
# blit yourself at your current position
surface.blit(self.image, self.rect)
dir_vec = pygame.math.Vector2()
dir_vec.from_polar((180, -self.rotate_vel))
cross_pos = dir_vec + self.rect.center
cross_x, cross_y = round(cross_pos.x), round(cross_pos.y)
surface.blit(self.cross_image, self.cross_image.get_rect(center = (cross_x, cross_y)))
def movement(self):
key = pygame.key.get_pressed()
self.rect = self.image.get_rect(center = (self.x, self.y))
dist = 3 # distance moved in 1 frame, try changing it to 5
if key[pygame.K_DOWN] or key[pygame.K_s]: # down key
self.y += dist # move down
elif key[pygame.K_UP] or key[pygame.K_w]: # up key
self.y -= dist # move up
if key[pygame.K_RIGHT] or key[pygame.K_d]: # right key
self.x += dist # move right
elif key[pygame.K_LEFT] or key[pygame.K_a]: # left key
self.x -= dist # move left
def rotate(self, surface):
keys = pygame.key.get_pressed()
if keys[K_LSHIFT]:
self.rotate_vel += 5
self.image = pygame.transform.rotate(self.orig_image, self.rotate_vel)
self.rect = self.image.get_rect(center=self.rect.center)
surface.blit(self.image, self.rect)
if keys[K_SPACE]:
self.rotate_vel += -5
self.image = pygame.transform.rotate(self.orig_image, self.rotate_vel)
self.rect = self.image.get_rect(center=self.rect.center)
surface.blit(self.image, self.rect)
def update(self):
self.movement()
self.draw(screen)
self.rotate(screen)
class Bullet(pygame.sprite.Sprite):
def __init__(self, pos, angle):
super().__init__()
self.image = pygame.image.load('graphics/weapons/bullets/default_bullet.png')
self.image = pygame.transform.rotate(self.image, angle)
self.rect = self.image.get_rect(center = pos)
self.speed = 25
self.pos = pos
self.dir_vec = pygame.math.Vector2()
self.dir_vec.from_polar((self.speed, -angle))
def update(self, screen):
self.pos += self.dir_vec
self.rect.center = round(self.pos.x), round(self.pos.y)
class Zombie(pygame.sprite.Sprite):
def __init__(self, x , y):
super().__init__()
self.x = x
self.y = y
self.image = pygame.image.load('graphics/zombie/zoimbie1_hold.png').convert_alpha()
self.rect = self.image.get_rect(center = (x,y))
self.orig_image = pygame.image.load('graphics/Robot 1/robot1_gun.png').convert_alpha()
def draw(self, surface):
surface.blit(self.image, self.rect)
def update(self):
self.draw(screen)
#screen
clock = pygame.time.Clock()
FPS = 60
screen = pygame.display.set_mode((1200, 600))
#player
player_sprite = Player(600, 300)
player = pygame.sprite.GroupSingle()
player.add(player_sprite)
#bullet
bullet_group = pygame.sprite.Group()
#Zombie
zombie_sprite = Zombie(600, 300)
zombie = pygame.sprite.Group()
zombie.add(zombie_sprite)
#keys
keys = pygame.key.get_pressed()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = player_sprite.rect.center
new_bullet = Bullet(pos, player_sprite.rotate_vel)
bullet_group.add(new_bullet)
if pygame.sprite.spritecollide(zombie_sprite,bullet_group , True):
Zombie(1,1).kill()
bullet_group.update(screen)
#screen
screen.fill('grey')
#player sprite funtions
player.update()
#buller group draw
bullet_group.draw(screen)
bullet_group.update(screen)
#Zombie update
zombie.update()
clock.tick(FPS)
pygame.display.update()
Try zombie_sprite.kill() instead of Zombie(1,1).kill()
You are instantiating a new object instead of destroying the original one.
I am creating a 4-way movement, tile based rpg. The code I have written for collision detection is relatively simple but I have a minor graphical error.
The player moves directly between evenly spaced tiles. To control the speed the player moves at there is a walk buffer of 200 seconds. When the player collides with a wall, they should be pushed back in the same direction they hit the wall. This works, however, very briefly the player sprite will flicker in the wall.
I suspect it's to do with the player update function and how that's ordered but I've messed around with it to no avail.
import sys
vec = pg.math.Vector2
WHITE = ( 255, 255, 255)
BLACK = ( 0, 0, 0)
RED = ( 255, 0, 0)
YELLOW = ( 255, 255, 0)
BLUE = ( 0, 0, 255)
WIDTH = 512 # 32 by 24 tiles
HEIGHT = 384
FPS = 60
TILESIZE = 32
PLAYER_SPEED = 3 * TILESIZE
MAP = ["1111111111111111",
"1P.............1",
"1..............1",
"1..1111........1",
"1..1..1........1",
"1..1111.111111.1",
"1............1.1",
"1........111.1.1",
"1........1...1.1",
"1........11111.1",
"1..............1",
"1111111111111111"]
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
def player_collisions(sprite, group):
hits_walls = pg.sprite.spritecollide(sprite, group, False, collide_hit_rect)
if hits_walls:
sprite.pos -= sprite.vel * TILESIZE
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.walk_buffer = 200
self.vel = vec(0, 0)
self.pos = vec(x, y) *TILESIZE
self.current_frame = 0
self.last_update = 0
self.walking = False
self.walking_sprite = pg.Surface((TILESIZE, TILESIZE))
self.walking_sprite.fill(YELLOW)
self.image = self.walking_sprite
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.hit_rect.bottom = self.rect.bottom
def update(self):
self.get_keys()
self.rect = self.image.get_rect()
self.rect.topleft = self.pos
self.pos += self.vel * TILESIZE
self.hit_rect.topleft = self.pos
player_collisions(self, self.game.walls)
self.rect.midbottom = self.hit_rect.midbottom
def get_keys(self):
self.vel = vec(0,0)
now = pg.time.get_ticks()
keys = pg.key.get_pressed()
if now - self.last_update > self.walk_buffer:
self.vel = vec(0,0)
self.last_update = now
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.vel.x = -1
elif keys[pg.K_RIGHT] or keys[pg.K_d]:
self.vel.x = 1
elif keys[pg.K_UP] or keys[pg.K_w]:
self.vel.y = -1
elif keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel.y = 1
class Obstacle(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.w = TILESIZE
self.h = TILESIZE
self.game = game
self.image = pg.Surface((self.w,self.h))
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.rect.x = self.x
self.rect.y = self.y
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Hello Stack Overflow")
self.clock = pg.time.Clock()
pg.key.set_repeat(500, 100)
def new(self):
self.all_sprites = pg.sprite.Group()
self.walls = pg.sprite.Group()
for row, tiles in enumerate(MAP):
for col, tile in enumerate(tiles):
if tile == "1":
Obstacle(self, col, row)
elif tile == "P":
print("banana!")
self.player = Player(self, col, row)
def quit(self):
pg.quit()
sys.exit()
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.update()
self.draw()
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
def update(self):
self.player.update()
def draw(self):
self.screen.fill(WHITE)
for wall in self.walls:
self.screen.blit(wall.image, wall.rect)
for sprite in self.all_sprites:
self.screen.blit(sprite.image, sprite.rect)
pg.display.flip()
# create the game object
g = Game()
while True:
g.new()
g.run()
pg.quit()```
You're setting the rect before checking for collision but not resetting it after the collision check.
Here is the updated code (in Player class, update method):
self.hit_rect.topleft = self.pos
player_collisions(self, self.game.walls) # may change postion
self.hit_rect.topleft = self.pos # reset rectangle
Code:
def main():
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
player.update()
enemy.update(player)
#player.rect.clamp_ip(border_rect)
screen.fill(BACKGROUND_COLOR)
screen.blit(player.image, player.rect)
screen.blit(enemy.image, enemy.rect)
pygame.display.update()
class Mob(pygame.sprite.Sprite):
def __init__(self, position):
super(Mob, self).__init__()
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color('red'))
self.rect = self.image.get_rect(center=position)
self.position = pygame.math.Vector2(position)
self.speed = 3
def hunt_player(self, player):
player_position = player.rect.center
direction = player_position - self.position
velocity = direction.normalize() * self.speed
self.position += velocity
self.rect.center = self.position
def update(self, player):
self.hunt_player(player)
class Player(pygame.sprite.Sprite):
def __init__(self, position):
super(Player, self).__init__()
self.image = pygame.Surface((40, 40))
self.image.fill(pygame.Color('blue'))
self.rect = self.image.get_rect(center=position)
self.position = pygame.math.Vector2(position)
self.velocity = pygame.math.Vector2(0, 0)
self.speed = 6
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.velocity.x = -self.speed
elif keys[pygame.K_d]:
self.velocity.x = self.speed
else:
self.velocity.x = 0
if keys[pygame.K_w]:
self.velocity.y = -self.speed
elif keys[pygame.K_s]:
self.velocity.y = self.speed
else:
self.velocity.y = 0
self.position += self.velocity
self.rect.center = self.position
player = Player(position=(350, 220))
enemy = Mob(position=(680, 400))
startscreen()
main()
Here is a 'Chase' style game which I've been dabbling over the past few months.
Could anyone help me with creating a border for my sprite? I've managed to use the clamp_ip() method but it only stops the image from leaving the screen and not the actual sprite so I've took it out. Any help would be much appreciated.
The actual position of the player sprite is stored in the self.position attribute, but calling player.rect.clamp_ip(border_rect) only affects the self.rect attribute and the position will still be changed each frame.
I'd check if the player is outside of the screen area with the pygame.Rect.contains method, if yes, call clamp_ip and set the player.position to the new player.rect.center.
if not border_rect.contains(player.rect):
player.rect.clamp_ip(border_rect)
player.position = pygame.math.Vector2(player.rect.center)
I'm working on a space shooter game and In that game, when an alien sprite collides with a player sprite, I want the game to end. When wrote the code for the collision, It didn't end the game. Can someone help me?
main.py
# IMPORTS
import pygame
from sprites import *
from config import *
# GAME
class Game():
def __init__(self):
# INIT PYGAME
pygame.init()
pygame.mixer.init()
pygame.display.set_caption(TITLE)
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.running = True
self.player = Player()
# NEW GAME
def new(self):
self.allSprites = pygame.sprite.Group()
self.allAliens = pygame.sprite.Group()
for amount in range(ALIEN_AMOUNT):
self.alien = Alien()
self.allAliens.add(self.alien)
self.allSprites.add(self.alien)
self.allSprites.add(self.player)
self.run()
# RUN GAME
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
self.animate()
self.collision()
# DRAW
def draw(self):
self.screen.fill(BLACK)
self.allAliens.draw(self.screen)
self.allSprites.draw(self.screen)
pygame.display.update()
# ANIMATE
def animate(self):
pass
# DETECT COLLISION
def collision(self):
# player collition with alien
hits = pygame.sprite.collide_rect(self.alien, self.player)
if hits:
self.playing = False
self.running = False
# CHECK FOR EVENTS
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
# UPDATE GAME
def update(self):
self.allSprites.update()
# GAME OVER
def gameOver(self):
pass
# START SCREEN
def startScreen(self):
pass
# END SCREEN
def endScreen(self):
pass
game = Game()
game.startScreen()
while game.running:
game.new()
game.gameOver()
# QUIT
pygame.quit()
quit()
sprites.py
# IMPORTS
import pygame, random
from config import *
# PLAYER
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("assets/img/player.png").convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = WIDTH / 2
self.rect.y = HEIGHT - 50
self.velX = 0
def animate(self):
self.rect.x += self.velX
def control(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.velX = -5
elif keys[pygame.K_RIGHT]:
self.velX = 5
else:
self.velX = 0
def collision(self):
# collision with walls
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > WIDTH:
self.rect.right = WIDTH
def update(self):
self.animate()
self.control()
self.collision()
# ALIEN
class Alien(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("assets/img/rsz_alien.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(10, WIDTH - self.rect.w - 10)
self.rect.y = random.randrange(-100, -10)
self.velY = random.randrange(2, 8)
def animate(self):
self.rect.y += self.velY
def collision(self):
if self.rect.y > HEIGHT:
self.reset()
def update(self):
self.animate()
self.collision()
def reset(self):
self.rect.x = random.randrange(0, WIDTH)
self.rect.y = random.randrange(-100, -10)
self.velY = random.randrange(2, 8)
There's a config.py file but I don't think its needed for this question
You're only testing for collision with a single alien (which will be the last one that you created and assigned to self.alien). You need to iterate over all the aliens and test for collisions between the player and each of them.
def collision(self):
# player collision with each alien
for alien in self.allAliens:
hits = pygame.sprite.collide_rect(alien, self.player)
if hits:
self.playing = False
self.running = False
Though it the collision is detected when I use pygame.sprite.collide_rect or pygame.sprite.collide_circle, when I try to assign a bitmask to a sprite, and run it as so, the collision is not detected. (The wall.collision_action() makes the first circle move at the same speed and direction of the other circle) Though this is not a concern now,
as I am using images of circles which makes the pygame.sprite.collide_circle work fine, in the future when I am using more detailed and non-circular sprites it will be.
code:
import pygame, sys, math
from pygame.locals import *
pygame.init()
class ball_class(pygame.sprite.Sprite):
def __init__(self, surface):
self.surface = surface
self.x = 250
self.y = 250
self.vy = 0
self.vx = 0
self.sprite = pygame.sprite.Sprite()
self.sprite.image = pygame.image.load("ball.png").convert()
self.sprite.rect = self.sprite.image.get_rect(center = (self.x, self.y))
self.sprite.rect.topleft = [int(self.x), int(self.y)]
self.sprite.mask = pygame.mask.from_surface(self.sprite.image)
def event(self, event):
if event.key == K_UP:
self.vy = -1
self.vx = 0
elif event.key == K_DOWN:
self.vy = 1
self.vx = 0
elif event.key == K_LEFT:
self.vx = -1
self.vy = 0
elif event.key == K_RIGHT:
self.vx = 1
self.vy = 0
def move(self):
self.y += self.vy
self.x += self.vx
self.sprite.rect.topleft = [int(self.x), int(self.y)]
self.sprite.mask = pygame.mask.from_surface(self.sprite.image)
def draw(self, surface):
surface.blit(self.sprite.image, self.sprite.rect)
def position(self):
return self.sprite.rect()
class wall_class(pygame.sprite.Sprite):
def __init__(self, surface):
self.surface = surface
self.x = 250
self.y = 100
self.vy = 0
self.sprite = pygame.sprite.Sprite()
self.sprite.image = pygame.image.load("ball.png").convert()
self.sprite.rect = self.sprite.image.get_rect(center = (self.x, self.y))
self.sprite.rect.topleft = [int(self.x), int(self.y)]
self.sprite.mask = pygame.mask.from_surface(self.sprite.image)
def draw(self, surface):
surface.blit(self.sprite.image, self.sprite.rect)
def collision_action(self):
self.vy = ball.vy
self.vx = ball.vx
self.x += self.vx
self.y += self.vy
self.sprite.rect.topleft = [int(self.x), int(self.y)]
def gameQuit():
pygame.quit()
sys.exit()
screen = pygame.display.set_mode((500, 500), 0, 32)
ball = ball_class(screen)
wall = wall_class(screen)
clock = pygame.time.Clock()
while True:
screen.fill((0, 0, 0))
ball.move()
ball.draw(screen)
wall.draw(screen)
is_a_collision = pygame.sprite.collide_mask(wall.sprite, ball.sprite)
if is_a_collision:
wall.collision_action()
for event in pygame.event.get():
if event.type == QUIT:
gameQuit()
elif event.type == KEYDOWN:
ball.event(event)
clock.tick(100)
pygame.display.update()
The bitmask collision is working in your code, but I think the code is making it more difficult because the sprite objects ball_class and wall_class, have separate sub-sprite objects defined inside them.
class ball_class(pygame.sprite.Sprite):
def __init__(self, surface):
self.surface = surface
self.x = 250
self.y = 250
self.vy = 0
self.vx = 0
self.sprite = pygame.sprite.Sprite() # <-- HERE
...
Generally when the code "subclasses" an existing class/object it becomes one of the same. So it is not necessary to have a secondary sprite contained within the class. This also makes the code and logic more involved, tricky and confusing.
Please consider this re-working of your ball_class:
class ball_class(pygame.sprite.Sprite):
def __init__(self, surface):
pygame.sprite.Sprite.__init__(self) # <-- HERE Must initialise sprites
self.x = 250
self.y = 250
self.vy = 0
self.vx = 0
self.image = pygame.image.load("ball_64.png").convert_alpha()
self.rect = self.image.get_rect(center = (self.x, self.y))
self.mask = pygame.mask.from_surface(self.image)
Note the image, rect and mask member variables. Previously the code was using these off the contained sprite. But the PyGame library is written to expect these as members of the class, and it doesn't work properly if they're not defined. Also your two sprite classes were not calling the base sprite initialiser function.
Here is a re-working of your code, which - when the ball hits the corner of the wall, collides properly (with bit-masks).
ball_64.png
wall.png
import pygame, sys, math
from pygame.locals import *
pygame.init()
class ball_class(pygame.sprite.Sprite):
def __init__(self, surface):
pygame.sprite.Sprite.__init__(self)
self.surface = surface
self.x = 250
self.y = 250
self.vy = 0
self.vx = 0
self.image = pygame.image.load("ball_64.png").convert_alpha()
self.rect = self.image.get_rect(center = (self.x, self.y))
# self.sprite.rect.topleft = [int(self.x), int(self.y)]
self.mask = pygame.mask.from_surface(self.image)
def event(self, event):
if event.key == K_UP:
self.vy = -1
self.vx = 0
elif event.key == K_DOWN:
self.vy = 1
self.vx = 0
elif event.key == K_LEFT:
self.vx = -1
self.vy = 0
elif event.key == K_RIGHT:
self.vx = 1
self.vy = 0
def move(self):
self.y += self.vy
self.x += self.vx
self.rect.topleft = [int(self.x), int(self.y)]
#self.mask = pygame.mask.from_surface(self.sprite.image)
def draw(self, surface):
surface.blit(self.image, self.rect)
def position(self):
return self.rect()
class wall_class(pygame.sprite.Sprite):
def __init__(self, surface):
pygame.sprite.Sprite.__init__(self)
self.x = 250
self.y = 100
self.vy = 0
self.image = pygame.image.load("wall.png").convert_alpha()
self.rect = self.image.get_rect(center = (self.x, self.y))
# self.sprite.rect.topleft = [int(self.x), int(self.y)]
self.mask = pygame.mask.from_surface( self.image )
def draw(self, surface):
surface.blit(self.image, self.rect)
def collision_action(self, by_sprite_at ):
print("wall_class.collision_action( by_sprite_at=%s )" % ( str( by_sprite_at ) ) )
# self.vy = ball.vy
# self.vx = ball.vx
# self.x += self.vx
# self.y += self.vy
# self.sprite.rect.topleft = [int(self.x), int(self.y)]
def gameQuit():
pygame.quit()
sys.exit()
screen = pygame.display.set_mode((500, 500), 0, 32)
ball = ball_class(screen)
wall = wall_class(screen)
clock = pygame.time.Clock()
while True:
screen.fill((128,128,128))
ball.move()
ball.draw(screen)
wall.draw(screen)
is_a_collision = pygame.sprite.collide_mask(wall, ball)
if is_a_collision:
wall.collision_action( ball.rect.center )
for event in pygame.event.get():
if event.type == QUIT:
gameQuit()
elif event.type == KEYDOWN:
ball.event(event)
clock.tick(100)
pygame.display.update()