Unable to close pygame window when try again button clicked - python

I am making a spaceship game where you control a spaceship and fire bullets to destroy enemy spaceships. When you click the try again button when you lose, you should be able to both replay and close the game. However, even though I was able to play multiply times, I wasn't able to close the window. I think this has something to do with the if statement that checks if the try again button is clicked. Or maybe it has something to do with something else. How can I fix it so I can close the window at all times?
This is my current code:
import pygame
from pygame.locals import *
from random import randint, choice
from tools import *
pygame.init()
pygame.font.init()
SCREEN_X = 800
SCREEN_Y = 500
CENTER_POS = (400, 225)
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
class Spaceship(pygame.sprite.Sprite):
"""A spaceship object. Used for the player."""
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x, self.y = x, y
self.image = pygame.image.load("spaceship.png")
self.image = pygame.transform.scale(self.image, (175, 175))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
"""Updates the spaceship rect"""
self.rect.center = (self.x, self.y)
class Bullet(pygame.sprite.Sprite):
"""A bullet object. Appears when the player clicks."""
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x, self.y = x, y
self.image = pygame.image.load("bullet.png")
self.image = pygame.transform.scale(self.image, (100, 100))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
"""Let's the bullet move upward."""
self.y -= 5
self.rect.center = (self.x, self.y)
if self.y < 0:
self.kill()
class Enemy(pygame.sprite.Sprite):
"""An enemy object. The player's job is to destroy enemies."""
def __init__(self, s, x, y, t):
pygame.sprite.Sprite.__init__(self)
self.type = t
self.screen, self.x, self.y = s, x, y
self.image = pygame.image.load(get_enemy_image()[self.type])
# There is an if statement because the
# N1 Galaxy Fighter and M7 Comet Glider need different sizes
if self.type == "N1 Galaxy Fighter":
self.image = pygame.transform.scale(self.image, (235, 215))
elif self.type == "M7 Comet Glider":
self.image = pygame.transform.scale(self.image, (155, 215))
self.rect = self.image.get_rect()
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
self.score_given = get_enemy_given_score()[self.type]
def update(self):
if self.y < 0:
self.kill()
self.y += 3
self.rect.center = (self.x, self.y)
class GameOverBackground(pygame.sprite.Sprite):
"""The game over background object."""
def __init__(self, s, x, y, size=(100, 100)):
pygame.sprite.Sprite.__init__(self)
self.screen, self.x, self.y = s, x, y
self.size = size
self.image = pygame.image.load("Game_Over.jpg")
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect()
def blitme(self):
"""Blits the game over image on the screen"""
self.screen.blit(self.image, self.rect)
class Coin(pygame.sprite.Sprite):
"""A coin object."""
def __init__(self, pos=(0, 0), size=(100, 100)):
pygame.sprite.Sprite.__init__(self)
self.x, self.y = pos[0], pos[1]
self.size = size
self.image = pygame.image.load("coin.png")
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
"""Updates the coin rect"""
self.rect.center = (self.x, self.y)
class StartButton(pygame.sprite.Sprite):
def __init__(self, s, x, y, size=None):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x = x
self.y = y
self.image = pygame.image.load("start_button.png")
if size is not None:
self.image = pygame.transform.scale(self.image, size)
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def blitme(self):
self.screen.blit(self.image, self.rect)
class TryAgainButton(pygame.sprite.Sprite):
def __init__(self, s, x, y, size=None):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x = x
self.y = y
self.image = pygame.image.load("try_again.png")
if size is not None:
self.image = pygame.transform.scale(self.image, size)
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def blitme(self):
self.screen.blit(self.image, self.rect)
def main():
bg = GameOverBackground(screen, 0, 0, size=(800, 500))
spaceship = Spaceship(screen, 400, 400)
start_button = StartButton(screen, CENTER_POS[0], CENTER_POS[1], size=(300, 195))
button_rect = start_button.image.get_rect(topleft=(start_button.x, start_button.y))
game_started = False
try_again_button = TryAgainButton(screen, CENTER_POS[0], CENTER_POS[1]+215, size=(300, 195))
button_rect_2 = try_again_button.image.get_rect(topleft=(try_again_button.x, try_again_button.y))
try_again = False
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
coins = pygame.sprite.Group()
clock = pygame.time.Clock()
enemy_interval = 2000
enemy_event = pygame.USEREVENT + 1
pygame.time.set_timer(enemy_event, enemy_interval)
coin_interval = 3500
coin_event = pygame.USEREVENT + 1
pygame.time.set_timer(coin_event, coin_interval)
score = 0
lives = 3
with open("high_score.txt", "r") as file:
highscore = file.read()
font = pygame.font.SysFont("Arial", 30)
score_text_surface = font.render("Score: {:,}".format(score), True, (0, 0, 0))
lives_text_surface = font.render("HP: %s" % lives, True, (0, 0, 0))
high_score_text_surface = font.render("High Score: %s" % highscore, True, (0, 0, 0))
spaceship_collided = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
if not game_started:
if button_rect.collidepoint(event.pos):
game_started = True
if game_started is True and button_rect_2.collidepoint(event.pos):
try_again = True
if game_started and event.type == MOUSEBUTTONDOWN:
bullet = Bullet(screen, spaceship.x, spaceship.y - 20)
bullets.add(bullet)
if game_started and event.type == enemy_event and not lives <= 0:
enemy = Enemy(screen, randint(-100, 725), 0, choice(["N1 Galaxy Fighter", "M7 Comet Glider"]))
enemies.add(enemy)
if game_started and event.type == coin_event and not lives <= 0:
if len(coins) < 100:
coins.add(Coin((randint(-125, 750), randint(-200, 400))))
screen.fill((255, 255, 255)) # DO NOT DRAW ANYTHING IN FRONT OF THIS LINE, I'M WARNING YOU
if not game_started:
start_button.blitme()
if try_again:
main()
if game_started:
bullets.update()
key = pygame.key.get_pressed()
amount = 5
if key[pygame.K_a]:
spaceship.x -= amount
elif key[pygame.K_d]:
spaceship.x += amount
elif key[pygame.K_w]:
spaceship.y -= amount
elif key[pygame.K_s]:
spaceship.y += amount
spaceship.update()
if not lives <= 0:
screen.blit(spaceship.image, spaceship.rect)
if not lives <= 0:
bullets.draw(screen)
enemies.draw(screen)
coins.update()
coins.draw(screen)
for i in enemies:
i.update()
if pygame.sprite.spritecollide(i, bullets, True):
score += i.score_given
i.kill()
if spaceship_collided and lives <= 0:
bg.blitme()
if score > int(highscore):
with open("high_score.txt", "w") as file:
file.write(str(score))
if score >= 99999:
score = 99999
if not lives <= 0:
score_text_surface = font.render("Score: {:,}".format(score), True, (0, 0, 0))
screen.blit(score_text_surface, (590, 0))
lives_text_surface = font.render("HP: %s" % lives, True, (0, 0, 0))
screen.blit(lives_text_surface, (260, 0))
high_score_text_surface = font.render("High Score: %s" % highscore, True, (0, 0, 0))
screen.blit(high_score_text_surface, (360, 0))
if pygame.sprite.spritecollide(spaceship, enemies, dokill=True):
lives -= 1
spaceship_collided = True
if pygame.sprite.spritecollide(spaceship, coins, dokill=True):
score += 10
if lives <= 0:
try_again_button.blitme()
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()

