我有这段代码,当鼠标位于该块上时,每个框都会亮起 但它不起作用,我想如果我使用矩形而不是加载图像,我可以正确地实现效果,但是看起来并不酷 那么关于如何获得该死效果的任何想法?
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()
答案 0 :(得分: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()