Grundsätzlich möchte ich einen Button erstellen und schreiben Sie Text mit Pygame und ich gelange.Pygame - Probleme mit Button-Rendering
Aber die letzte Schaltfläche (namens LeaderboardButton) ändert das Bild nicht in "./image/mouseOnButton.png", wenn sich der Cursor auf der Schaltfläche befindet.
Ich frage mich wirklich, warum ist das passiert. Der Rest des Buttons ist in Ordnung.
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)
Ich kann Ihren Code verstehen, aber ich weiß immer noch nicht, was das Problem war. Es ist wegen der Optimierung oder etwas? – SaGwa
Oh, es scheint nur ein Tippfehler in der zweiten Zeile der 'createButton' Methode zu sein:' (mouseX> = self.x und mouseY <= self.x + self.w) '...' mouseY' sollte sein 'MausX'. – skrx
Ich war so dumm ... danke für deine Freundlichkeit und zusätzliche Tipps! – SaGwa