I'm following a begginner tutorial to start using pygame, but I'm stuck with the sprite display. I'm not able to make it visible on screen, please find below the code related to the sprite.
I'm not able to find where I mess up, do you have any idea ?
My Sprite is "Projectile" and is defined in Projectile Class, it is sent by the player, so I added a "launch_projectile" method, and in main it should be displaying the projectiles
Projectile class
import pygame
# Définir la classe projectile
class Projectile(pygame.sprite.Sprite):
# Définir le constructeur de la classe
def __init__(self, player):
super().__init__()
self.velocity = 5
self.image = pygame.image.load('Assets\Blast.png')
#self.image = pygame.transform.scale(self.image, (50, 50))
self.rect = self.image.get_rect()
self.rect.x = player.rect.x + 100
self.rect.y = player.rect.y
Player class
import pygame
from projectile import Projectile
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.health = 100
self.max_health = 100
self.attack = 10
self.velocity = 1
self.all_projectiles = pygame.sprite.Group()
self.image = pygame.image.load('Assets\Flying_Raccoon.png')
self.image = pygame.transform.scale(self.image, (100, 100))
self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = 0
# Créer un nouveau Projectile
def launch_projectile(self):
self.all_projectiles.add(Projectile(self))
### Méthodes de déplacement (X / Y) ###
def move_right(self):
self.rect.x += self.velocity
def move_left(self):
self.rect.x -= self.velocity
def move_up(self):
self.rect.y -= self.velocity
def move_down(self):
self.rect.y += self.velocity
Game
import pygame
from player import Player
class Game:
def __init__(self):
#Générer le joueur à la création du jeu
self.player = Player()
self.pressed = {}
Main
import pygame
from game import Game
pygame.init()
pygame.display.set_caption("Pilot Raccoon")
screen = pygame.display.set_mode((1080,720))
#Charger arrière plan
background = pygame.image.load('Assets\Bg.jpg')
#Charger le jeu
game = Game()
running = True
#Boucle du jeu
while running:
#Mettre à jour l'écran
pygame.display.flip()
# Afficher le background
screen.blit(background, (0, 0))
# Afficher le joueur
screen.blit(game.player.image, game.player.rect)
# Afficher les projectiles
game.player.all_projectiles.draw(screen)
# Déplacement du joueur
if game.pressed.get(pygame.K_RIGHT) and game.player.rect.x + game.player.rect.width < screen.get_width():
game.player.move_right()
elif game.pressed.get(pygame.K_LEFT) and game.player.rect.x > 0:
game.player.move_left()
elif game.pressed.get(pygame.K_UP) and game.player.rect.y > 0:
game.player.move_up()
elif game.pressed.get(pygame.K_DOWN) and game.player.rect.y + game.player.rect.height < screen.get_height():
game.player.move_down()
for event in pygame.event.get():
# Evenement de fermeture de la fenêtre
if event.type == pygame.QUIT:
running = False
# Détecter la touche enfoncée
elif event.type == pygame.KEYDOWN:
game.pressed[event.key] = True
# Détecter si la touche espace est enfoncée
if event.type == pygame.K_SPACE:
game.player.launch_projectile()
elif event.type == pygame.KEYUP:
game.pressed[event.key] = False
pygame.quit()
→ Edited with full code
EDIT 02 with answer :
In Main, to launch projectile if spacebar on press, code was :
...
# Détecter la touche enfoncée
elif event.type == pygame.KEYDOWN:
game.pressed[event.key] = True
# Détecter si la touche espace est enfoncée
if event.type == pygame.K_SPACE:
game.player.launch_projectile()
In the condition above, the ".type" should be replaced by ".key" :
...
# Détecter la touche enfoncée
elif event.type == pygame.KEYDOWN:
game.pressed[event.key] = True
# Détecter si la touche espace est enfoncée
if event.key == pygame.K_SPACE:
game.player.launch_projectile()
In your main file you used:
if event.type == pygame.K_SPACE:
game.player.launch_projectile()
However, the "event.type" is just "pygame.KEYDOWN" and not "pygame.K_SPACE". You should use "game.pressed.get(pygame.K_SPACE)" instead:
if game.pressed.get(pygame.K_SPACE):
game.player.launch_projectile()