How to use groupcollide? - python

So I've been wondering how to use the pygame groupcollide. And I'm utterly stumped right now. As I am using collide_rect and it is fine. But for groupcollide I can't seem to figure out how to call the properties of the item inside of that group. And I can't do collide rect because there's going to be a lot of bullets.
def check_blast_collisions(player,bullet):
hits = pg.sprite.groupcollide(player,bullet,False,True)
for hit in hits:
print (hits)
if hit.vx == 20:
player.vx += 40
elif hit.vx == -20:
player.vx += -40
Here is a snippet of where I'm trying to use groupcollide.
After I made this function, the bullets don't even show up. (The bullets are supposed to be called blasts but I forgot about it in this function.)
import pygame as pg
#settings
CAPTION = "Knockback Arena"
resolution = 1600,900
WIDTH = resolution[0]
HEIGHT = resolution[1]
FPS = 60
player_jump_height = 30
player_max_fall_speed = 30
player_fall_speed_increase = 2
player_lives = 5
shoot_cooldown = 500
#initialize pygame
pg.init()
pg.mixer.init()
pg.font.init
screen = pg.display.set_mode(resolution)
pg.display.set_caption(CAPTION)
clock = pg.time.Clock()
#sprites
class Platform(pg.sprite.Sprite):
def __init__(self,x,y,width,height,r,g,b):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((width,height))
self.image.fill((r,g,b))
self.rect = self.image.get_rect()
self.rect.center = (x,y)
class Player(pg.sprite.Sprite):
def __init__(self,r,g,b,x,y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((40, 100))
self.image.fill((r,g,b))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.startx = x
self.starty = y
self.vx = 0
self.vy = 5
self.vy_max = player_max_fall_speed
self.vy_increase = player_fall_speed_increase
self.lives = player_lives
self.last_shot = 0
self.facing_right = False
self.facing_left = False
def update(self):
self.rect.x += self.vx
self.rect.y += self.vy
if self.vy >= self.vy_max:
self.vy = self.vy_max
self.vy_increase = 0
if self.vy < self.vy_max:
self.vy_increase = player_fall_speed_increase
if self.rect.bottom < HEIGHT:
self.vy += self.vy_increase
if self.rect.top >= HEIGHT:
self.rect.x = self.startx
self.rect.y = self.starty
self.lives -= 1
if self.lives <= 0:
self.kill()
if self.rect.right >= WIDTH:
self.rect.right = WIDTH
self.vx = 0
if self.rect.left <= 0:
self.rect.left = 0
self.vx = 0
def jump(self):
if self.rect.bottom >= main_platform.rect.top:
self.vy -= player_jump_height
if self.rect.bottom >= HEIGHT:
self.vy -= player_jump_height
def shoot(self):
if pg.time.get_ticks() - self.last_shot >= shoot_cooldown:
if self.facing_left == True:
return "shoot_left"
elif self.facing_right == True:
return "shoot_right"
else:
return "cd_not_done"
class Blast(pg.sprite.Sprite):
def __init__(self,player,direction):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((20,10))
self.image.fill((0,255,255))
self.rect = self.image.get_rect()
self.rect.center = (player.rect.center)
self.direction = direction
if self.direction == 0:
self.vx = -20
elif self.direction == 1:
self.vx = 20
def update(self):
self.rect.x += self.vx
if self.rect.right < 0:
self.kill()
if self.rect.left > WIDTH:
self.kill()
#functions
def check_for_collisions(player,platform):
hits = pg.sprite.collide_rect(player,platform)
if hits:
if hits and player.vy > 0:
player.rect.bottom = platform.rect.top
player.vy = 0
def check_blast_collisions(player,bullet):
hits = pg.sprite.groupcollide(player,bullet,False,True)
for hit in hits:
print (hits)
if hit.vx == 20:
player.vx += 40
elif hit.vx == -20:
player.vx += -40
font = pg.font.Font('font/Roboto-Light.ttf', 30)
all_sprites = pg.sprite.Group()
players = pg.sprite.Group()
platforms = pg.sprite.Group()
blasts = pg.sprite.Group()
main_platform = Platform(WIDTH/2,650,1000,100,0,200,0)
player_1 = Player(0,0,255,WIDTH/2 + -100,200)
player_2 = Player(255,0,0,WIDTH/2 + 100,200)
platforms.add(main_platform)
players.add(player_1)
players.add(player_2)
all_sprites.add(player_1)
all_sprites.add(player_2)
all_sprites.add(main_platform)
menu = True
run = True
while run:
#check for closing window
for event in pg.event.get():
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
player_1.jump()
if event.key == pg.K_a:
player_1.vx = -10
player_1.facing_left = True
player_1.facing_right = False
elif event.key == pg.K_d:
player_1.vx = 10
player_1.facing_right = True
player_1.facing_left = False
if event.key == pg.K_UP:
player_2.jump()
if event.key == pg.K_LEFT:
player_2.vx = -10
player_2.facing_left = True
player_2.facing_right = False
elif event.key == pg.K_RIGHT:
player_2.vx = 10
player_2.facing_right = True
player_2.facing_left = False
if event.key == pg.K_j:
if player_1.shoot() == "shoot_left":
b = Blast(player_1,0)
all_sprites.add(b)
blasts.add(b)
elif player_1.shoot() == "shoot_right":
b = Blast(player_1,1)
all_sprites.add(b)
blasts.add(b)
if event.key == pg.K_KP1:
if player_2.shoot() == "shoot_left":
b = Blast(player_2,0)
all_sprites.add(b)
blasts.add(b)
elif player_2.shoot() == "shoot_right":
b = Blast(player_2,1)
all_sprites.add(b)
blasts.add(b)
elif event.type == pg.KEYUP:
if event.key == pg.K_a:
player_1.vx = 0
if event.key == pg.K_d:
player_1.vx = 0
if event.key == pg.K_LEFT:
player_2.vx = 0
if event.key == pg.K_RIGHT:
player_2.vx = 0
if event.type == pg.QUIT:
pg.quit()
exit()
#update all sprites
all_sprites.update()
check_for_collisions(player_1,main_platform)
check_for_collisions(player_2,main_platform)
check_blast_collisions(players,blasts)
#draw sprites
screen.fill((255,255,255))
all_sprites.draw(screen)
#draw other stuff
p1lives = font.render(str(player_1.lives), False, (0,0,255))
screen.blit(p1lives,(20,50))
p2lives = font.render(str(player_2.lives), False, (255,0,0))
screen.blit(p2lives,(1580,50))
clock.tick(FPS)
pg.display.flip()
Here is the entire code.
Any help is much appreciated. Thanks.

You cannot use pygame.sprite.groupcollide() here, because the bullets collide with the player that shoots the bullets.
You have to use pygame.sprite.spritecollide(), with one player and the bullets of the opponent. Call it once for each player.

Related

I am having trouble with my stat function in my player class

When I shoot at my cement block sprites I have it set so that self.score is self.score += 1 in my player collision function, but when I shoot my cement blocks and destroy them, either 1 or 2 points is added at random to my score. Why? How can I fix this? A clear example is, I shoot at and destroy 2 cement blocks in a row and 1 point is added for each one destroyed which means my score is 2, which is what I want cause I want to add 1 point to my score whenever I destroy a cement block, but then when I shoot and destroy the third cement block, 2 points are added instead of 1 bringing my score to 4 instead of being a score of 3 points.
Github: https://github.com/Enoc-Mena99/AutoPilot
My code:
import random
import pygame
import pygame.freetype
pygame.init()
#screen settings
WIDTH = 1000
HEIGHT = 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))
#fps
FPS = 120
clock = pygame.time.Clock()
#load images
bg = pygame.image.load('background/street.png').convert_alpha() # background
bullets = pygame.image.load('car/bullet.png').convert_alpha()
debris_img = pygame.image.load('debris/cement.png')
#define game variables
shoot = False
#player class
class Player(pygame.sprite.Sprite):
def __init__(self, scale, speed):
pygame.sprite.Sprite.__init__(self)
self.bullet = pygame.image.load('car/bullet.png').convert_alpha()
self.bullet_list = []
self.speed = speed
#self.x = x
#self.y = y
self.moving = True
self.frame = 0
self.flip = False
self.direction = 0
self.score = 0
#load car
self.images = []
img = pygame.image.load('car/car.png').convert_alpha()
img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.update_time = pygame.time.get_ticks()
self.movingLeft = False
self.movingRight = False
self.rect.x = 465
self.rect.y = 325
#draw car to screen
def draw(self):
screen.blit(self.image, (self.rect.centerx, self.rect.centery))
#move car
def move(self):
#reset the movement variables
dx = 0
dy = 0
#moving variables
if self.movingLeft and self.rect.x > 33:
dx -= self.speed
self.flip = True
self.direction = -1
if self.movingRight and self.rect.x < 900:
dx += self.speed
self.flip = False
self.direction = 1
#update rectangle position
self.rect.x += dx
self.rect.y += dy
#shoot
def shoot(self):
bullet = Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
bullet_group.add(bullet)
#check collision
def collision(self, debris_group):
for debris in debris_group:
if pygame.sprite.spritecollide(debris, bullet_group, True):
debris.health -= 1
if debris.health <= 0:
self.score += 1
#player stats
def stats(self):
myfont = pygame.font.SysFont('comicsans', 30)
scoretext = myfont.render("Score: " + str(self.score), 1, (0,0,0))
screen.blit(scoretext, (100,10))
#bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.speed = 5
self.image = bullets
self.rect = self.image.get_rect()
self.rect.center = (x,y)
self.direction = direction
def update(self):
self.rect.centery -= self.speed
#check if bullet has gone off screen
if self.rect.centery < 1:
self.kill()
#debris class
class Debris(pygame.sprite.Sprite):
def __init__(self,scale,speed):
pygame.sprite.Sprite.__init__(self)
self.scale = scale
self.x = random.randrange(100,800)
self.speed_y = 10
self.y = 15
self.speed = speed
self.vy = 0
self.on_ground = True
self.move = True
self.health = 4
self.max_health = self.health
self.alive = True
self.velocity = random.randrange(1,2)
self.speed_x = random.randrange(-3,3)
self.moving_down = True
self.is_destroyed = False
#load debris
self.image = debris_img
self.rect = self.image.get_rect()
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.rect.center = (self.x,self.y)
#load explosion
self.img_explosion_00 = pygame.image.load('explosion/0.png').convert_alpha()
self.img_explosion_00 = pygame.transform.scale(self.img_explosion_00, (self.img_explosion_00.get_width() * 2,
self.img_explosion_00.get_height() * 2))
self.img_explosion_01 = pygame.image.load('explosion/1.png').convert_alpha()
self.img_explosion_01 = pygame.transform.scale(self.img_explosion_01, (self.img_explosion_01.get_width() * 2,
self.img_explosion_01.get_height() * 2))
self.img_explosion_02 = pygame.image.load('explosion/2.png').convert_alpha()
self.img_explosion_02 = pygame.transform.scale(self.img_explosion_02, (self.img_explosion_02.get_width() * 2,
self.img_explosion_02.get_height() * 2))
self.img_explosion_03 = pygame.image.load('explosion/3.png').convert_alpha()
self.img_explosion_03 = pygame.transform.scale(self.img_explosion_03, (self.img_explosion_03.get_width() * 2,
self.img_explosion_03.get_height() * 2))
#explosion list
self.anim_explosion = [self.img_explosion_00,
self.img_explosion_01,
self.img_explosion_02,
self.img_explosion_03]
self.anim_index = 0
self.frame_len = 10
#spawn new debris
def spawn_new_debris(self):
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.velocity = random.randrange(1, 2)
self.speed_x = random.randrange(-3, 3)
#respawn debris when they go of the screen
def boundaries(self):
if self.rect.left > WIDTH + 10 or self.rect.right < -10 or self.rect.top > HEIGHT + 10:
self.spawn_new_debris()
#update image
def update(self):
self.rect.y += self.velocity
self.rect.x += self.speed_x
self.boundaries()
if self.health <= 0:
max_index = len(self.anim_explosion) - 1
if self.anim_index > max_index:
self.kill()
else:
if self.frame_len == 0:
self.image = self.anim_explosion[self.anim_index]
self.anim_index += 1
self.frame_len = 10
else:
self.frame_len -= 1
#make debris fall down
def falldown(self):
self.rect.centery += self.velocity
if self.moving_down and self.rect.y > 350:
self.kill()
######################CAR/DEBRIS##########################
player = Player(1,5)
##########################################################
#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
for x in range(50):
d = Debris(1, 5)
debris_group.add(d)
all_sprites.add(d)
#game runs here
run = True
while run:
#draw street
screen.blit(bg, [0, 0])
#update groups
bullet_group.update()
bullet_group.draw(screen)
debris_group.update()
debris_group.draw(screen)
#draw car
player.draw()
player.move()
player.collision(debris_group)
player.stats()
#update all sprites
all_sprites.update()
all_sprites.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#check if key is down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_a:
player.movingLeft = True
if event.key == pygame.K_d:
player.movingRight = True
if event.key == pygame.K_SPACE:
player.shoot()
shoot = True
#check if key is up
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.movingLeft = False
if event.key == pygame.K_d:
player.movingRight = False
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
Do not count debris objects with healt <= 0:
class Player(pygame.sprite.Sprite):
# [...]
def collision(self, debris_group):
for debris in debris_group:
# if health > 0 and collision:
if debris.health > 0 and pygame.sprite.spritecollide(debris, bullet_group, True):
debris.health -= 1
if debris.health <= 0:
self.score += 1

