There is a "player" (sprite) and a group of walls (other_group), also sprites. When creating a wall with NOT the same x/y coordinates, the sprite.spritecollide method does not work correctly. Horizontally everything is ok, vertically the player just falls into the wall.
fullcode
class Tile(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(other_group)
self.image = wall_images
self.rect = self.image.get_rect().move(x, y)
# player
class Player(pygame.sprite.Sprite):
def __init__(self, speed, x, y):
super().__init__(player_group)
self.speed = speed
self.frames = []
self.cur_frame = 0
self.image = animation_down[0]
self.rect = self.image.get_rect().move(x, y)
self.rect.x = x
self.rect.y = y
def possibility_of_movement(self, directory_of_movement):
if directory_of_movement == 'right':
tester = Player(self.speed, self.rect.x + self.speed, self.rect.y)
elif directory_of_movement == 'left':
tester = Player(self.speed, self.rect.x - self.speed, self.rect.y)
elif directory_of_movement == 'up':
tester = Player(self.speed, self.rect.y - self.speed, self.rect.x)
else:
tester = Player(self.speed, self.rect.y + self.speed, self.rect.x)
if pygame.sprite.spritecollide(tester, other_group, dokill=False, collided=pygame.sprite.collide_rect_ratio(0.7)):
tester.kill()
return False
else:
return True
tile2 = Tile(264, 264)
tile3 = Tile(264+64, 264)
tile4 = Tile(446, 446)
tile4 = Tile(382, 382)
while running:
if keys[pygame.K_LEFT] and pos_x > 5 and player.possibility_of_movement('left'):
pos_x -= player.speed
left = True
right = False
down = False
up = False
I can't test it but I think all problem is because you set values in wrong order when you create tester - and it checks collision with wrong place (with place which doesn't have any wall) and it doesn't stop.
For up (and similar for down) you set y-speed,x but you have to set x, y-speed
Correct oder of values in Player()
elif directory_of_movement == 'up':
tester = Player(self.speed, self.rect.x, self.rect.y - self.speed)
else:
tester = Player(self.speed, self.rect.x, self.rect.y + self.speed)
Related
The helicopter flies from right to left. When a key is pressed it crashes. In the code excerpt, it flies to the bottom right corner.
Then another helicopter should come from the left and fly straight ahead to the right. That doesn't happen. He comes from the left and immediately falls again, although the angle has been reset.
breite,hoehe = 1200,800
screen = pygame.display.set_mode((breite,hoehe))
class Helikopter(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Bilder/heli1.png").convert_alpha()
self.img = pygame.transform.scale(self.image,(160,60))
self.rect = self.img.get_rect()
self.rect.x = - 20
self.rect.y = random.randrange(100,300)
self.speed = 10
self.absturz = False
self.angle = 0
def update(self):
self.rect.x += 2
self.rect.y += 0
if self.rect.x > breite or self.rect.y > hoehe:
self.rect.x = - 20
self.rect.y = random.randrange(100,300)
self.absturz == False
if self.absturz == True:
x = breite - self.rect.x
y = hoehe - self.rect.y
self.dist = math.sqrt(x ** 2 + y ** 2)
self.rect.x += self.speed *x / self.dist
self.rect.y += self.speed *y / self.dist
self.angle = math.degrees(-math.atan2(y, x))
else:
self.absturz = False
self.angle = 0
self.image = pygame.transform.rotozoom(self.img, self.angle,1)
heli_sprites = pygame.sprite.Group()
heli = Helikopter()
heli_sprites.add(heli)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
heli.absturz = True
screen.fill((250,0,0))
heli_sprites.draw(screen)
heli_sprites.update()
pygame.display.flip()
There are 2 problems:
self.absturz == False is a comparison, but not an assignement
self.absturz needs to be set True when self.rect hits the ground (self.absturz == True)
class Helikopter(pygame.sprite.Sprite):
# [...]
def update(self):
self.rect.x += 2
self.rect.y += 0
if self.rect.x > breite or self.rect.y > hoehe:
self.rect.x = - 20
self.rect.y = random.randrange(100,300)
self.absturz = True # <---
# [...]
I am creating a game where you shoot enemies in a top-down format. My bullet and enemy function properly in every aspect except where it has to delete itself. I have hooked it up to where if the bullet collides with the enemy or 5 seconds have passed, the bullet would delete itself. It does do that, except the code continually iterates to create a sprite and kill it as the bullet moves for some reason, and still shows the bullet on the screen as if nothing is happening to it. Can someone tell me why the code is iterating the creation of a bullet and the deletion of it so many times?
-------- CODE --------
method in player class
def shoot(self):
pos = pygame.mouse.get_pos()
pos += camera_group.offset
click = pygame.mouse.get_pressed()
if click[0] and not self.shooting:
self.shooting = True
# create bullet instance if player shoots
bullet = Bullet(self.rect.centerx, self.rect.centery, "bullet", pos, camera_group)
bullet_group.add(bullet)
if not click[0]:
self.shooting = False
bullet class
class Bullet(Entity):
def __init__(self, x, y, image, pos, group):
super().__init__(x, y, image, group)
pygame.sprite.Sprite.__init__(self)
# creating and scaling image
self.image = pygame.transform.flip(self.image, True, False)
self.image = pygame.transform.scale(self.image, (32, 16))
# creating rect
self.x = x
self.y = y
self.speed = 8
self.mousepos = pos
# calculating angle of bullet
self.angle = math.atan2(pos[1] - y, pos[0] - x)
self.angle_deg = (self.angle * (180 / math.pi))
self.image = pygame.transform.rotate(self.image, self.angle_deg * -1)
# calculating vel_x/vel_y
dx = self.mousepos[0] - player.rect.centerx
dy = self.mousepos[1] - player.rect.centery
total = abs(dx) + abs(dy)
self.vel_x = (self.speed * (dx / total))
self.vel_y = (self.speed * (dy / total))
# setting variable to store time of bullet creation
self.start_time = pygame.time.get_ticks()
def update(self):
if pygame.time.get_ticks() - self.start_time > 50 and len(bullet_group) >= 1:
bullet_group.sprites()[0].kill()
elif self.rect.colliderect(enemy):
bullet_group.sprites()[0].kill()
enemy.kill()
else:
# movement
self.x += self.vel_x
self.y += self.vel_y
self.rect.x = int(self.x)
self.rect.y = int(self.y)
Don't kill the first bullet in the Group, just delete the bullet object itself:
bullet_group.sprites()[0].kill()
self.kill()
method update in class Bullet:
class Bullet(Entity):
# [...]
def update(self):
if pygame.time.get_ticks() - self.start_time > 50 and len(bullet_group) >= 1:
self.kill()
elif self.rect.colliderect(enemy):
self.kill()
enemy.kill()
else:
# movement
self.x += self.vel_x
self.y += self.vel_y
self.rect.x = int(self.x)
self.rect.y = int(self.y)
This question already has answers here:
How do I detect collision in pygame?
(5 answers)
Closed 1 year ago.
import libraries and define variables
import pygame, sys
import math
#variables globales
pygame.init()
size = width, height = 1000,700
screen = pygame.display.set_mode(size)
white = 255,255,255
reloj = pygame.time.Clock()
x = 100
y = 550
canjump = False
level = 3
this is my player class
class Player:
x = 100
y = 550
jump = 0
change_x = 0
change_y = 0
ha_saltado = False
top = 550
plataformaTop = False
def __init__(self):
super().__init__()
square_size = 50,50
self.image = pygame.Surface((square_size))
self.image.fill(white)
#establecer referencia de la imagen
self.rect = self.image.get_rect()
self.impulso_salto = 10
draw my player with x and y coordinates
def dibujar(self):
self.x += self.change_x
self.rect.x = self.x + self.change_x
self.rect.y = self.y
screen.blit(self.image, (self.x,self.y))
#movimiento horizontal
change player coordinates
def derecha (self):
self.change_x = 5
def izquierda (self):
self.change_x = -5
def stop (self):
self.change_x = 0
gravity function
#gravedad
def grav(self):
if self.ha_saltado:
if self.impulso_salto>= -10:
if self.impulso_salto < 0:
self.y += (self.impulso_salto ** 2)*0.5
else:
self.y -= (self.impulso_salto ** 2)*0.5
self.impulso_salto -= 1
else:
self.ha_saltado = False
canjump = False
self.impulso_salto = 10
def salto(self):
Player.grav(self)
def colisiones(self):
self.y += 2
if self.rect.colliderect(plataforma.rect):
self.plataformaTop = True
else:
self.plataformaTop = False
self.y -= 2
if self.plataformaTop:
print(plataforma.rect.top, self.y)
self.y -= 6
this is my stage class, here i make a white rectangle and i want my player can collide with the rectangle
class Escenario:
def __init__(self):
super().__init__()
here i create the rectangle
self.image = pygame.Surface([300,100])
self.image.fill(white)
self.rect = self.image.get_rect()
def dibujar(self, x, y):
screen.blit(self.image, (x, y))
self.rect.x = x
self.rect.y = y
here I create the events and initialize the objects
plataforma = Escenario()
player = Player()
texto = Texto()
pygame.mixer.music.load("C:/Users/Mati/Desktop/game/ost.mp3")
pygame.mixer.music.play()
while 1:
screen.fill((0,0,0))
player.colisiones()
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.derecha()
if event.key == pygame.K_LEFT:
player.izquierda()
if event.key == pygame.K_x:
player.stop()
if event.key == pygame.K_UP:
canjump = True
player.ha_saltado = True
gravity control
if canjump == True and player.ha_saltado == True:
player.salto()
else:
player.ha_saltado = False
if player.y < 550:
player.y += 10
level control(when the player x coordinate is greater than the screen width, the level increases)
if player.x >= width:
level += 1
player.x = 100
if level == 1:
texto.write("welcome to monochromatic adventure", 43, 200)
if level == 3:
plataforma.dibujar(800,500)
player.colisiones()
if level == 2:
texto.write("stage 1: Anguish", 43, 200)
update the game frames and draw the objects
#dibujar escenario
player.dibujar()
pygame.draw.line(screen, white, [1000, 600], [0, 600], 2)
reloj.tick(60)
pygame.display.flip()
There is a bug in the dibujar method. It must be self.rect.x = self.x instead of self.rect.x = self.x + self.change_x:
class Player:
# [...]
def dibujar(self):
self.x += self.change_x
self.rect.x = self.x
self.rect.y = self.y
screen.blit(self.image, self.rect)
I was just getting some help to figure out how to get my player fire bullets when I realized that they only go (kinda expected this but however as only had y value for movement). I don't know how I'll make the bullets fire off in the direction the player is facing.
I have some idea of what to but I just don't know how to do it... I thought I could somehow use the cursor and player tracking that's in this game for the visuals but I don't know how to make that a one-time thing instead of a constant. For diagonal movement of the bullet, I have no clue.
Code below (split into two parts/file Main.py and PlayerSprite.py):
Main:
py.init()
py.mixer.init()
screen = py.display.set_mode((WIDTH, HEIGHT))
py.display.set_caption("Dimensional Drifter")
clock = py.time.Clock()
all_sprites = py.sprite.Group()
NPCs = py.sprite.Group()
bullets = py.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(14):
n = NPC(player)
all_sprites.add(n)
NPCs.add(n)
# Game loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
for event in py.event.get():
# check for closing window
if event.type == py.QUIT:
running = False
elif event.type == py.KEYDOWN:
if event.key == py.K_SPACE:
New_bullet = player.Shoot()
all_sprites.add(New_bullet)
bullets.add(New_bullet)
# Update
all_sprites.update()
# # check if there a collision between the bullet and NPC
hits = py.sprite.groupcollide(NPCs, bullets, True, True)
# check if there a collision between the player and NPC
hits = py.sprite.spritecollide(player, NPCs, True)
if hits:
running = False
# updates the position of of mouse and rotates it towards the mouse position
mouse_x, mouse_y = py.mouse.get_pos()
player.rotate(mouse_x, mouse_y)
# render
screen.fill(BLACK)
all_sprites.draw(screen)
# flip the display
py.display.flip()
py.quit()
PlayerSprite
import pygame as py
import math
import random
WIDTH = 800
HEIGHT = 600
FPS = 60
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
class Player(py.sprite.Sprite):
def __init__(self):
py.sprite.Sprite.__init__(self)
self.image = py.Surface((40, 40), py.SRCALPHA)
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT / 2
self.Yspeed = 0
self.rotatableimage = self.image
def update(self):
self.Xspeed = 0
self.Yspeed = 0
# line below allow for key press to equate to move of sprite
keypreesed = py.key.get_pressed()
if keypreesed[py.K_a]:
self.Xspeed = - 11
if keypreesed[py.K_d]:
self.Xspeed = 11
if keypreesed[py.K_w]:
self.Yspeed = - 11
if keypreesed[py.K_s]:
self.Yspeed = 11
self.rect.x += self.Xspeed
self.rect.y += self.Yspeed
# line below allow the sprite to wrap around the screen
if self.rect.left > WIDTH:
self.rect.right = 0
if self.rect.right < 0:
self.rect.left = WIDTH
if self.rect.top > HEIGHT:
self.rect.top = 0
if self.rect.bottom < 0:
self.rect.bottom = HEIGHT
def rotate(self, mouse_x, mouse_y):
rel_x = mouse_x - self.rect.x
rel_y = mouse_y - self.rect.y
angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
self.image = py.transform.rotate(self.rotatableimage, int(angle))
self.rect = self.image.get_rect(center=(self.rect.centerx, self.rect.centery))
return
def Shoot(self):
return Bullet(self.rect.centerx, self.rect.top)
class NPC(py.sprite.Sprite):
def __init__(self, player):
py.sprite.Sprite.__init__(self)
self.player = player
self.image = py.Surface((30, 30)).convert_alpha()
self.image.fill(RED)
self.originalimage = self.image
self.rect = self.image.get_rect()
self.spawn()
# allows of spawning from all four side of the screen and set the x, y speed and spawn position
def spawn(self):
self.direction = random.randrange(4)
if self.direction == 0:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.Xspeed = random.randrange(-2, 2)
self.Yspeed = random.randrange(4, 8)
elif self.direction == 1:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(HEIGHT, HEIGHT + 60)
self.Xspeed = random.randrange(-2, 2)
self.Yspeed = -random.randrange(4, 8)
elif self.direction == 2:
self.rect.x = random.randrange(-100, -40)
self.rect.y = random.randrange(HEIGHT - self.rect.height)
self.Xspeed = random.randrange(4, 8)
self.Yspeed = random.randrange(-2, 2)
elif self.direction == 3:
self.rect.x = random.randrange(WIDTH, WIDTH + 60)
self.rect.y = random.randrange(HEIGHT - self.rect.height)
self.Xspeed = -random.randrange(4, 8)
self.Yspeed = random.randrange(-2, 2)
def update(self):
self.rect.x += self.Xspeed
self.rect.y += self.Yspeed
# makes it so that NPC point to wards the player as it passes from side to side
dir_x, dir_y = self.player.rect.x - self.rect.x, self.player.rect.y - self.rect.y
self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
self.image = py.transform.rotate(self.originalimage, self.rot)
# Respawns the NPC when they hit an side
if self.direction == 0:
if self.rect.top > HEIGHT + 10:
self.spawn()
elif self.direction == 1:
if self.rect.bottom < -10:
self.spawn()
elif self.direction == 2:
if self.rect.left > WIDTH + 10:
self.spawn()
elif self.direction == 3:
if self.rect.right < -10:
self.spawn()
class Bullet(py.sprite.Sprite):
def __init__(self, x, y):
py.sprite.Sprite.__init__(self)
self.image = py.Surface((5, 5))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.Yspeed = -10
def update(self):
self.rect.y += self.Yspeed
# kill if moved of screen
if self.rect.bottom > HEIGHT or self.rect.top < 0:
self.kill()
if self.rect.right > WIDTH or self.rect.left < 0:
self.kill()
Add 2 attributes self.lastX and self.lastY to the class Player and change the attributes when the player changes the direction:
class Player(py.sprite.Sprite):
def __init__(self):
# [...]
self.lastX = 0
self.lastY = -10
def update(self):
# [...]
self.rect.x += self.Xspeed
self.rect.y += self.Yspeed
if self.Xspeed != 0 or self.Yspeed != 0:
self.lastX = self.Xspeed
self.lastY = self.Yspeed
Add an argument Xspeed ans Yspeed to the class Bullet
class Bullet(py.sprite.Sprite):
def __init__(self, x, y, Xspeed, Yspeed):
py.sprite.Sprite.__init__(self)
self.image = py.Surface((5, 5))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.Xspeed = Xspeed
self.Yspeed = Yspeed
def update(self):
self.rect.x += self.Xspeed
self.rect.y += self.Yspeed
# [...]
Set the attributes when the bullet spawns
class Player(py.sprite.Sprite):
# [...]
def Shoot(self):
return Bullet(self.rect.centerx, self.rect.centery, self.lastX, self.lastY)
Alternatively it is also possible to set the speed dependent on the direction to the mouse cursor.
Get the position of the player and the mouse cursor and compute the x and y distance (Vector ):
pos = self.rect.center
mpos = py.mouse.get_pos()
vx = mpos[0] - pos[0]
vy = mpos[1] - pos[1]
If the mouse position and the bullet position are equal, that does not make any sense, thus the bullet is skipped
if vx == 0 and vy == 0:
return None
Of course this vector is far to long, if you would use it for the direction (Xspeed, Yspeed) directly, then the bullet would step to the mouse cursor in one turn.
In the following I use pygame.math.Vector2, because it provides the handy method scale_to_length, that scales a vector to a specified Euclidean length:
direction = py.math.Vector2(vx, vy)
direction.scale_to_length(10)
Now the x and y component of the vector contain the x and y component of the speed. Since the components are floating point values, they are round to integral values:
return Bullet(pos[0], pos[1], round(direction.x), round(direction.y))
Method Shoot:
class Player(py.sprite.Sprite):
# [...]
def Shoot(self):
pos = self.rect.center
mpos = py.mouse.get_pos()
vx, vy = mpos[0] - pos[0], mpos[1] - pos[1]
if vx == 0 and vy == 0:
return None
direction = py.math.Vector2(vx, vy)
direction.scale_to_length(10)
return Bullet(pos[0], pos[1], round(direction.x), round(direction.y))
Note, if you set the bullet dependent on the direction to the mouse cursor, then it may be useful to spawn the bullet by a mouse click:
while running:
# [...]
for event in py.event.get():
if event.type == py.QUIT:
# [...]
elif event.type == py.MOUSEBUTTONDOWN:
if event.button == 1:
New_bullet = player.Shoot()
if New_bullet:
all_sprites.add(New_bullet)
bullets.add(New_bullet)
You can use pygames Vector2 to move in any direction. You calculate the angle of the player you can use that.
class Player(py.sprite.Sprite):
def __init__(self):
...
self.angle = 0
def rotate(self, mouse_x, mouse_y):
...
self.angle = -angle #make negative otherwise it will be going away from mouse
def Shoot(self):
return Bullet(self.rect.centerx, self.rect.top, py.math.Vector2(1,0).rotate(self.angle))
then in your bullet class, get the direction and add to its position
class Bullet(py.sprite.Sprite):
def __init__(self, x, y, Dir):
...
self.Dir = Dir
def update(self):
self.rect.y += self.Dir[1] * self.speed
self.rect.x += self.Dir[0] * self.speed
...
I am building a top down shooter in the style of Raiden 2. I need to know how to get access to the enemy object when I detect collision using 'spritecollide'.
The reason I need the enemy object is so that I can lower their energy level for every bullet that hits them.
This is the dictionary that I have to work with:
enemyCollisions = pygame.sprite.groupcollide(shipBulletGroup, enemyGroup, True, False)
Here's my Bullet class:
class Bullet(Entity):
def __init__(self, ship, angle):
Entity.__init__(self)
self.speed = 8
self.level = 0
self.image = pygame.Surface((BULLET_DIMENSIONS, BULLET_DIMENSIONS)).convert()
self.rect = self.image.get_rect()
self.rect.x = ship.rect.centerx
self.rect.y = ship.rect.top
self.angle = angle
def update(self, enemyGroup):
if self.rect.bottom < 0:
self.kill()
else:
self.rect.y -= self.speed
Here's my unfinished Enemy class:
class Enemy_1(Entity):
def __init__(self):
Entity.__init__(self)
self.speed = (1, 1)
self.energy = 10
self.begin = False
self.run = False
self.image = pygame.Surface((WIN_W/5, WIN_H/15)).convert()
self.image.fill((70, 70, 70))
self.rect = self.image.get_rect()
self.rect.centerx = WIN_W/1.333
self.rect.y = -WIN_H/15
def shoot(self):
bullet_1 = Bullet(self, 270)
bullet_2 = Bullet(self, 225)
bullet_3 = Bullet(self, 315)
def update(self, enemyGroup, timer):
if timer == 300:
self.begin = True
elif timer == 900:
self.run = True
elif timer > 900 and self.rect.x > WIN_W:
self.remove(enemyGroup)
if self.run:
self.rect.x += self.speed[0]
self.rect.y += self.speed[1]
elif self.begin:
self.rect.y += self.speed[1]
if self.rect.y > WIN_H/4:
self.rect.y = WIN_H/4
if self.energy == 0:
self.kill()
Here's the complete program:
import sys, pygame, os, random, math
from ast import literal_eval
# Force static position of screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Constants
LEFT = 'left'
RIGHT = 'right'
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PILL_WIDTH = 5
PILL_HEIGHT = 20
WIN_W = 500
WIN_H = 800
SHIP_WIDTH = WIN_W/15
SHIP_HEIGHT = WIN_H/15
BULLET_DIMENSIONS = 5
TIMER = 0
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Text(Entity):
def __init__(self, text, size, color, position, font=None):
Entity.__init__(self)
self.color = color
self.font = pygame.font.Font(font, size)
self.text = text
self.set(text, position)
def set(self, text, position):
self.image = self.font.render(str(text), 1, self.color)
self.rect = self.image.get_rect()
self.rect.move_ip(position[0]-self.rect.width/2, (position[1]-self.rect.height)/2)
class Ship(Entity):
def __init__(self, container):
Entity.__init__(self)
self.speed = 5
self.score = 0
self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect.centerx = container.centerx
self.rect.y = container.centery
def update(self, bulletGroup):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.rect.centery -= self.speed
if key[pygame.K_s]:
self.rect.centery += self.speed
if key[pygame.K_d]:
self.rect.centerx += self.speed
if key[pygame.K_a]:
self.rect.centerx -= self.speed
if key[pygame.K_SPACE]:
if TIMER % 7 == 0:
bullet = Bullet(self, 90, 'friend')
bulletGroup.add(bullet)
# Ship Movement Boundaries
if self.rect.y < WIN_H/25:
self.rect.y = WIN_H/25
if self.rect.y > WIN_H - SHIP_HEIGHT:
self.rect.y = WIN_H - SHIP_HEIGHT
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > WIN_W - SHIP_WIDTH:
self.rect.x = WIN_W - SHIP_WIDTH
class Bullet(Entity):
def __init__(self, ship, angle, type):
Entity.__init__(self)
self.speed = 8
self.level = 0
self.image = pygame.Surface((BULLET_DIMENSIONS, BULLET_DIMENSIONS)).convert()
self.rect = self.image.get_rect()
self.dx = math.cos(math.radians(angle)) * self.speed
self.dy = math.sin(math.radians(angle)) * self.speed
self.setXY(ship, type)
def setXY(self, ship, type):
self.rect.x = ship.rect.centerx
if type == 'friend':
self.rect.y = ship.rect.top
elif type == 'enemy':
self.rect.y = ship.rect.bottom
def update(self):
if self.rect.bottom < 0:
self.kill()
else:
self.rect.x -= self.dx
self.rect.y -= self.dy
if type == 'friend':
self.rect.y = -self.rect.y
self.rect.x = -self.rect.x
class Powerup(Entity):
def __init__(self, xden, pillCount):
Entity.__init__(self)
self.speed = 3
self.density = xden
self.image = pygame.Surface((PILL_WIDTH, PILL_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect = self.rect.move(100,100)
def restart(self):
pass
def update(self):
if self.rect.y > WIN_H:
del self
else:
self.rect = self.rect.move((0, self.speed))
class Enemy(Entity):
def __init__(self):
Entity.__init__(self)
self.begin = False
self.run = False
self.color = (153, 0, 76)
class Planes(Enemy):
def __init__(self, speed, energy, size, location):
Enemy.__init__(self)
self.speed = speed
self.energy = energy
self.image = pygame.Surface(size).convert()
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.rect.centerx = location[0]
self.rect.y = location[1]
def shoot(self, enemyBulletGroup):
bullet_1 = Bullet(self, 240, 'enemy')
bullet_2 = Bullet(self, 270, 'enemy')
bullet_3 = Bullet(self, 300, 'enemy')
enemyBulletGroup.add(bullet_1, bullet_2, bullet_3)
def update(self, enemyGroup, enemyBulletGroup):
if TIMER == 300:
self.begin = True
elif TIMER == 900:
self.run = True
elif self.rect.x > WIN_W or self.energy == 0:
self.kill()
if self.run:
self.rect.x += self.speed[0]
self.rect.y += self.speed[1]
if TIMER % 100 == 0:
self.shoot(enemyBulletGroup)
elif self.begin:
self.rect.y += self.speed[1]
if self.rect.y > WIN_H/4:
self.rect.y = WIN_H/4
if TIMER % 100 == 0:
self.shoot(enemyBulletGroup)
def main():
# Initialize Everything
pygame.init()
global TIMER
fps = 60
lLeft = lRight = lUp = lDown = shoot = False
clock = pygame.time.Clock()
play = True
pygame.display.set_caption('Pong')
screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.SRCALPHA)
# Create Game Objects
ship = Ship(pygame.rect.Rect(0, 0, WIN_W, WIN_H))
score1 = Text("Score: " + str(ship.score), 40, BLACK, (WIN_W/2, (WIN_H/25)))
enemy_1 = Planes((1, 1), 10, (WIN_W/5, WIN_H/15), (WIN_W/1.4, -WIN_H/15))
# Create Groups
powerupGroup = pygame.sprite.Group()
shipGroup = pygame.sprite.Group()
shipGroup.add(ship)
textGroup = pygame.sprite.Group()
textGroup.add(score1)
shipBulletGroup = pygame.sprite.Group()
enemyBulletGroup = pygame.sprite.Group()
enemyGroup = pygame.sprite.Group()
enemyGroup.add(enemy_1)
# Gameplay
while play:
# Checks if window exit button pressed
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# Update
powerupGroup.update()
ship.update(shipBulletGroup)
shipBulletGroup.update()
textGroup.update()
enemyGroup.update(enemyGroup, enemyBulletGroup)
enemyBulletGroup.update()
enemyCollisions = pygame.sprite.groupcollide(shipBulletGroup, enemyGroup, True, False)
for key in enemyCollisions:
print enemyCollisions[key].energy
# Print Background/Sprites
screen.fill(WHITE)
powerupGroup.draw(screen)
shipGroup.draw(screen)
shipBulletGroup.draw(screen)
textGroup.draw(screen)
enemyGroup.draw(screen)
enemyBulletGroup.draw(screen)
# Print Score Bar
hori_partition = pygame.Surface((WIN_W, 1))
screen.blit(hori_partition, (0, WIN_H/25))
TIMER += 1
# Limits frames per iteration of while loop
clock.tick(fps)
# Writes to main surface
pygame.display.flip()
if __name__ == "__main__":
main()
The pygame.sprite.groupcollide() function returns a dictionary which values are list objects:
Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.
Thats the reason why calling print enemyCollisions[key].energy when iterating over the return dict fails, since a list object -- the value of enemyCollisions[key] -- does not have an energy() method.
To access all values of a dictionary in Python 2.X you could use the .itervalues() method of a dict instance. To get each colliding sprite, iterate again over these values:
enemyCollisions = pygame.sprite.groupcollide(shipBulletGroup, enemyGroup, True, False)
#iterate over all values of the enemyCollisions dict
for enemyCollisions in enemyCollisions.itervalues():
#access all enemies which collided with an shipBullet
for enemy in enemyCollisions:
print enemy.energy
The syntax for accessing the collided sprite's property is as follows:
collisions = pygame.sprite.spritecollide(self, pillGroup, True)
for key in collisions:
self.density += key.density