Witam robię prostą grę w pythonie w bibliotece Pygame. Aktualnie robię prostą fizykę strzelania w, której chodzi o to, aby po naciśnięciu lewego ctrl postać wystrzeliwuje 4 pociski w górę, w dół, w lewo i prawo i jak dotknie wroga to dodaje punkt i się usuwa, a jak wypadnie po za mapę to też się usuwa. Jednakże mam problem, mógłby mi ktoś pomóc, aby nie było żadnego błędu.
import pygame
import os
pygame.font.init()
pygame.mixer.init()
width, height = 700, 700
WHITE = 255, 255, 255
BLACK = 0, 0, 0
RED = 255, 0, 0
GREEN = 0, 255, 0
score = 0
MAX_BULLETS = 10
HIT = pygame.USEREVENT + 1
SHOOT = pygame.USEREVENT + 2
WIN = pygame.display.set_mode((width, height))
NAME = pygame.display.set_caption("FIRST GAME")
FPS = 60
CREATURE = pygame.Rect(height//5, 100, 100, width//5)
BORDER = pygame.Rect(height, 0, 1, width)
ENEMY = pygame.Rect(height//2, 350, 90, width//5)
def color_window(bullets):
WIN.fill(WHITE)
pygame.draw.rect(WIN, BLACK, CREATURE)
pygame.draw.rect(WIN, RED, ENEMY)
pygame.display.update()
def creature_movement(key_pressed, CREATURE, score):
if key_pressed[pygame.K_w] and CREATURE.y - 5 > 0:
CREATURE.y -= 5
if key_pressed[pygame.K_s] and CREATURE.y + 5 < 600:
CREATURE.y += 5
if key_pressed[pygame.K_a] and CREATURE.x - 5 > 0:
CREATURE.x -= 5
if key_pressed[pygame.K_d] and CREATURE.x + 5 + CREATURE.width < 700:
CREATURE.x += 5
def handle_bullets(bullets, CREATURE):
key_pressed = pygame.key.get_pressed()
if key_pressed(pygame.K_LCTRL):
UP, DOWN, LEFT, RIGHT = pygame.Rect(height//5, 0, 10, width//5)
for event in pygame.event.get:
pygame.post.event(event.Event(SHOOT))
if event.type == SHOOT:
UP = WIN.blit(CREATURE.x, CREATURE.y)
DOWN = WIN.blit(CREATURE.x, CREATURE.y)
LEFT = WIN.blit(CREATURE.x, CREATURE.y)
RIGHT = WIN.blit(CREATURE.x, CREATURE.y)
for UP in handle_bullets:
UP.y -= 7
if UP < width:
UP.remove
if UP.colliderect(ENEMY):
score + 1
UP.remove
for DOWN in handle_bullets:
DOWN.y += 7
if DOWN.y > width:
DOWN.remove
if DOWN.colliderect(ENEMY):
score + 1
DOWN.remove
for LEFT in handle_bullets:
LEFT.x -= 7
if LEFT.x < height:
LEFT.remove
if LEFT.colliderect(ENEMY):
score + 1
LEFT.remove
for RIGHT in handle_bullets:
RIGHT.x -= 7
if RIGHT.x > width:
RIGHT.remove
if RIGHT.colliderect(ENEMY):
score + 1
RIGHT.remove
def main():
Clock = pygame.time.Clock()
run = True
while run:
key_pressed = pygame.key.get_pressed()
bullets = []
Clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
creature_movement(key_pressed, CREATURE, score)
handle_bullets(bullets, CREATURE)
color_window(bullets)
if __name__ == "__main__":
main()