嗨,我是一个 python 菜鸟,我正在尝试自己用 pygame 制作一个刽子手游戏,同时尽可能避免来自 YouTube 教程的帮助。
我的问题是:
当我将鼠标悬停在按钮上时,按钮会改变颜色(这很好),但即使我仍在按钮上悬停,它也会变回原来的颜色。此外,将鼠标悬停在多个按钮上时,按钮的响应速度非常差。
当我单击一个按钮时,程序认为我多次单击该按钮。print('clicked!')
因为它多次执行该行。
最后,当我点击一个按钮来 blit 一个精灵时,它只会短暂地 blit 精灵,然后它会自动取消 blit 自己。
这是我的代码:
import pygame
pygame.init()
# DISPLAY
WIDTH, HEIGHT = 800, 500
window = pygame.display.set_mode((WIDTH, HEIGHT))
# TITLE BAR
TITLE = "Hangman"
pygame.display.set_caption(TITLE)
# HANGMAN SPRITES
man = [pygame.image.load(f"hangman{frame}.png") for frame in range(0, 7)]
class Button:
def __init__(self, color, x, y, radius, text=""):
self.radius = radius
self.color = color
self.x = x
self.y = y
self.width = 2
self.text = text
self.visible = True
def draw(self, window, outline=None):
if self.visible:
if outline:
# draws a bigger circle behind
pygame.draw.circle(window, outline, (self.x, self.y), self.radius + 2, 0)
pygame.draw.circle(window, self.color, (self.x, self.y), self.radius, 0)
if self.text != "":
if self.visible:
font = pygame.font.SysFont("courier", 30)
text = font.render(self.text, 1, (0, 0, 0))
window.blit(text, (self.x - text.get_width() / 2, self.y - text.get_height() / 2))
def hover(self, pos):
if self.y + self.radius > pos[1] > self.y - self.radius:
if self.x + self.radius > pos[0] > self.x - self.radius:
return True
return False
def main():
run = True
FPS = 60
clock = pygame.time.Clock()
large_font = pygame.font.SysFont("courier", 50)
letters = []
error = 0
另外,我从 YouTube 上的 Tech With Tim Hangman 教程中获得了 sprite(我只是在没有看他编写游戏代码的情况下获得了 sprite,因为我想尝试自己做,这样我可以学到更多)。我也从 Tech With Tim 的视频中获得了按钮类的代码。
心有法竹
回首忆惘然
相关分类