so the code was working but i dont know what happened, so if you click the Left CTRL or the right CTRL the red/yellow should fire a bullet but that's not happening . so i used print to check if the code would append something to the red/yellow_bullet lists but it didnt so is the problem from my side or is it a problem in the code??? please help
Code :
import pygame
import os
from pygame import event #opereting system
WIDTH, HEIGHT = 900, 500 #this is to make the WINDOW
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Maisa Ahmed")
DARK_GREEN = (00, 64, 00)
BLACK = (0, 0, 0)
BLOOD_RED = (136, 8, 8)
YELLOW = (255, 255, 0)
boarder = pygame.Rect(WIDTH//2-5, 0, 10, HEIGHT)
FPS = 60
SPACESHIP_WIDTH , SPACESHIP_HEIGHT = 55,40
velocity = 5
Bullet_velocity = 7
MAX_BULLETS = 5
YELLOW_HITS = pygame.USEREVENT 1
RED_HITS = pygame.USEREVENT 2
#importing images
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)
def draw_window(red ,yellow, red_bullets, yellow_bullets):
WIN.fill(DARK_GREEN)
pygame.draw.rect(WIN, BLACK, boarder)
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, BLOOD_RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def yellow_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - velocity > 0:
yellow.x -= velocity
if keys_pressed[pygame.K_d] and yellow.x velocity yellow.width < boarder.x:
yellow.x = velocity
if keys_pressed[pygame.K_w] and yellow.y - velocity > 0:
yellow.y -= velocity
if keys_pressed[pygame.K_s] and yellow.y velocity yellow.height < HEIGHT - 10:
yellow.y = velocity
def red_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - velocity > boarder.x boarder.width: # left
red.x -= velocity
if keys_pressed[pygame.K_RIGHT] and red.x velocity red.width < WIDTH: # right
red.x = velocity
if keys_pressed[pygame.K_UP] and red.y - velocity > 0:# up
red.y -= velocity
if keys_pressed[pygame.K_DOWN] and red.y velocity red.height < HEIGHT - 10: # down
red.y = velocity
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x = Bullet_velocity
if red.colliderect(bullet):
pygame.event.post(event.Event(RED_HITS))
yellow_bullets.remove
for bullet in red_bullets:
bullet.x -= Bullet_velocity
if yellow.colliderect(bullet):
pygame.event.post(event.Event(YELLOW_HITS))
red_bullets.remove
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) # x,y , width , height
red_bullets = []
yellow_bullets = []
clock = pygame.time.Clock() # create a clock object which can be used to keep track of time
run = True
while run: #event loop
clock.tick(FPS) #ensures that you will never go above the FPS you set the game on
for event in pygame.event.get():
if event.type == pygame.QUIT: #.QUIT to end pygame in the loop
run = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x yellow.width, yellow.y yellow.height//2 - 2,10,5) # so it goes from the middle
yellow_bullets.append(bullet)
if event.type == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y red.height//2 - 2,10,5) # so it goes from the middle
red_bullets.append(bullet)
print(red_bullets, yellow_bullets)
keys_pressed = pygame.key.get_pressed() # this tells us what keys are being clicked every 1
yellow_movement(keys_pressed, yellow)
red_movement(keys_pressed, red)
handle_bullets(yellow_bullets, red_bullets, yellow, red)
draw_window(red, yellow, red_bullets, yellow_bullets) #you took the yellow and red to the Drawing functions
pygame.quit #.quit to end pygame in general
if __name__ == "__main__": #doesnt matter
main()
CodePudding user response:
Change event.type == pygame.K_LCTRL
to event.key == pygame.K_LCTRL
and event.type == pygame.K_RCTRL
to event.key == pygame.K_RCTRL