我正在尝试让演员 "player" 出现在屏幕上(与其他演员一起)并向其添加控件
i am trying to make the actor "player" show on screen (along with the other actors) and add controls to it
import pgzero, pygame
#music, sprites and background
music.play("temp.mp3")
x = 0
y = 0
WIDTH = 850
HEIGHT = 425
screen = pygame.display.set_mode((WIDTH, HEIGHT))
title = Actor('title.png') #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
def draw():
screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
player.draw()
foe.draw()
vel = 5
#controls
screen.update()
while True:
player.pos = x, y
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left')
x -= vel
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right')
x += vel
if event.key == pygame.K_UP or event.key == ord('w'):
print('up')
y += vel
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down')
y -= vel
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left stop')
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right stop')
if event.key == pygame.K_UP or event.key == ord('w'):
print('up stop')
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down stop')
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
screen.update()
我试图让它工作,但我不断收到各种错误,我现在遇到的错误是:AttributeError: 'NoneType' object has no attribute 'blit'
我是编码新手,我觉得它很有趣,但我似乎没有掌握它哈哈。希望你能帮助我解决问题 :D 谢谢!
Indentation in your code is incorrect after screen.clear()
. You have to delete the complete while True:
-loop. This code will not work with Pygame Zero. It is a Pygame 应用程序和事件循环。 Pygame 零基于 Pygame 但不一样。您不能使用 Pygame 零,而是 Pygame 的事件处理。注意 draw
在后台连续调用。 draw
是“应用程序循环”。
一个工作示例可能如下所示:
import pgzrun
import pygame
WIDTH = 850
HEIGHT = 425
title = Actor('title.png') #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
vel = 5
music.play("temp.mp3")
def draw():
screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
player.draw()
foe.draw()
def update():
keys = pygame.key.get_pressed()
x, y = player.pos
x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
player.pos = x, y
pgzrun.go()
import pgzero, pygame
#music, sprites and background
music.play("temp.mp3")
x = 0
y = 0
WIDTH = 850
HEIGHT = 425
screen = pygame.display.set_mode((WIDTH, HEIGHT))
title = Actor('title.png') #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
def draw():
screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
player.draw()
foe.draw()
vel = 5
#controls
screen.update()
while True:
player.pos = x, y
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left')
x -= vel
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right')
x += vel
if event.key == pygame.K_UP or event.key == ord('w'):
print('up')
y += vel
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down')
y -= vel
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left stop')
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right stop')
if event.key == pygame.K_UP or event.key == ord('w'):
print('up stop')
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down stop')
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
screen.update()
我试图让它工作,但我不断收到各种错误,我现在遇到的错误是:AttributeError: 'NoneType' object has no attribute 'blit' 我是编码新手,我觉得它很有趣,但我似乎没有掌握它哈哈。希望你能帮助我解决问题 :D 谢谢!
Indentation in your code is incorrect after screen.clear()
. You have to delete the complete while True:
-loop. This code will not work with Pygame Zero. It is a Pygame 应用程序和事件循环。 Pygame 零基于 Pygame 但不一样。您不能使用 Pygame 零,而是 Pygame 的事件处理。注意 draw
在后台连续调用。 draw
是“应用程序循环”。
一个工作示例可能如下所示:
import pgzrun
import pygame
WIDTH = 850
HEIGHT = 425
title = Actor('title.png') #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
vel = 5
music.play("temp.mp3")
def draw():
screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
player.draw()
foe.draw()
def update():
keys = pygame.key.get_pressed()
x, y = player.pos
x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
player.pos = x, y
pgzrun.go()