Я пытаюсь создать простой шутер в pygame, я относительный новичок в python, программировании и общем, поэтому я, вероятно, пропустил что-то важное, я пытался несколько раз выяснить, что не позволяет мне рендерить спрайты на окно. Мне нужно выяснить, как визуализировать спрайты на экране, и заново визуализировать их, когда я нажимаю клавиши со стрелками.
Я уже просмотрел self.fill (), проблема может быть в другом. Я пытался изменить цвета RGB. однако это не сработало, поэтому в моем коде скорее всего отсутствует что-то важное. Я не могу сузить, где проблема, однако, поэтому я включил весь код.
import pygame
import time
from sys import exit
import random
BLACK = (0, 0, 0)
YELLOW = (0, 200, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 100)
PURPLE = (255, 0, 200)
# set up pygame display
pygame.init()
screen_width = 500
screen = pygame.display.set_mode((500, screen_width))
pygame.display.set_caption('Trench Raid')
# classes
class Player(pygame.sprite.Sprite):
''' this class controls the player '''
def __init__(self):
super().__init__()
self.image = pygame.Surface([10, 10])
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
class Enemy(pygame.sprite.Sprite):
''' this class conrols enemies '''
def __init__(self, color):
super().__init__()
self.image = pygame.Surface([10, 10])
self.image.fill(color)
self.rect = self.image.get_rect()
class Supporter(pygame.sprite.Sprite):
''' this class controls supporters'''
def __init__(self, color):
super().__init__()
self.image = pygame.Surface([10, 1])
self.image.fill(color)
self.rect = self.image.get_rect()
class Projectile(pygame.sprite.Sprite):
''' this class conrols bullets '''
def __init__(self):
super().__init__()
self.image = pygame.Surface([2, 5])
self.image.fill(RED)
self.rect = self.image.get_rect()
playerSupporterFire = True
def update(self):
self.rect.y += 3
class FireLine(pygame.sprite.Sprite):
''' this class controls the fireLine'''
def __init__(self):
super().__init__()
self.image = pygame.Surface([500, 1])
self.image.fill(BLUE)
self.rect = self.image.get_rect()
all_sprites_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
supporter_list = pygame.sprite.Group()
projectile_list = pygame.sprite.Group()
fireline_list = pygame.sprite.Group()
player_list = pygame.sprite.Group()
for i in range(20):
enemy = Enemy(BLUE)
enemy.rect.x = random.randrange(500)
enemy.rect.y = random.randrange(300, 350)
enemy_list.add(enemy)
all_sprites_list.add(enemy)
for i in range(10):
supporter = Supporter(PURPLE)
supporter.rect.x = random.randrange(screen_width)
supporter.rect.y = random.randrange(100, 200)
supporter_list.add(supporter)
all_sprites_list.add(supporter)
fireline = FireLine()
fireline_list.add(fireline)`enter code here`
all_sprites_list.add(fireline)
player = Player()
all_sprites_list.add(player)
x, y = 150, 353
MOVE_RIGHT = 1
MOVE_LEFT = 2
MOVE_UP = 1
MOVE_DOWN = 2
direction = 0
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
direction = MOVE_LEFT
elif event.key == pygame.K_RIGHT:
direction = MOVE_RIGHT
elif event.key == pygame.K_UP:
direction = MOVE_UP
elif event.type == pygame.K_DOWN:
direction = MOVE_DOWN
if event.key == pygame.K_SPACE:
projectile = Projectile()
projectile.rect.x = player.rect.x
projectile.rect.y = player.rect.y
all_sprites_list.add(projectile)
projectile_list.add(projectile)
#making enemies shooty shoot
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
direction = 0
elif event.key == pygame.K_RIGHT:
direction = 0
elif event.type == pygame.K_UP:
direction = 0
elif event.type == pygame.K_DOWN:
direction = 0
if event.key == pygame.K_SPACE:
pass
if (direction == MOVE_LEFT):
x-=0.05
elif(direction == MOVE_RIGHT):
x+=0.05
elif(direction == MOVE_UP):
y+=0.05
elif(direction == MOVE_DOWN):
y-=0.05
all_sprites_list.update()
for projectile in projectile_list:
enemy_hit_list = pygame.sprite.spritecollide(projectile, enemy_list, True)
player_hit_list = pygame.sprite.spritecollide(projectile, enemy_list, True)
for enemy in enemy_hit_list:
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
for player in player_hit_list:
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
if bullet.rect.y < -10:
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
screen.fill((0, 0, 0))
all_sprites_list.draw(screen)
screen.blit(background, (0, 0))
screen.blit(player, (x, y))
pygame.display.update()
pygame.display.flip()
clock.tick(60)
pygame.quit()