基本的に、私はボタンを作成し、それにpygameでテキストを書きたいと思います。Pygame - ボタンレンダリングの問題
しかし、最後のボタン(leaderboardButton)は、カーソルがボタンの上にあるときに画像を "./image/mouseOnButton.png"に変更しません。
私は本当になぜこれが起こっているのだろうか。残りのボタンは問題ありません。私はインスタンスを作成することをお勧め
if((mouseX >= self.x and mouseY <= self.x + self.w) and (mouseY >= self.y and mouseY <= self.y + self.h)):
# Should be:
if((mouseX >= self.x and mouseX <= self.x + self.w) and (mouseY >= self.y and mouseY <= self.y + self.h)):
:
import pygame
from pygame.locals import *
import sys
FPS = 60
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 700))
screen.fill((255, 255, 255))
pygame.font.init()
pygame.init()
class Button():
def __init__(self, msg, font, x , y, w, h, color):
self.msg = msg
self.font = font
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
def createButton(self):
(mouseX, mouseY) = pygame.mouse.get_pos()
if((mouseX >= self.x and mouseY <= self.x + self.w) and (mouseY >= self.y and mouseY <= self.y + self.h)):
button = pygame.image.load("./image/mouseOnButton.png")
else:
button = pygame.image.load("./image/button.png")
screen.blit(button, (self.x, self.y))
text = pygame.font.SysFont(self.font, 32)
textSurface = text.render(self.msg.encode("utf-8"), True, self.color)
screen.blit(textSurface, (self.x + self.w/len(self.msg), self.y + self.h/2))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if(event.key == pygame.K_F4 and pygame.KMOD_ALT):
sys.exit()
localButton = Button("Local Play", "./font/NanumSquareRoundEB.ttf", 325, 200, 150, 75, (255, 255, 255)).createButton()
socketButton = Button("Socket Play", "./font/NanumSquareRoundEB.ttf", 325, 325, 150, 75, (255, 255, 255)).createButton()
howtoButton = Button("How to Play", "./font/NanumSquareRoundEB.ttf", 325, 450, 150, 75, (255, 255, 255)).createButton()
leaderboardButton = Button("Leaderboard", "./font/NanumSquareRoundEB.ttf", 325, 575, 150, 75, (255, 255, 255)).createButton()
pygame.display.update()
clock.tick(FPS)
私はあなたのコードを理解することができますが、それでも問題は何か分かりません。 それは最適化なのか何か? – SaGwa
ああ、 'createButton'メソッドの2行目の入力ミスであったようです:'(mouseX> = self.xとmouseY <= self.x + self.w) '...' mouseY'は'mouseX'。 – skrx
私はとても馬鹿だった...あなたの親切と追加のヒントをありがとう! – SaGwa