我正在尝试在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()