You can't do it like that. Never run the main application loop recursively. The issue is
if try_again:
main()
What you are actually doing is starting a new game within the game in progress. If you want to close the game, you will need to close all games that you have started before. Hence, you have to press close several times.
Instead of calling main recursively, you either have to reset all game states or call main in a loop.
In your particular situation, the later solution is easier to implement. Remove the recursive call but set running = False, return try_again from main and call main while it returns True:
def main():
# [...]
try_again = False
running = True
while running:
# [...]
if try_again:
# main() <---- DELETE
running = False
# [...]
return try_again
if __name__ == "__main__":
keep_running = True
while keep_running:
keep_running = main()

Related

How to delete a sprite at collision in a TDS game pygame

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.

Pygame: player sprite colliding incorrectly and clipping into wall

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

Pygame problem : my player image don't move with scroll

Firts I am french so excuse-me for my english. I have recently discoverd pygame and I love that so decided to create a rpg on pygame and... I have a little problem : with many, many difficulty I made a scrolling system on my pygame's rpg I evn do a system how make sure player's image gets back when he go up, puts himself in profile ...
BUT (a big but) my player's image don't move, the scrolling, move and the player's rect also, I don't know I can solve that so please help me !!!!! The main.py file :
from game import Game
from level import *
pygame.init()
lvl = Level()
screen = pygame.display.set_mode((1024, 768))
pygame.display.set_caption("RPG")
game = Game()
cam = Camera(1024, 768)
running = True
lvl.generer()
print(game.player.rect)
clock = pygame.time.Clock()
while running:
cam.update(game.player)
#print(cam.rect.topleft)
if game.pressed.get(pygame.K_RIGHT) and game.player.rect.x + game.player.rect.width < 1080:
game.player.move_right()
lvl.afficher(screen, 0, 0, cam.x, cam.y)
#print(game.player.rect.x)
elif game.pressed.get(pygame.K_LEFT) and game.player.rect.x > 0:
game.player.move_left()
lvl.afficher(screen, 0, 0, cam.x, cam.y)
#print(game.player.rect.x)
elif game.pressed.get(pygame.K_DOWN):
game.player.move_down()
lvl.afficher(screen, 0, 0, cam.x, cam.y)
#screen.scroll(0, -game.player.velocity)
#print(game.player.rect.y)
elif game.pressed.get(pygame.K_UP):
game.player.move_up()
lvl.afficher(screen, 0, 0, cam.x, cam.y)
#screen.scroll(0, game.player.velocity)
#print(game.player.rect.y)
#print(cam.rect.x, cam.rect.y)
#print(cam.rect)
screen.blit(game.player.image, game.player.rect)
print(game.player.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.KEYDOWN:
game.pressed[event.key] = True
elif event.type == pygame.KEYUP:
game.pressed[event.key] = False```
the player.py class :
```import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, game):
super(Player, self).__init__()
self.game = game
self.health = 100
self.maxHealth = 100
self.attack = 10
self.velocity = 10
self.dimension = (64, 88)
self.image = pygame.image.load("assets/player/player_face/player_face.png")
self.original_image = pygame.image.load("assets/player/player_face/player_face.png")
self.image = pygame.transform.scale(self.image, self.dimension)
self.rect = self.image.get_rect()
self.rect.x = 512
self.rect.y = 384
self.face = [pygame.image.load("assets/player/player_face/player/sprite_00.png"), pygame.image.load("assets/player/player_face/player/sprite_01.png"), pygame.image.load("assets/player/player_face/player/sprite_02.png"), pygame.image.load("assets/player/player_face/player/sprite_03.png"), pygame.image.load("assets/player/player_face/player/sprite_04.png"), pygame.image.load("assets/player/player_face/player/sprite_05.png"), pygame.image.load("assets/player/player_face/player/sprite_06.png"), pygame.image.load("assets/player/player_face/player/sprite_07.png"), pygame.image.load("assets/player/player_face/player/sprite_08.png"), pygame.image.load("assets/player/player_face/player/sprite_09.png")]
def move_right(self):
self.rect.x += self.velocity
self.image = pygame.image.load("assets/player/player_profileLeft/player_profile_left.png")
self.image = pygame.transform.scale(self.image, self.dimension)
def move_left(self):
self.rect.x -= self.velocity
self.image = pygame.image.load("assets/player/player_profileRight/player_profile_right.png")
self.image = pygame.transform.scale(self.image, self.dimension)
def move_down(self):
self.rect.y += self.velocity
#self.image = pygame.image.load("assets/player/player_face/player_face.png")
#self.image = pygame.transform.scale(self.image, self.dimension)
i = 0
for elt in self.face:
self.image = self.face[i]
print("lol")
self.image = pygame.transform.scale(self.image, self.dimension)
i += 1
def move_up(self):
self.rect.y -= self.velocity
self.image = pygame.image.load("assets/player/player_back/player_dos.png")
self.image = pygame.transform.scale(self.image, self.dimension)
the level.py class :
from pygame.locals import *
from player import *
class Camera:
def __init__(self, widht, height):
self.rect = pygame.Rect(0, 0, widht, height)
self.widht = widht
self.height = height
self.topleft = list(self.rect.topleft)
self.x = self.topleft[0]
self.y = self.topleft[1]
def update(self, target):
self.rect.y = -target.rect.y + int(769 / 2)
self.rect.x = -target.rect.x + int(1024 / 2)
self.topleft = list(self.rect.topleft)
self.x = self.topleft[0]
self.y = self.topleft[1]
self.rect = pygame.Rect(self.rect.x, self.rect.y, self.widht, self.height)
#self.rect.topleft_x = self.rect.topleft(0)
#self.coo = (0, 0)
class Level:
def __init__(self):
self.structure = 0
def generer(self):
with open("niveau.txt", "r") as fichier:
structure_niveau = []
for ligne in fichier:
ligne_niveau = []
for sprite in ligne:
if sprite != '\n':
ligne_niveau.append(sprite)
structure_niveau.append(ligne_niveau)
self.structure = structure_niveau
def afficher(self, fenetre, x, y, camLeftX, camLeftY):
tailleSprite = 64
#Camera.__init__(self, x, y)
grass = pygame.image.load("assets/bloc/grass.png").convert_alpha()
tree = pygame.image.load("assets/bloc/tree_grass.png").convert_alpha()
no_texture = pygame.image.load("assets/bloc/no_texture.png")
num_ligne = 0
for ligne in self.structure:
num_case = 0
for sprite in ligne:
x = num_case * tailleSprite - camLeftX
y = num_ligne * tailleSprite - camLeftY
if sprite == 'G':
fenetre.blit(grass, (x, y))
elif sprite == 'T':
fenetre.blit(tree, (x, y))
#print(self.x, self.y)
else:
fenetre.blit(no_texture, (x, y))
num_case += 1
num_ligne += 1```
Ok I just found the solution but It's making an other problem I'll ask the question on a other post. At x = num_case * tailleSprite - camLeftX and y = num_ligne * tailleSprite - camLeftY on level.py just replace the - by a +

How do I properly implement collision response in Pygame?

I'm trying to program a simple top-down dungeon crawler in Pygame and I've already hit a roadblock in designing my collision response. So far I've programmed player movement, collision detection (only knows when a player hits a wall) and player-to-mouse rotation, the latter of which is disabled to simplify this solution.
I've tried the normal method of moving the character, checking for collision, changing the character's position if needed and then drawing all elements to the screen.
import pygame
from pygame.locals import *
import os
import sys
import math
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
pygame.font.init()
clock = pygame.time.Clock()
myfont = pygame.font.SysFont('sourcecodeproblack', 12)
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
WALLS = [220, 40, 30]
SIZE = [240, 240]
DSIZE = [480, 480]
TSIZE = [720, 720]
dub = False
trip = False
def normal():
global screen
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("METAL PACKET")
def full():
global screen
screen = pygame.display.set_mode((SIZE), pygame.FULLSCREEN)
pygame.display.set_caption("METAL PACKET: Fullscreen Edition")
def doubled():
global screen
global window
global res
global invscale
invscale = 1/2
res = DSIZE
window = pygame.display.set_mode(DSIZE)
screen = pygame.Surface(SIZE)
pygame.display.set_caption("METAL PACKET: Double Boogaloo")
def doubledfull():
global screen
global window
global res
global invscale
invscale = 1/2
res = DSIZE
window = pygame.display.set_mode((DSIZE), pygame.FULLSCREEN)
screen = pygame.Surface(SIZE)
pygame.display.set_caption("METAL PACKET: Fullscreen Boogaloo")
def tripled ():
global screen
global window
global res
global invscale
invscale = 1/3
res = TSIZE
window = pygame.display.set_mode(TSIZE)
screen = pygame.Surface(SIZE)
pygame.display.set_caption("METAL PACKET: Trifecta of Resolution")
def tripledfull ():
global screen
global window
global res
global invscale
invscale = 1/3
res = TSIZE
window = pygame.display.set_mode((TSIZE), pygame.FULLSCREEN)
screen = pygame.Surface(SIZE)
pygame.display.set_caption("METAL PACKET: Fullscreen Trifecta Edition")
print("")
print("1: 240 x 240")
print("2: 480 x 480")
print("3: 720 x 720")
print("4: 240 x 240 Fullscreen")
print("5: 480 x 480 Fullscreen")
print("6: 720 x 720 Fullscreen")
print("")
res = input("Choose a video mode. ")
if res == ("1"):
normal()
if res == ("2"):
doubled()
dub = True
if res == ("3"):
tripled()
trip = True
if res == ("4"):
full()
if res == ("5"):
doubledfull()
dub = True
if res == ("6"):
tripledfull()
trip = True
class Walls(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.currentsprite = ()
def draw(self):
self.image = pygame.image.load(self.currentsprite)
screen.blit(self.image, [0, 0])
room1 = Walls()
room1.currentsprite = ("room1.png")
collidelist = pygame.sprite.Group()
class WallColliders(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self, collidelist)
self.rectval = []
def collide(self):
self.rect = (self.rectval)
topwall = WallColliders()
topwall.rectval = [0, 0, 240, 20]
bottomwall = WallColliders()
bottomwall.rectval = [0, 160, 240, 20]
leftwall = WallColliders()
leftwall.rectval = [0, 0, 20, 180]
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.collide = 0
self.x = 0
self.y = 0
self.speed = 0
self.currentsprite = ("pac.png")
self.image = pygame.image.load(self.currentsprite)
self.rect = self.image.get_rect()
def collidedetect(self):
if pygame.sprite.spritecollideany(player, collidelist):
self.collide = 1
else:
self.collide = 0
def move(self):
key = pygame.key.get_pressed()
if key[K_LEFT] or key[ord("a")]:
self.x -= self.speed
if self.collide == 1:
self.x += self.speed
self.collide = 0
if key[K_RIGHT] or key[ord("d")]:
self.x += self.speed
if self.collide == 1:
self.x -= self.speed
self.collide = 0
if key[K_UP] or key[ord("w")]:
self.y -= self.speed
if self.collide == 1:
self.y += self.speed
self.collide = 0
if key[K_DOWN] or key[ord("s")]:
self.y += self.speed
if self.collide == 1:
self.y -= self.speed
self.collide = 0
if self.collide == 1:
print("collide")
def rotate(self):
pos = pygame.mouse.get_pos()
if dub == True or trip == True:
pos = pos[0] * invscale, pos[1] * invscale
mouse_x, mouse_y = pos
mouse_x, mouse_y = pos
rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
self.rotimage = pygame.transform.rotozoom(self.image, self.angle, 1)
self.rect = self.rotimage.get_rect(center=(self.rect.center))
def monitor(self):
textsurface = myfont.render(str(self.angle), False, (255, 255, 255))
screen.blit(textsurface,(50,210))
def draw(self):
#screen.blit(self.rotimage, [self.x, self.y])
screen.blit(self.image, [self.x, self.y])
self.rect = pygame.Rect(self.x, self.y, 20, 20)
player = Player()
player.x = 29
player.y = 89
player.speed = 3
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print("")
print("See you next time.")
pygame.quit()
sys.exit(0)
done = True
screen.fill(BLACK)
room1.draw()
for x in collidelist:
x.collide()
player.collidedetect()
player.move()
#player.rotate()
#player.monitor()
player.draw()
if dub == True or trip == True:
pygame.transform.scale(screen, res, window)
pygame.display.flip()
clock.tick(60)
pygame.quit()
For some reason this will stick the player in the wall unless they press move diagonally instead of moving the player out of the wall and resetting collision. I would greatly appreciate it if someone could read through my code and suggest any improvement, optimisation or solution. Any example code would be great. Thanks.
Don't overcomplicate. When the player is moved, the calculate the new position and update the .rect property of the Player object:
class Player(pygame.sprite.Sprite):
# [...]
def move(self):
key = pygame.key.get_pressed()
if key[K_LEFT] or key[ord("a")]:
self.x -= self.speed
if key[K_RIGHT] or key[ord("d")]:
self.x += self.speed
if key[K_UP] or key[ord("w")]:
self.y -= self.speed
if key[K_DOWN] or key[ord("s")]:
self.y += self.speed
self.updaterect()
def updaterect(self):
self.rect = pygame.Rect(self.x, self.y, 20, 20)
In the main loop:
backup the player position
move the player
check for collision
if there is a collision, then restore the position of the player
while not done:
# [...]
pos = (player.x, player.y)
player.move()
player.collidedetect()
if player.collide:
player.x, player.y = pos
player.updaterect()
player.collide = False
player.draw()
# [...]
Of course this can be done in a method, too:
class Player(pygame.sprite.Sprite):
# [...]
def moveandcollide(self):
pos = (self.x, self.y)
self.move()
self.collidedetect()
if player.collide:
self.x, self.y = pos
self.updaterect()
self.collide = False
while not done:
# [...]
player.moveandcollide()

How can I detect collision between two moving sprites?

I am trying to make it so when my player sprite collides with an enemy, the player is killed. However, my player only dies if the enemy happens to spawn at the same point as the player and not when the player moves about and touches another sprite on purpose.
Here is my code:
import math
from random import randint
import pygame
from pygame.math import Vector2
pygame.init()
screen = pygame.display.set_mode((600, 600))
screen_width = 600
screen_height = 600
black = (0, 0, 0)
pygame.display.set_caption("gang")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(self.image, pygame.Color('steelblue2'), [(0, 0), (50, 15), (0, 30)])
self.original_image = self.image
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.velocityx = Vector2(6, 0)
self.velocityy = Vector2(0, 6)
def update(self):
self.rect.center = (int(self.pos.x), int(self.pos.y))
clamp_rect = self.rect.clamp(screen.get_rect())
if clamp_rect != self.rect:
self.rect = clamp_rect
self.pos.x, self.pos.y = self.rect.center
self.rotate()
def rotate(self):
direction = pygame.mouse.get_pos() - self.pos
radius, angle = direction.as_polar()
self.image = pygame.transform.rotate(self.original_image, -angle)
self.rect = self.image.get_rect(center=self.rect.center)
class Enemy(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill((randint(0, 255), randint(0, 255), randint(0, 255)))
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.velocity = Vector2(1, 0)
def update(self):
self.pos += self.velocity
self.rect.center = self.pos
class Projectile(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((9, 15), pygame.SRCALPHA)
pygame.draw.circle(self.image, (0, 0, 0), (30, 50), 14)
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.velocityx = Vector2(9, 0)
self.velocityy = Vector2(0, 9)
def update(self):
self.pos += self.velocityx
self.rect.center = self.pos
def draw_window():
screen.fill((30, 30, 30))
all_sprites.update()
all_sprites.draw(screen)
bullets.update()
bullets.draw(screen)
pygame.display.update()
def xor(a, b):
if bool(a) != bool(b):
return True
else:
return False
def game_end():
pygame.font.init()
text = pygame.font.Font('freesansbold.ttf', 32)
text_surface = text.render('Game Over', False, (255, 255, 255))
text_rect = text_surface.get_rect()
text_rect.center = (86, 86)
screen.blit(text_surface, (172, 172))
pygame.display.update()
player = Player((300, 300))
projectile = Projectile((150, 150))
bullets = pygame.sprite.Group(projectile)
enemies_list = pygame.sprite.Group()
all_sprites = pygame.sprite.Group(player, projectile)
def collision():
score = 0
for i in range(5):
enemy = Enemy((randint(0, 600), randint(0, 600)))
enemies_list.add(enemy)
all_sprites.add(enemy)
collide_list = pygame.sprite.spritecollide(player, enemies_list, True)
for enemy in collide_list:
all_sprites.remove(player)
collision()
def main():
screen_rect = screen.get_rect()
run = True
while run:
clock.tick(60)
keys = pygame.key.get_pressed()
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
if keys[pygame.K_SPACE]:
if len(bullets) < 5:
projectile.pos += projectile.velocityx
if xor(keys[pygame.K_a], keys[pygame.K_LEFT]):
player.pos -= player.velocityx
elif xor(keys[pygame.K_d], keys[pygame.K_RIGHT]):
player.pos += player.velocityx
elif xor(keys[pygame.K_w], keys[pygame.K_UP]):
player.pos -= player.velocityy
elif xor(keys[pygame.K_s], keys[pygame.K_DOWN]):
player.pos += player.velocityy
player.rect.clamp_ip(screen_rect)
draw_window()
main()
How can I make it so that a collision is detected and hence my player is removed from the game whilst the two sprites are moving?
You have to check if the player collides with an enemy in the main loop:
e.g.
run = True
hit_count = 0
while run:
# [...]
player.rect.clamp_ip(screen_rect)
collidelist = pygame.sprite.spritecollide(player, enemies_list, False)
if collidelist:
hit_count += 1
print("hit", hit_count)
draw_window()
If you want to fins initial enemy positions, which are not "on" the player, you've to check for collision of a new enemy and the player before you add it to enemies_list respectively all_sprites:
e.g.
def collision():
score = 0
while len(enemies_list) < 5:#
enemy = Enemy((randint(0, 600), randint(0, 600)))
if not pygame.sprite.collide_rect(enemy, player):
enemies_list.add(enemy)
all_sprites.add(enemy)

Categories

Resources