在pygame中悬停效果

问题描述 投票:0回答:1

我有这个代码,当鼠标在那个块上时,每个盒子都亮起但它不起作用我想如果我使用矩形而不是加载图像我可以正常做效果但是它看起来不会很酷所以任何想法如何得到那该死的效果?

import pygame , os ,sys
pygame.init()

win = pygame.display.set_mode(300,300)
x_img = pygame.image.load(os.path.join('images', "x.png"))
o_img = pygame.image.load(os.path.join('images', "o.png"))

class Box:
    hoverd = False
    def __init__(self, height, width, x, y):
        self.height = height
        self.width = width
        self.x = x
        self.y = y
        self.img = self.is_hover()

    def draw(self, window):
        window.blit(self.img, (self.x, self.y)

    def is_hover(self):
        if self.hoverd:
            return pygame.image.load(os.path.join(resource_dir, "box_hover.png"))
        else:
            return pygame.image.load(os.path.join(resource_dir, "box.png"))

board_boxes = [Box(0, 0, 0, 0)] * 10
def draw_board(window):
    global board_boxes
    for y in (0, 100, 200):
        for x in (0, 100, 200):
            index = 1
            box = Box(100, 100, x, y)
            board_boxes[index] = box
            box.draw(window)
            index += 1

draw_board(win) 
run = True
while run:
    board = [" "] * 10
    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            run = False
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pass

    for box in board_boxes:
        if pos[0] > box.x and pos[0] < box.x + box.width:
            if pos[1] > box.y and pos[1] < box.y + box.height:
                box.hoverd = True
            else:
                box.hoverd = False

    pygame.display.update()
sys.exit()
python pygame
1个回答
1
投票

你不能使用is_hover()__init__中分配图像,并期望它会在鼠标悬停按钮时改变图像。

您应该在__init__中加载两个图像并在self.hoverd中使用draw()来显示不同的图像。

并且你必须在draw()中使用while True在所有测试后绘制所有框。

class Box:
    def __init__(self, height, width, x, y):
        self.height = height
        self.width = width
        self.x = x
        self.y = y

        self.hoverd = False

        self.img = pygame.image.load(os.path.join(resource_dir, "box.png"))
        self.img_hovered = pygame.image.load(os.path.join(resource_dir, "box_hover.png"))

    def draw(self, window):
        if self.hoverd:
            window.blit(self.img_hovered, (self.x, self.y))
        else
            window.blit(self.img, (self.x, self.y))

# create object without drawing

board_boxes = []

for y in (0, 100, 200):
    for x in (0, 100, 200):
        box = Box(100, 100, x, y)
        board_boxes.append(box)

# mainloop

run = True

while run:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # --- changes/moves ---

    pos = pygame.mouse.get_pos()

    for box in board_boxes:
        if pos[0] > box.x and pos[0] < box.x + box.width:
            if pos[1] > box.y and pos[1] < box.y + box.height:
                box.hoverd = True
            else:
                box.hoverd = False

    # --- draws ---

    # pygame.screen.fill( (0,0,0) ) # clear screen with black color

    for box in board_boxes:
         box.draw(window)

    pygame.display.update()

# end

pygame.quit()

使用pygame.Rect()你可以写得更短......

class Box:
    def __init__(self, height, width, x, y):
        self.rect = pygame.Rect(x, y, width, height)

        self.hoverd = False

        self.img = pygame.image.load(os.path.join(resource_dir, "box.png"))
        self.img_hovered = pygame.image.load(os.path.join(resource_dir, "box_hover.png"))

    def draw(self, window):
        if self.hoverd:
            window.blit(self.img_hovered, self.rect)
        else
            window.blit(self.img, self.rect)

# create object without drawing

board_boxes = []

for y in (0, 100, 200):
    for x in (0, 100, 200):
        box = Box(100, 100, x, y)
        board_boxes.append(box)

# mainloop

run = True

while run:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # --- changes/moves ---

    pos = pygame.mouse.get_pos()

    for box in board_boxes:
        box.hoverd = box.rect.collidepoint(pos)

    # --- draws ---

    # pygame.screen.fill( (0,0,0) ) # clear screen with black color

    for box in board_boxes:
         box.draw(window)

    pygame.display.update()

# end

pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.