曾经尝试将主角变成可以在游戏中调用的类。这是因为我知道,由于计划实施敌人和可产卵的加电,它的开发将在以后变得更加复杂。
由于某种原因,将绘制我的精灵,但是获取它的方法会检测到我的钥匙然后移动(getkey,animandmove),因为按任何输入都不会移动它,因此似乎无法正常工作。请帮忙?
import pygame
from pygame.locals import *
import sys
import pyganim # used for animations
pygame.init()
pygame.mixer.init()
WINDOWWIDTH = 1600
WINDOWHEIGHT = 900
# x = 100
# y = 100
# vel = 10
# width = 64
# height = 64
# moveleft = moveright = moveup = movedown = False
# direction = 'left'
background = pygame.image.load('sprites/background1.png')
leftidle = pygame.image.load("sprites/left.png")
rightidle = pygame.image.load("sprites/right.png")
upidle = pygame.image.load("sprites/up.png")
downidle = pygame.image.load("sprites/down.png")
charanim = {"walkleft": pyganim.PygAnimation(
[("sprites/left2.png", 100), ("sprites/left.png", 100), ("sprites/left3.png", 100), ("sprites/left.png", 10)]),
"walkright": pyganim.PygAnimation(
[("sprites/right2.png", 100), ("sprites/right.png", 100), ("sprites/right3.png", 100),
("sprites/right.png", 10)]), "walkup": pyganim.PygAnimation(
[("sprites/up2.png", 100), ("sprites/up.png", 100), ("sprites/up3.png", 100), ("sprites/up.png", 10)]),
"walkdown": pyganim.PygAnimation(
[("sprites/down2.png", 100), ("sprites/down.png", 100), ("sprites/down3.png", 100), ("sprites/down.png", 10)])}
moveConductor = pyganim.PygConductor(charanim)
mainmenuanim = {"splashscreen": pyganim.PygAnimation([("sprites/splash1.png", 500), ("sprites/splash2.png", 500)])}
mainmenuConductor = pyganim.PygConductor(mainmenuanim)
muzzleflashanim = {"fire": pyganim.PygAnimation(
[("sprites/muzzleflash.png", 100), ("sprites/muzzleflash2.png", 100), ("sprites/muzzleflash3.png", 100),
("sprites/muzzleflash4.png", 100)])}
mainwin = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("2084 Ver 0.1.1.1")
def mainmenu():
global running
menulive = True
pygame.mixer.music.load('track0.ogg')
pygame.mixer.music.play(-1)
while menulive:
mainmenuConductor.play()
mainmenuanim["splashscreen"].blit(mainwin, (0, 0))
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RETURN:
pygame.mixer.music.stop()
mainloop()
if event.type == pygame.QUIT:
running = False
pygame.quit()
pygame.display.update()
def mainloop():
global running
running = True
char1 = Character()
pygame.mixer.music.load("track1.ogg")
pygame.mixer.music.play(-1)
char1.__init__()
while running:
mainwin.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
char1.getkey()
char1.animandmove()
pygame.display.update()
class Character:
def __init__(self):
self.x = 100
self.y = 100
self.vel = 10
self.width = 64
self.height = 64
self.moveleft = False
self.moveright = False
self.moveup = False
self.movedown = False
self.direction = 'left'
def animandmove(self):
if self.moveleft or self.moveright or self.moveup or self.movedown:
moveConductor.play()
# draws animations for each of the directions
if self.direction == "left":
charanim["walkleft"].blit(mainwin, (self.x, self.y))
elif self.direction == "right":
charanim["walkright"].blit(mainwin, (self.x, self.y))
elif self.direction == "up":
charanim["walkup"].blit(mainwin, (self.x, self.y))
elif self.direction == "down":
charanim["walkdown"].blit(mainwin, (self.x, self.y))
# moving the physicial character
if self.moveleft and self.x > 0:
self.x -= self.vel
if self.moveright and self.x < (WINDOWWIDTH - 64):
self.x += self.vel
if self.moveup and self.y > 0:
self.y -= self.vel
if self.movedown and self.y < (WINDOWHEIGHT - 64):
self.y += self.vel
else:
moveConductor.stop()
if self.direction == "left":
mainwin.blit(pygame.image.load("sprites/left.png"), (self.x, self.y))
elif self.direction == "right":
mainwin.blit(pygame.image.load("sprites/right.png"), (self.x, self.y))
elif self.direction == "up":
mainwin.blit(pygame.image.load("sprites/up.png"), (self.x, self.y))
elif self.direction == "down":
mainwin.blit(pygame.image.load("sprites/down.png"), (self.x, self.y))
def getkey(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
self.moveleft = True
self.moveright = False
if not self.moveup and not self.movedown:
self.direction = "left"
elif event.key == K_RIGHT or event.key == K_d:
self.moveleft = False
self.moveright = True
if not self.moveup and not self.movedown:
self.direction = "right"
elif event.key == K_UP or event.key == K_w:
self.moveup = True
self.movedown = False
if not self.moveleft and not self.moveright:
self.direction = "up"
elif event.key == K_DOWN or event.key == K_s:
self.moveup = False
self.movedown = True
if not self.moveleft and not self.moveright:
self.direction = "down"
elif event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
self.moveleft = False
if self.moveup:
self.direction = "up"
if self.movedown:
self.direction = "down"
elif event.key == K_RIGHT or event.key == K_d:
self.moveright = False
if self.moveup:
self.direction = "up"
if self.movedown:
self.direction = "down"
elif event.key == K_UP or event.key == K_w:
self.moveup = False
if self.moveleft:
self.direction = "left"
if self.moveright:
self.direction = "right"
elif event.key == K_DOWN or event.key == K_s:
self.movedown = False
if self.moveleft:
self.direction = "left"
if self.moveright:
self.direction = "right"
pygame.display.update()
mainmenu()
pygame.quit()
答案 0 :(得分:1)
主要问题是您有两个事件循环。
在主循环中,调用pygame.event.get()
以获取将清除事件队列的所有事件。
稍后,您在pygame.event.get()
中再次致电char1.getkey()
。此时,事件队列已经为空(当然,反之亦然)。
最简单的解决方法是将每个事件从主循环传递到char1
:
while running:
mainwin.blit(background, (0, 0))
for event in pygame.event.get():
char1.getkey(event)
if event.type == pygame.QUIT:
pygame.quit()
char1.animandmove()
pygame.display.update()
并在getkey
中处理它们:
def getkey(self, event):
if event.type == ...
...
您的代码还有其他一些问题,但这不在此问题/答案的范围内。