我正在尝试更新箭头键,使其看起来就像被按下一样。但是,它们似乎并没有在单击时更新,而是屏幕闪烁,但实际上没有任何变化。我相对较不熟悉使用Pygame在python中进行编程,并且不确定如何解决该问题。 这是我检查是否被单击的功能:
import pygame
class Button():
def __init__(self, x, y, xSize, ySize):
self.x = x
self.y = y
self.xSize = xSize
self.ySize = ySize
def checkClicked(self):
if pygame.mouse.get_pressed()[0]:
x, y = pygame.mouse.get_pos()
if y > self.y and y < self.y + self.ySize and x > self.x and x <
self.x + self.xSize:
print('Clicked')
return True
return False
这些是代表按钮的对象,您可以单击这些按钮来更改图像并移动播放器:
leftObject = Button(765, 800, 68, 78)
rightObject = Button(910, 800, 70, 81)
upObject = Button(835, 725, 70, 80)
downObject = Button(835, 800, 70, 82)
leftArrow = pygame.transform.scale(pygame.image.load('leftarrow.png'), (68,
78))
rightArrow = pygame.transform.scale(pygame.image.load('rightarrow.png'), (70,
81))
upArrow = pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
downArrow = pygame.transform.scale(pygame.image.load('downarrow.png'), (70,
82))
这是根据用户键输入或按屏幕上的按钮移动的对象:
class Player(object):
def __init__(self, x, y, filename, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.transform.scale(pygame.image.load(filename),
(self.width, self.height))
这是检查用户是否单击了它们或按下箭头键以触发图像更改的事件:
def moveEvent(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if leftObject.checkClicked():
leftArrow =
pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
pygame.display.update()
self.x -= 25
if rightObject.checkClicked():
rightArrow =
pygame.transform.scale(pygame.image.load('rightarrowdown.png'), (70, 81))
pygame.display.update()
self.x += 25
if upObject.checkClicked():
upArrow =
pygame.transform.scale(pygame.image.load('uparrowdown.png'), (70, 80))
pygame.display.update()
self.y -= 25
if downObject.checkClicked():
downArrow =
pygame.transform.scale(pygame.image.load('downarrowdown.png'), (70, 82))
pygame.display.update()
self.y += 25
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
# Left
leftArrow =
pygame.transform.scale(pygame.image.load('leftarrow.png'), (68, 78))
pygame.display.update()
# Right
rightArrow =
pygame.transform.scale(pygame.image.load('rightarrow.png'), (70, 81))
pygame.display.update()
# Up
upArrow =
pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
pygame.display.update()
# Down
downArrow =
pygame.transform.scale(pygame.image.load('downarrow.png'), (70, 82))
pygame.display.update()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
leftArrow =
pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
self.x -= 25
if event.key == pygame.K_RIGHT:
rightArrow =
pygame.transform.scale(pygame.image.load('rightarrowdown.png'), (70, 81))
self.x += 25
if event.key == pygame.K_UP:
upArrow =
pygame.transform.scale(pygame.image.load('uparrowdown.png'), (70, 80))
self.y -= 25
if event.key == pygame.K_DOWN:
downArrow =
pygame.transform.scale(pygame.image.load('downarrowdown.png'), (70, 82))
self.y += 25
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftArrow =
pygame.transform.scale(pygame.image.load('leftarrow.png'), (68, 78))
pygame.display.update()
if event.key == pygame.K_RIGHT:
rightArrow =
pygame.transform.scale(pygame.image.load('rightarrow.png'), (70, 81))
pygame.display.update()
if event.key == pygame.K_UP:
upArrow =
pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
pygame.display.update()
if event.key == pygame.K_DOWN:
downArrow =
pygame.transform.scale(pygame.image.load('downarrow.png'), (70, 82))
pygame.display.update()
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
图像最初会在游戏本身的“游戏/运行”部分中被渗入屏幕:
class Game(object):
def __init__(self):
self.screensize = [1000, 1000]
self.white = [255, 255, 255]
self.black = [0, 0, 0]
self.screen = pygame.display.set_mode(self.screensize)
#self.bunny = pygame.transform.scale(pygame.image.load('bunny.png'),
(150, 200))
self.clock = pygame.time.Clock()
self.player = Player(50, 350, 'bunny.png', 150, 200)
def Run(self):
run = True
while run:
self.clock.tick(60)
self.screen.fill(self.white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
exit()
elif event.type == pygame.K_ESCAPE:
run = False
pygame.quit()
exit()
# - Objects Event Handle -
self.player.moveEvent(event)
# - Draws -
self.player.draw(self.screen)
self.screen.blit(leftArrow, (765, 800))
self.screen.blit(rightArrow, (910, 800))
self.screen.blit(upArrow, (835, 725))
self.screen.blit(downArrow, (835, 800))
pygame.display.flip()
game = Game()
game.Run()
任何帮助或简化此方面的方法/建议将不胜感激!
答案 0 :(得分:1)
陈述
leftArrow = pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
在方法leftArrow
的范围内设置局部变量moveEvent
。
使用global
语句将变量解释为全局变量。
请注意,所有
pygame.display.update()
moveEvent
中的语句是多余的,同时此更新会导致闪烁。
在主循环末尾的单个pygame.display.flip()
语句就足够了。从方法pygame.display.update()
中删除所有moveEvent
被调用。
例如
def moveEvent(self, event):
global leftArrow, rightArrow, upArrow, downArrow # <----- add
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if leftObject.checkClicked():
leftArrow = pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
# pygame.display.update() <----- remove
self.x -= 25
# [...]