So I watched this tutorial for creating simple game of spaceships fighting with pygame module, now I checked my code against the code I watched him working on and the functionallity is the same (I havent done yet so my code is less progressing), though the problem im having is that when I press once Lctrl I can see in the terminal that the list contains 3 objects instead of 1 object which is one bullet, and when I checked the code I was learning from I noticed that its contain only 1 object per one click and I cant figure out where is my mistake... Please help me :(
I went thorugh the functions of the code I was copying from and my function trying to look where is the difference and I couldnt find any...
My Code :
Main :
import sys
from settings import *
from player import Player
from time import sleep
class Game:
def __init__(self):
# Game init #
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
# Settings Players #
self.yellow_spaceship = Player('spaceship_yellow.png', 90, 100, 300)
self.red_spaceship = Player('spaceship_red.png', -90, 700, 300)
# Game Title #
pygame.display.set_caption("Zohar's Game")
icon = pygame.image.load('C:/Users/asmit/PycharmProjects/pythonProject/Zelda_Game/graphics/game_icon.png')
pygame.display.set_icon(icon)
self.red_bullets = []
self.yellow_bullets = []
def draw_window(self):
# Screen Backround #
self.screen.fill(BACKROUND_COLOR)
pygame.draw.rect(self.screen, 'black', BOARDER)
# Drawing surfaces to screen #
self.screen.blit(self.yellow_spaceship.player, self.yellow_spaceship.player_pos) # Adjust the width to be consistent
self.screen.blit(self.red_spaceship.player, self.red_spaceship.player_pos)
for bullet in self.red_bullets:
pygame.draw.rect(self.screen, 'red', bullet)
for bullet in self.yellow_bullets:
pygame.draw.rect(self.screen, 'red', bullet)
self.yellow_spaceship.input(self.red_spaceship.player_pos, 'arrows') # LEFT
self.red_spaceship.input(self.yellow_spaceship.player_pos, 'keys') # RIGHT
pygame.display.update()
def run(self):
while True:
self.clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(self.yellow_bullets) < MAX_BULLETS:
print(self.yellow_bullets)
bullet = pygame.Rect(self.yellow_spaceship.player_pos.x + PLAYER_WIDTH, self.yellow_spaceship.player_pos.y + PLAYER_HEIGHT / 1.9, 10, 5)
self.yellow_bullets.append(bullet)
if event.key == pygame.K_RCTRL and len(self.red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(self.red_spaceship.player_pos.x - 5, self.red_spaceship.player_pos.y + PLAYER_HEIGHT / 1.9, 10, 5)
self.red_bullets.append(bullet)
self.handle_bullets()
self.draw_window()
def handle_bullets(self):
for bullet in self.yellow_bullets:
bullet.x += BULLETS_VEL
if self.red_spaceship.player_pos.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
self.yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
self.yellow_bullets.remove(bullet)
for bullet in self.red_bullets:
bullet.x -= BULLETS_VEL
if self.yellow_spaceship.player_pos.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
self.red_bullets.remove(bullet)
elif bullet.x < 0:
self.red_bullets.remove(bullet)
# print(self.red_bullets, self.yellow_bullets)
if __name__ == "__main__":
game = Game()
game.run()
Player:
import os
from settings import *
import pygame
class Player():
def __init__(self, picture_path, rotate, x_pos, y_pos):
# Players Images Loading #
self.player_image = pygame.image.load(os.path.join('Assets', picture_path))
self.player = pygame.transform.rotate(pygame.transform.scale(self.player_image, (PLAYER_WIDTH, PLAYER_HEIGHT)), rotate)
self.player_pos = pygame.Rect(x_pos, y_pos, PLAYER_WIDTH, PLAYER_HEIGHT)
# Player Weapon #
def input(self, player_pos, movement_type, speed=5):
# Player Movement #
keys = pygame.key.get_pressed()
if movement_type == 'keys':
if keys[pygame.K_w] and player_pos.y - speed > 0:
player_pos.y -= speed
elif keys[pygame.K_s] and player_pos.y + speed < HEIGHT - PLAYER_HEIGHT * 1.25:
player_pos.y += speed
else:
player_pos.y += 0
if keys[pygame.K_d] and player_pos.x + speed < BOARDER_LEFT - 35:
player_pos.x += speed
elif keys[pygame.K_a] and player_pos.x - speed > 0:
player_pos.x -= speed
else:
player_pos.x += 0
if movement_type == 'arrows':
if keys[pygame.K_UP] and player_pos.y - speed > 0:
player_pos.y -= speed
elif keys[pygame.K_DOWN] and player_pos.y + speed < HEIGHT - PLAYER_HEIGHT * 1.25:
player_pos.y += speed
else:
player_pos.y += 0
if keys[pygame.K_RIGHT] and player_pos.x + speed < WIDTH - 40:
player_pos.x += speed
elif keys[pygame.K_LEFT] and player_pos.x - speed > BOARDER_RIGHT - 5:
player_pos.x -= speed
else:
player_pos.x += 0
Settings:
# Screen #
import pygame
WIDTH = 900
HEIGHT = 600
# Player #
PLAYER_WIDTH = 55
PLAYER_HEIGHT = 40
# Boarder Boundaries #
BOARDER_WIDTH = 10
BOARDER_LEFT = WIDTH/2 - BOARDER_WIDTH/2
BOARDER_RIGHT = WIDTH/2 + BOARDER_WIDTH/2
BOARDER = pygame.Rect(WIDTH/2 - BOARDER_WIDTH/2, 0, BOARDER_WIDTH, HEIGHT)
FPS = 60
TILESIZE = 64
BACKROUND_COLOR = (255, 255, 255)
BULLETS_VEL = 7
MAX_BULLETS = 3
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
The code I was learning from:
import pygame
import os
pygame.font.init()
pygame.mixer.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))
def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)
red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL
def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)
for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
red_bullets = []
yellow_bullets = []
red_health = 10
yellow_health = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
print(yellow_bullets)
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()
if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()
winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"
if yellow_health <= 0:
winner_text = "Red Wins!"
if winner_text != "":
draw_winner(winner_text)
break
keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)
handle_bullets(yellow_bullets, red_bullets, yellow, red)
draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health)
main()
if __name__ == "__main__":
main()