如何在 pygame 中绘制 class 个对象的列表?
How do I draw a list of class objects in pygame?
我正在尝试在 pygame 中创建 Space 个入侵者(我是初学者,这只是我的第二个游戏)并且我创建了一个外星对象列表:
numOfAliens = 24
aliens = []
# An alien class containing an x, y, width, and height value
class alien:
x, y = 0, 0
width, height = 20, 20
# Loops 24 times and adds alien to the class
for i in range(numOfAliens):
aliens.append(alien)
我还有一段代码,将每个 x 值加 5 和宽度 space 外星人分开:
for i in aliens:
i.x += 5 + i.width
print(i.x, i.y, i.width, i.height)
印刷品告诉我前两段代码工作正常,没有任何问题,当我尝试将其绘制到 pygame window:
时出现问题
# Loops 24 times, drawing each alien to the window
for i in range(numOfAliens):
pygame.draw.rect(win, GREEN, (aliens[i].x, aliens[i].y, aliens[i].width, aliens[i].height))
aliens[i].x 肯定会得到列表中每个对象的 x 值,但它没有。
如果我在这个 for 循环中添加一个 print("hi") 它会无限打印出 "hi" 而它应该只循环 24 次,并且在 window 上没有绘制任何东西,有没有办法解决这个问题?
如果需要,这里是所有代码:
import pygame
pygame.init()
clock = pygame.time.Clock()
# Window
win_width, win_height = 500, 500
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Space Invaders")
# Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (30, 224, 27)
# Player
player_width, player_height = 20, 20
player_x, player_y = int((win_width / 2) - (player_width / 2)), (win_height - 50)
player_vel = 10
isShooting = False
isAlive = True
# Bullet
bullet_width, bullet_height = 4, 16
bullet_x, bullet_y = player_x, player_y
bullet_vel = 5
# Aliens
numOfAliens = 24
aliens = []
class alien:
x, y = 0, 0
width, height = player_width, player_height
for i in range(numOfAliens):
aliens.append(alien)
for i in aliens:
i.x += 5 + i.width
print(i.x, i.y, i.width, i.height)
# Draw Function
def draw():
win.fill(BLACK)
pygame.draw.rect(win, GREEN, (player_x, player_y, player_width, player_height))
for i in aliens:
pygame.draw.rect(win, GREEN, (i.x, i.y, i.width, i.height))
if isShooting:
pygame.draw.rect(win, WHITE, (bullet_x, bullet_y, bullet_width, bullet_height))
pygame.display.update()
# Game Loop
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_vel
if keys[pygame.K_RIGHT] and player_x < win_width - player_width:
player_x += player_vel
if keys[pygame.K_SPACE] and not(isShooting):
bullet_y = player_y
bullet_x = int(player_x + (player_width / 2))
isShooting = True
if bullet_y + bullet_height <= 0:
isShooting = False
if isShooting:
bullet_y -= bullet_vel
draw()
pygame.quit()
实际上你并没有创建任何外星人的实例。 alien
只是 class 本身。
aliens.append(alien)
要创建 class alien
的实例,您必须添加括号:
参见 Class Objects
aliens.append(alien())
请注意,python 中的 class 个名称应以大写字母开头。
参见 Style Guide for Python Code - Class Names
将带有 x 和 y 坐标参数的构造函数添加到 class Alien
并为位置和大小创建 instance attributes。
根据控制变量i
计算外星人的位置:
numOfAliens = 24
aliens = []
class Alien:
def __init__(self, x, y):
self.x, self.y = x, y
self.width, self.height = player_width, player_height
for i in range(numOfAliens):
aliens.append(Alien(i * (5+player_width), 0))
我正在尝试在 pygame 中创建 Space 个入侵者(我是初学者,这只是我的第二个游戏)并且我创建了一个外星对象列表:
numOfAliens = 24
aliens = []
# An alien class containing an x, y, width, and height value
class alien:
x, y = 0, 0
width, height = 20, 20
# Loops 24 times and adds alien to the class
for i in range(numOfAliens):
aliens.append(alien)
我还有一段代码,将每个 x 值加 5 和宽度 space 外星人分开:
for i in aliens:
i.x += 5 + i.width
print(i.x, i.y, i.width, i.height)
印刷品告诉我前两段代码工作正常,没有任何问题,当我尝试将其绘制到 pygame window:
时出现问题# Loops 24 times, drawing each alien to the window
for i in range(numOfAliens):
pygame.draw.rect(win, GREEN, (aliens[i].x, aliens[i].y, aliens[i].width, aliens[i].height))
aliens[i].x 肯定会得到列表中每个对象的 x 值,但它没有。 如果我在这个 for 循环中添加一个 print("hi") 它会无限打印出 "hi" 而它应该只循环 24 次,并且在 window 上没有绘制任何东西,有没有办法解决这个问题?
如果需要,这里是所有代码:
import pygame
pygame.init()
clock = pygame.time.Clock()
# Window
win_width, win_height = 500, 500
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Space Invaders")
# Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (30, 224, 27)
# Player
player_width, player_height = 20, 20
player_x, player_y = int((win_width / 2) - (player_width / 2)), (win_height - 50)
player_vel = 10
isShooting = False
isAlive = True
# Bullet
bullet_width, bullet_height = 4, 16
bullet_x, bullet_y = player_x, player_y
bullet_vel = 5
# Aliens
numOfAliens = 24
aliens = []
class alien:
x, y = 0, 0
width, height = player_width, player_height
for i in range(numOfAliens):
aliens.append(alien)
for i in aliens:
i.x += 5 + i.width
print(i.x, i.y, i.width, i.height)
# Draw Function
def draw():
win.fill(BLACK)
pygame.draw.rect(win, GREEN, (player_x, player_y, player_width, player_height))
for i in aliens:
pygame.draw.rect(win, GREEN, (i.x, i.y, i.width, i.height))
if isShooting:
pygame.draw.rect(win, WHITE, (bullet_x, bullet_y, bullet_width, bullet_height))
pygame.display.update()
# Game Loop
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_vel
if keys[pygame.K_RIGHT] and player_x < win_width - player_width:
player_x += player_vel
if keys[pygame.K_SPACE] and not(isShooting):
bullet_y = player_y
bullet_x = int(player_x + (player_width / 2))
isShooting = True
if bullet_y + bullet_height <= 0:
isShooting = False
if isShooting:
bullet_y -= bullet_vel
draw()
pygame.quit()
实际上你并没有创建任何外星人的实例。 alien
只是 class 本身。
aliens.append(alien)
要创建 class alien
的实例,您必须添加括号:
参见 Class Objects
aliens.append(alien())
请注意,python 中的 class 个名称应以大写字母开头。
参见 Style Guide for Python Code - Class Names
将带有 x 和 y 坐标参数的构造函数添加到 class Alien
并为位置和大小创建 instance attributes。
根据控制变量i
计算外星人的位置:
numOfAliens = 24
aliens = []
class Alien:
def __init__(self, x, y):
self.x, self.y = x, y
self.width, self.height = player_width, player_height
for i in range(numOfAliens):
aliens.append(Alien(i * (5+player_width), 0))