Cant move sprite on screen [duplicate]

This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
Closed 1 year ago.
The sprite is just stuck in the top left corner. It moves sligthly when pressing "wasd".
I am trying to make a simple clone of Mayhem/xPilot. A 2 player game where there are 2 rockets that can shoot eachother down.
I am fairly new to coding and python, I would be gratefull if you could ELI5.
Has anyone else had this problem?
import pygame as pg
import sys
x, y = 0, 0
class Rocket(pg.sprite.Sprite):
def __init__(self, picture_path, x, y):
super().__init__()
self.image = pg.image.load(picture_path)
self.image = pg.transform.scale(self.image, (120, 100))
self.rect = self.image.get_rect()
self.rect.x = 900
self.rect.y = 400
self.pressed_w = False
self.pressed_a = False
self.pressed_s = False
self.pressed_d = False
self.speed = 3
self.gravity = False
def update(self):
self.rect.x = 0
self.rect.y = 0
if self.pressed_a and self.rect.left > 0:
self.rect.x = self.speed
if self.pressed_d and self.rect.right < width:
self.rect.x = self.speed
if self.pressed_s and self.rect.bottom < height:
self.rect.y = self.speed
if self.pressed_w and self.rect.top > 0:
self.rect.y = -self.speed
if self.gravity and self.rect.bottom < height:
self.rect.y = self.speed
self.rect.x += x
self.rect.y += y
pg.init()
clock = pg.time.Clock()
width = 1920
height = 1080
screen = pg.display.set_mode((width, height))
background = pg.image.load("bg.jpg")
#rocket
player_rect = Rocket("rocket.png", x, y)
rocket_group = pg.sprite.Group()
rocket_group.add(player_rect)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
player_rect.pressed_w = True
player_rect.gravity = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_s:
player_rect.pressed_s = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player_rect.pressed_d = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
player_rect.pressed_a = True
if event.type == pg.KEYUP:
if event.key == pg.K_w:
player_rect.pressed_w = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_s:
player_rect.pressed_s = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_d:
player_rect.pressed_d = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_a:
player_rect.pressed_a = False
player_rect.gravity = True
pg.display.flip()
screen.blit(background, (0, 0))
rocket_group.draw(screen)
rocket_group.update()
clock.tick(120)
The problem is in the update method. The method has to change the coordinates of the rect attribute. However, your method continuously sets self.rect.x = 0 and self.rect.y = 0.
Change update:
class Rocket(pg.sprite.Sprite):
# [...]
def update(self):
if self.pressed_a:
self.rect.x -= self.speed
if self.pressed_d
self.rect.x += self.speed
if self.pressed_w
self.rect.y -= self.speed
if self.pressed_s
self.rect.y += self.speed
if self.gravity
self.rect.y += self.speed
if self.rect.left < 0: self.rect.left = 0
if self.rect.right > width: self.rect.right = width
if self.rect.top < 0: self.rect.top = 0
if self.rect.bottom > height: self.rect.bottom = height

