首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >pygame中的悬停效果

pygame中的悬停效果
EN

Stack Overflow用户
提问于 2019-04-25 03:46:38
回答 1查看 767关注 0票数 0

我有这个代码,当鼠标放在那个块上时,让每个框都亮起来,但它不工作,我想如果我使用矩形而不是加载图像,我可以正确地做效果,但它看起来不会很酷,所以你有什么想法如何获得那个该死的效果吗?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 05:43:23

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

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

在所有测试之后,你必须使用while True中的draw()来绘制所有的方框。

代码语言:javascript
复制
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()你可以把它写得更短...

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55837520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档