我正在在线观看一系列视频,以协助开发pygame项目。我无法让我的playerobject类在我的游戏中移动。一切都能正确编译,但是当我按向左,向右,向上,向下等按钮时,什么都不会发生。唯一起作用的是绘制对象和pygame退出事件。
我尝试过移动一些代码,但无济于事。
import os
import random
import time
#declarations
displaywidth = 600
displayheight = 850
gamedisplay = pygame.display.set_mode((displaywidth, displayheight))
fps = 60
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
#initializations
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Into The Battlefield")
clock = pygame.time.Clock()
#classes
class playerobject(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.centerx = displaywidth / 2
self.rect.bottom = displayheight - 10
self.speedx = 0
self.speedy = 0
def update(self):
self.speedx = 0
self.speedy = 0
keypress = pygame.key.get_pressed()
if keypress[pygame.K_LEFT]:
self.speedx = -5
if keypress[pygame.K_RIGHT]:
self.speedx = 5
if keypress[pygame.K_UP]:
self.speedy = 5
if keypress[pygame.K_DOWN]:
self.speedy = -5
self.rect.x += self.speedx
self.rect.y += self.speedy
#sprite group
sprites = pygame.sprite.Group()
playerobject = playerobject()
sprites.add(playerobject)
#gameloop
def gameloop():
gameon = True
while gameon:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameon = False
gamedisplay.fill(black)
sprites.update()
sprites.draw(gamedisplay)
pygame.display.update()
pygame.display.flip()
gameloop()
pygame.quit()
我只是想让我的绿色方块左右移动。
答案 0 :(得分:1)
除非只是一个代码粘贴问题,否则看来playerobject
类没有正确定义update()
函数(因为它缺少足够的缩进以使其成为playerobject
的一部分) )。
代码中有一个样式问题,因为它处理playerobject
和主循环中的用户输入。最好在主循环中(或至少在一个地方)处理所有用户输入。我重新安排了代码,使其具有这种行为。哦,您的上/下数字被颠倒了(可能是故意的)。
import os
import random
import time
import pygame
#declarations
displaywidth = 600
displayheight = 850
gamedisplay = pygame.display.set_mode((displaywidth, displayheight))
fps = 60
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
#initializations
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Into The Battlefield")
clock = pygame.time.Clock()
#classes
class playerobject(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.centerx = displaywidth / 2
self.rect.bottom = displayheight - 10
self.speedx = 0
self.speedy = 0
def setSpeedXY( self, x, y ):
self.speedx = x
self.speedy = y
def adjustSpeedX( self, x ):
self.speedx += x
def adjustSpeedY( self, y ):
self.speedy += y
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
#sprite group
sprites = pygame.sprite.Group()
playerobject = playerobject()
sprites.add(playerobject)
#gameloop
def gameloop():
gameon = True
while gameon:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameon = False
keypress = pygame.key.get_pressed()
if keypress[pygame.K_LEFT]:
playerobject.adjustSpeedX( -5 )
if keypress[pygame.K_RIGHT]:
playerobject.adjustSpeedX( 5 )
if keypress[pygame.K_UP]:
playerobject.adjustSpeedY( -5 )
if keypress[pygame.K_DOWN]:
playerobject.adjustSpeedY( 5 )
if keypress[pygame.K_SPACE]:
playerobject.setSpeedXY( 0, 0 )
gamedisplay.fill(black)
sprites.update()
sprites.draw(gamedisplay)
pygame.display.update()
pygame.display.flip()
gameloop()
pygame.quit()
现在它可以真正快速地移动 。