Multiple sprite collision detections

Ok, so I have been trying to figure out how to set this up and can't get it working properly. I'm trying to get the functionality similar to the moving logs in the game Frogger if you are familiar with it. So far what I have works with the collision detection for only one pygame.sprite.Group()
import pygame
import os
import random
os.environ['SDL_VIDEO_CENTERED'] = '1'
WIDTH = 480
HEIGHT = 720
FPS = 60
# Moving Object spacing setup first row
INSTANCE_COUNT = 0
BOX_WIDTH = 30
SPACING = 120
# object speed
pos_objspeed = 1
neg_objspeed = -1
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 30))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 96
self.speedx = 0
self.speedy = 0
class MovingObj_Test(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((90, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = (INSTANCE_COUNT * (SPACING+ BOX_WIDTH))
#self.rect.x = random.randrange(WIDTH)
self.rect.bottom = 384
self.speedx = pos_objspeed
def update(self):
self.rect.x += self.speedx
if self.rect.x >= 480:
self.rect.x = -30
self.rect.bottom = 384
self.speedx = pos_objspeed
class MovingObj_TestTwo(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((90, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = (INSTANCE_COUNT * (SPACING+ BOX_WIDTH))
#self.rect.x = random.randrange(WIDTH)
self.rect.bottom = 336
self.speedx = neg_objspeed
def update(self):
self.rect.x += self.speedx
if self.rect.x >= 480:
self.rect.x = -30
self.rect.bottom = 336
self.speedx = neg_objspeed
all_sprites = pygame.sprite.Group()
movingobj_l = pygame.sprite.Group()
movingobj_r = pygame.sprite.Group()
for i in range (3):
INSTANCE_COUNT = i + 1
obj1 = MovingObj_Test()
all_sprites.add(obj1)
movingobj_l.add(obj1)
for i in range (3):
INSTANCE_COUNT = i + 1
obj2 = MovingObj_TestTwo()
all_sprites.add(obj2)
movingobj_r.add(obj2)
player = Player()
all_sprites.add(player)
running = True
onLogLeft = False
onLogRight = False
groundSpd = 48
while running:
clock.tick(FPS)
hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)
for hit in hitsleft:
player.speedx = pos_objspeed
player.speedy = 0
onLogLeft = True
if len(hitsleft) == 0:
onLogLeft = False
player.speedx = 0
player.speedy = 0
hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)
for hit in hitsright:
player.speedx = neg_objspeed
player.speedy = 0
onLogRight = True
if len(hitsright) == 0:
onLogRight = False
player.speedx = 0
player.speedy = 0
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speedx = -groundSpd
if event.key == pygame.K_RIGHT:
player.speedx = groundSpd
if event.key == pygame.K_UP:
player.speedy = -groundSpd
if event.key == pygame.K_DOWN:
player.speedy = groundSpd
if event.type == pygame.KEYUP and onLogLeft == False and onLogRight == False:
if event.key == pygame.K_LEFT:
player.speedx = 0
if event.key == pygame.K_RIGHT:
player.speedx = 0
if event.key == pygame.K_UP:
player.speedy = 0
if event.key == pygame.K_DOWN:
player.speedy = 0
if onLogLeft == False and onLogRight == False and player.rect.bottom <= 384:
running = False
player.rect.x += player.speedx
player.rect.y += player.speedy
if event.type == pygame.QUIT:
running = False
#player.rect.x += player.speedx
#player.rect.y += player.speedy
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
quit()
Ok so in my code the
hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)
checks the collision and updates the players speed how I'm expecting it to but, the
hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)
is not doing the same.
I'm confused as to why this is if anyone has any ideas. Also how could I fix it to work for both?
The code for left and right behaves different, because if a collision with hitsleft is detected, the player.speedx is set:
for hit in hitsleft:
player.speedx = pos_objspeed
But if there is a collision with hitsleft there it no collision with hitsright and player.speedx is reset immediately:
if len(hitsright) == 0:
onLogRight = False
player.speedx = 0
You have to set player.speedx dependent on both conditions (e.g. in a if - elif - else statement):
while running:
# [...]
hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)
onLogLeft = any(hitsleft)
hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)
onLogRight = any(hitsright)
player.speedy = 0
if onLogLeft:
player.speedx = pos_objspeed
elif onLogRight:
player.speedx = neg_objspeed
else:
player.speedx = 0

