我目前正在尝试使人流随机移动,每次我运行该程序时,它都会崩溃,并且不确定为什么。
我以为这是信息过载,所以我在上面加了一些延迟,但仍然崩溃。 我假设我没有正确使用pygame或有一个命令我没有运行/运行错误,但我一直在寻找解决方案,但我找不到任何解决方案。这也是我做的第一个如此大的项目,所以我可能会做错更多的事情。
pygame.init()
(width, height) = (600, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption('kings Game')
running = True
class King(object):
#reproduction,Food count, food distributuon, movment
Status = 1
def __init__(self,kfood,King_reproduction):
self.food = kfood
self.King_reproduction = King_reproduction
def Charecter():
x=50
y=50
width=60
highth=60
vel=5
Status = 1
pygame.draw.circle(screen,(0,0,255), (x,y), 10,)
pygame.display.update()
#KEYS = pygame.key.get_pressed()
while Status == 1:
direction = random.randrange(3)
time.sleep(2)
if direction == 0: #right
x += vel
#time.sleep(5)
print(direction)
elif direction == 1:#left
x -= vel
#time.sleep(5)
print(direction)
elif direction == 2:#up
y += vel
#time.sleep(5)
print(direction)
elif direction == 3:#down
y -= vel
#time.sleep(5)
print(direction)
else:
break
screen.fill(0)
def reproduction():
pass
def food(kfood):
if kfood == 1:
print('bet')
else:
print('nope')
class People():
#movment, reproduction, giving king food, finding food
def reproduction():
pass
def movment():
pass
def food():
pass
class Food():
#spawn
pass
test = King.Charecter()
test
while running == True:
pg.display.flip()
for event in pg.event.get():
if event.type==pg.event.QUIT:
running = False
答案 0 :(得分:2)
更改类King
。制作x
,y
,width
,height
和vel
instance attributes。
Character
必须是实例Method,它执行一个“步骤”而不是一个无限的while
循环。
请注意,Character
中的循环会更改对象的位置,但不会绘制字符或处理事件,因此您没有“看到”对象的更改。
无论如何,创建多个应用程序循环不是一个好主意。使用游戏循环处理事件并连续绘制场景。
class King(object):
#reproduction,Food count, food distributuon, movment
Status = 1
def __init__(self, kfood=None, King_reproduction=None):
self.food = kfood
self.King_reproduction = King_reproduction
self.x=50
self.y=50
self.width=60
self.highth=60
self.vel=5
def Character(self, surface):
#KEYS = pygame.key.get_pressed()
if self.Status == 1:
self.direction = random.randrange(4)
if self.direction == 0: #right
self.x += self.vel
print(self.direction)
elif self.direction == 1:#left
self.x -= self.vel
print(self.direction)
elif self.direction == 2:#up
self.y += self.vel
print(self.direction)
elif self.direction == 3:#down
self.y -= self.vel
print(self.direction)
pygame.draw.circle(surface, (0,0,255), (self.x,self.y), 10)
创建King
的实例并使用主应用程序循环,以不断更新并分别绘制对象:
test = King()
while running == True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
running = False
screen.fill(0)
test.Character(screen)
pygame.display.update()