Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame-按钮渲染问题_Python_Pygame - Fatal编程技术网

Python Pygame-按钮渲染问题

Python Pygame-按钮渲染问题,python,pygame,Python,Pygame,基本上,我想用pygame创建一个按钮并在上面写下文本,我成功了 但当光标位于按钮上时,最后一个按钮(名为LeadboardButton)不会将图像更改为“/image/mouseOnButton.png” 我真想知道为什么会这样。按钮的其余部分很好 import pygame from pygame.locals import * import sys FPS = 60 clock = pygame.time.Clock() screen = pygame.display.set_mode(

基本上,我想用pygame创建一个按钮并在上面写下文本,我成功了

但当光标位于按钮上时,最后一个按钮(名为LeadboardButton)不会将图像更改为“/image/mouseOnButton.png”

我真想知道为什么会这样。按钮的其余部分很好

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)
导入pygame
从pygame.locals导入*
导入系统
FPS=60
clock=pygame.time.clock()
screen=pygame.display.set_模式((800700))
屏幕填充((255、255、255))
pygame.font.init()
pygame.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=颜色
def创建按钮(自身):
(mouseX,mouseY)=pygame.mouse.get_pos()

如果((mouseX>=self.x和mouseY=self.y和mouseY问题是由
createButton
方法第二行中的键入错误引起的(最好使用s及其碰撞检测方法):


问题是由
createButton
方法第二行中的输入错误引起的(最好使用s及其碰撞检测方法):


我能理解你的代码,但我仍然不知道问题出在哪里。这是因为优化还是什么?哦,看起来这只是
createButton
方法第二行的一个输入错误:
(mouseX>=self.x和mouseY我太笨了…谢谢你的好意和额外的提示!我能理解你的代码,但我仍然不知道问题出在哪里。这是因为优化还是什么?哦,这似乎只是
createButton
方法:
(mouseX>=self.x和mouseY我太笨了……谢谢你的好意和额外的提示!
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 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)