Pygame velocity and collision trouble

i am currentely making my final project for my exam and i'm making a platform game. Actually i'm made the collisions and it works. But the actual problem is that if i jump on a platform and i keep my player static for 1 second the player disappears which is weird. Could you help me to solve this issue? I have a hint that this issue is related to the velocity (calles vitesse_x and vitesse_y).
import pygame
from pygame.locals import *
pygame.init()
fenetre = pygame.display.set_mode((1024,768))
pygame.display.set_caption("Portal Escape")
class Perso(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50,50))
self.rect = self.image.get_rect(bottomleft=(0,740))
self.image.fill((255, 0, 0))
self.doit_monter = False
self.vitesse_x = 0
self.vitesse_y = 0
def update(self):
self.rect.x += self.vitesse_x
self.rect.y += self.vitesse_y
touche = pygame.key.get_pressed()
if touche[pygame.K_RIGHT] and self.rect.x < 920:
self.vitesse_x += 1
if touche[pygame.K_LEFT] and self.rect.x > 0:
self.vitesse_x -= 1
collision = pygame.sprite.spritecollide(self, blocs, False)
for bloc in collision:
if self.vitesse_y >= 0:
self.rect.bottom = bloc.rect.top
elif self.vitesse_y < 0:
self.rect.top = bloc.rect.bottom
perso = Perso()
class Bloc(pygame.sprite.Sprite): #pour creer un obstacle et éléments du jeu
def __init__(self, x, y, w, h):
super().__init__()
self.image = pygame.Surface((w,h))
self.image.fill((0, 255, 0))
self.rect = self.image.get_rect(topleft=(x, y))
all_sprites = pygame.sprite.Group()
blocs = pygame.sprite.Group()
all_sprites.add(perso)
b1 = Bloc(200,500,250,50)
b2 = Bloc(500,600,200,50)
b3 = Bloc(0,740,1024,30)
blocs.add(b1,b2,b3)
all_sprites.add(b1,b2,b3)
new_y = 0
continuer = True
while continuer:
pygame.time.Clock().tick(60)
for event in pygame.event.get():
if event.type == QUIT:
continuer = False
if event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
perso.doit_monter = True
new_y = perso.rect.y - 100
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and perso.vitesse_x < 0:
perso.vitesse_x = 0
perso.vitesse_y = 0
if event.key == pygame.K_RIGHT and perso.vitesse_x > 0:
perso.vitesse_x = 0
perso.vitesse_y = 0
if event.key == pygame.K_SPACE and perso.vitesse_y < 0:
perso.vitesse_y = 0
if perso.doit_monter == True:
if perso.rect.y > new_y and perso.rect.y > 0:
perso.vitesse_y -= 5
else:
perso.doit_monter = False
else:
perso.vitesse_y += 1
all_sprites.update()
fenetre.fill((0,0,0))
all_sprites.draw(fenetre)
pygame.display.flip()
pygame.quit()
Ok simple, problem, simple solution. You have to reset the y velocity if you are on the ground.
In Perso.update:
for bloc in collision:
if self.vitesse_y >= 0:
self.vitesse_y = 0 # reset velocity, so it isn't increasing forever
self.rect.bottom = bloc.rect.top
elif self.vitesse_y < 0:
self.rect.top = bloc.rect.bottom
If you don't do that, the velocity will increase, till it is high enough to jump throug the floor in one go without colliding with stopping box

