2017-12-04 3 views
0

基本的に、私はボタンを作成し、それに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) 

答えて

2

問題は、あなたのcreateButton方法の2行目にタイプミスによって引き起こされた(pygame.Rect sおよびその衝突検出方法を使用する方が良いでしょう) whileループの前にButtondrawとメソッドを渡すと、whileループ内でbutton.draw(screen)を呼び出すことができます。次の例で私はすでにボタンを修正しました。 blitの位置と衝突の検出にpygame.Rectを使用することで、コードを単純化することができます。ボタンをリストに入れて描画し、forループでイベントを処理することもできます(例: for button in button_list: button.draw(screen)

import sys 
import pygame 


pygame.init() 
FPS = 60 
clock = pygame.time.Clock() 
screen = pygame.display.set_mode((800, 700)) 


class Button(): 
    def __init__(self, msg, font, x, y, w, h, color): 
     self.msg = msg 
     self.color = color 
     self.font = pygame.font.SysFont(font, 32) 
     self.text_surf = self.font.render(self.msg, True, self.color) 
     self.image_normal = pygame.Surface((w, h)) 
     self.image_normal.fill(pygame.Color('dodgerblue1')) 
     self.image_hover = pygame.Surface((w, h)) 
     self.image_hover.fill(pygame.Color('lightskyblue')) 

     self.image = self.image_normal 
     self.rect = self.image.get_rect(topleft=(x, y)) 
     # To center the text rect. 
     self.text_rect = self.text_surf.get_rect(center=self.rect.center) 

    def handle_event(self, event): 
     if event.type == pygame.MOUSEMOTION: 
      if self.rect.collidepoint(event.pos): 
       self.image = self.image_hover 
      else: 
       self.image = self.image_normal 

    def draw(self, screen): 
     screen.blit(self.image, self.rect) 
     screen.blit(self.text_surf, self.text_rect) 


local_button = Button(
    "Local Play", "Comic Sans MS", 325, 200, 150, 75, (255, 255, 255)) 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

     local_button.handle_event(event) 

    screen.fill((30, 30, 30)) 
    local_button.draw(screen) 

    pygame.display.update() 
    clock.tick(FPS) 
+0

私はあなたのコードを理解することができますが、それでも問題は何か分かりません。 それは最適化なのか何か? – SaGwa

+0

ああ、 'createButton'メソッドの2行目の入力ミスであったようです:'(mouseX> = self.xとmouseY <= self.x + self.w) '...' mouseY'は'mouseX'。 – skrx

+0

私はとても馬鹿だった...あなたの親切と追加のヒントをありがとう! – SaGwa

関連する問題