现在我已经拥有了它,以便一些物体能够以不同的时间和速度在屏幕上滚动,以及一个我可以用箭头键控制的图标(英雄)。但是,每当我按下一个键,图标就会返回到原来的位置。可能是什么导致了这个?
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
winW = 800
winH = 500
surface = pygame.display.set_mode ((winW, winH),0,32)
class Enemy():
def __init__(self, char, xMoveAmnt, startY=0, startX=0, delay=0):
self.char = char
self.delay=delay
self.x = startX
self.y = startY
self.startX=startX
self.startY=startY
self.xMoveAmnt = xMoveAmnt
self.image = pygame.image.load(self.char)
self.rect = self.image.get_rect()
def moveChar(self):
if self.delay > 0:
self.delay -= 1
return
self.x += self.xMoveAmnt
if self.x + self.rect.right >= 800:
self.x=self.startX
self.y=random.randint(0,300)
class FastEnemy(Enemy):
def moveChar(self):
if self.delay > 0:
self.delay -= 1
return
self.x += self.xMoveAmnt+10
if self.x + self.rect.right >= 800:
self.x=self.startX
self.y=random.randint(0,300)
class SlowEnemy(Enemy):
def moveChar(self):
if self.delay > 0:
self.delay -= 1
return
self.x += self.xMoveAmnt-5
if self.x + self.rect.right >= 800:
self.x=self.startX
self.y=random.randint(0,300)
class Hero():
def __init__(self):
self.X_MOVE_AMT = 5
self.Y_MOVE_AMT = 5
self.spaceMoveX = 0
self.spaceMoveY = 0
self.startingX=400
self.startingY=250
self.x=self.startingX
self.y=self.startingY
self.space = pygame.image.load ("hero_sprite.jpg")
self.spaceRect = self.space.get_rect()
self.spaceRect.topleft = (100,100)
def moveHero(self):
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT :
self.x += -(self.X_MOVE_AMT)
self.y = 0
if event.key == K_RIGHT :
self.x += self.X_MOVE_AMT
self.y = 0
if event.key == K_UP :
self.y += -(self.Y_MOVE_AMT)
self.x = 0
if event.key == K_DOWN :
self.y += self.Y_MOVE_AMT
self.x = 0
if event.key == ord (' '):
self.y = 0
self.x = 0
if event.key == K_ESCAPE:
pygame.quit ()
sys.exit()
self.spaceRect.top += self.spaceMoveY
self.spaceRect.left += self.spaceMoveX
if self.spaceRect.top < 0:
self.spaceRect.top = 0
if self.spaceRect.bottom > 500:
self.spaceRect.bottom = 500
if self.spaceRect.left < 0:
self.spaceRect.left = 0
if self.spaceRect.right > 800:
self.spaceRect.right = 800
enemyList = []
hero=Hero()
for i in range (0, 8):
xMoveAmnt=20
enemyList.append(SlowEnemy(("orc3.png"), xMoveAmnt, random.randint(0, 500), 0, random.randint(0,100)))
enemyList.append(Enemy(("orc.png"), xMoveAmnt, random.randint(0, 500), 0, random.randint(0,100)))
enemyList.append(FastEnemy(("orc2.png"), xMoveAmnt, random.randint(0, 500), 0, random.randint(0,100)))
while True:
surface.fill ((255,255,255))
for enemy in enemyList:
enemy.moveChar()
hero.moveHero()
surface.blit(enemy.image, (enemy.x, enemy.y))
surface.blit(hero.space, (hero.x, hero.y))
time.sleep(00.01)
pygame.display.update()
答案 0 :(得分:2)
if event.key == K_LEFT :
self.x += -(self.X_MOVE_AMT)
self.y = 0
if event.key == K_RIGHT :
self.x += self.X_MOVE_AMT
self.y = 0
if event.key == K_UP :
self.y += -(self.Y_MOVE_AMT)
self.x = 0
if event.key == K_DOWN :
self.y += self.Y_MOVE_AMT
self.x = 0
if event.key == ord (' '):
self.y = 0
self.x = 0
if event.key == K_ESCAPE:
pygame.quit ()
sys.exit()
需要
if event.key == K_LEFT :
self.x += -(self.X_MOVE_AMT)
if event.key == K_RIGHT :
self.x += self.X_MOVE_AMT
if event.key == K_UP :
self.y += -(self.Y_MOVE_AMT)
if event.key == K_DOWN :
self.y += self.Y_MOVE_AMT
if event.key == K_ESCAPE:
pygame.quit ()
sys.exit()
每次按下按钮,我都会重置Y和X.抱歉浪费你的时间!