Pygame: Timer For Time Alive

Hello I am currently making a game in python and I am trying to make a timer which I have never attempted before, hence asking this question. What I really need to know is how to loop this small area where it says #Timer. Any help will be appreciated, thank you.
import pygame, time
import pygame.mixer
from bullet import Bullet
from constants import DIR_LEFT, DIR_RIGHT
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, gravity):
pygame.mixer.init()
# Player dimensions and position
# Player image and animation
self.images = []
self.images.append(pygame.image.load('images/Sprites/player.png'))
self.images.append(pygame.image.load('images/Sprites/player2.png'))
#~ self.images.append(pygame.image.load('ball1.png'))
#~ self.images.append(pygame.image.load('ball2.png'))
self.maxImage = len(self.images)
self.currentImage = 0
self.jumpSound = pygame.mixer.Sound('sounds/jump.ogg')
self.shootSound = pygame.mixer.Sound('sounds/laser.ogg')
#~ self.rect = pygame.Rect(x, y, 80, 80)
self.rect = self.images[0].get_rect()
self.rect.x = x
self.rect.y = y
self.timeTarget = 10
self.timeNum = 0
self.velX = 0
self.velY = 0
self.health = 200
self.score = 0
self.alivetime = 0
self.second = 1000
self.direction = DIR_RIGHT
# Jump and gravity
self.jumping = False
self.on_ground = False
self.origJumpVel = 15
self.jumpVel = self.origJumpVel
self.gravity = 0.5
# Jump inputs
def do_jump(self):
if self.jumping and not self.on_ground:
self.velY = -self.jumpVel
self.jumpVel -= self.gravity
if self.on_ground:
self.jumping = False
self.jumpVel = self.origJumpVel
self.velY = 0
self.on_ground = True
def handle_events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.jumpSound.play(0)
if not self.jumping:
self.jumping = True
self.on_ground = False
if event.key == pygame.K_s:
self.shootSound.play(0)
elif event.key == pygame.K_a:
#pygame.transform.flip(self.images[self.currentImage], False, False)
self.velX = -5
elif event.key == pygame.K_d:
#pygame.transform.flip(self.images[self.currentImage], True, False)
self.velX = +5
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_a, pygame.K_d):
self.velX = 0
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
bullet.velX = 0
# PLayer updates
def update(self):
#self.timeNum += 1
# Animations
#if self.direction == DIR_LEFT:
#if self.timeNum == self.timeTarget:
#self.currentImage += 1
#if self.currentImage >= self.maxImage:
#self.currentImage = 0
#self.timeNum = 0
# Timer
if pygame.time.get_ticks() >= self.second:
self.alivetime = +1
pygame.time.get_ticks() == 0
# Screen wrap
if self.rect.right > 1280:
self.rect.left = 0
elif self.rect.left < 0:
self.rect.right = 1280
if self.velX < 0 and self.direction != DIR_RIGHT: # Moving right
self.direction = DIR_RIGHT
self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
elif self.velX > 0 and self.direction != DIR_LEFT: # Moving left
self.direction = DIR_LEFT
self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
# Player rendering
def render(self, surface):
surface.blit(self.images[self.currentImage], self.rect)
Run player.update() in mainloop in every loop and Timer will loop too.
By The Way:
if you try to set tick to zero in this
pygame.time.get_ticks() == 0
than you are wrong. You can't change number of ticks.
Use this
# __init__()
self.time_to_change_alivetime = pygame.time.get_ticks() + self.second
# update()
if pygame.time.get_ticks() >= self.time_to_change_alivetime:
self.alivetime += 1
self.time_to_change_alivetime += self.second
or this (without if)
# __init__()
self.start_alivetime = pygame.time.get_ticks()
# update()
self.alivetime = (pygame.time.get_ticks() - self.start_alivetime) / self.second
or more precisely (1 minute = 60 seconds = 60 000 milliseconds)
# __init__()
self.start_alivetime = pygame.time.get_ticks()
# update()
milliseconds = pygame.time.get_ticks() - self.start_alivetime
self.alivetime_minutes = milliseconds / 60000
self.alivetime_seconds = (milliseconds % 60000) / self.second
self.alivetime_milliseconds = milliseconds % self.second

Categories