如何解决为什么在 pygame 中,我的鼠标事件在调整大小时无法与我的对象同步?
我现在可以成功地让我的游戏完全调整大小并且键盘事件工作正常,但是鼠标事件只会在我将指针放置在对象最初被 blit 到表面的位置时触发,即使在我调整屏幕大小之后也是如此。我觉得自己问得像个白痴,但我几乎尝试了所有方法,但总是遇到同样的问题。
编辑:我在代码中添加了 actionbar 类,以防出现错误我会进一步解释我所做的是我有一个带有 3 个按钮的窗口图像有文字“检查,使用项目,睡眠”。
我有 3 张图像,我可以将它们切换到图像中每个按钮上的文本呈黄色的位置 我还有一个透明的矩形图像,每个按钮的大小。我想要完成的是,当我将鼠标悬停在按钮上时,它会切换图像,使其显示一个发光的按钮。我让它按照我预期的方式工作,除了当我在调整窗口大小时添加代码来调整游戏大小时,它只会在鼠标悬停在按钮起始位置上时更改图像。
更新:我在这里添加了一个指向我的谷歌驱动器文件夹的链接,其中包含整个代码和资产,以防有人想在那里的 Python IDE 中重现它以获得更实际的外观。 https://drive.google.com/drive/folders/1EnAZrMG4omJwMkss8z3QvVZfYcPidG9a?usp=sharing
import pygame
import sys
from pygame.locals import *
from config import *
from graphics import *
from gameobjects import *
from guiobjects import *
pygame.init()
win = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT),HWSURFACE|DOUBLEBUF|RESIZABLE)
resizescreen = win.copy()
pygame.display.set_caption("TESTGAME")
clock = pygame.time.Clock()
class actionbar(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.inactive = False
self.examine = False
self.useitem = False
self.sleep = False
def draw_AB(self,win):
if self.examine == True:
win.blit(examineBar,(self.x,self.y))
elif self.useitem == True:
win.blit(useitemBar,self.y))
elif self.sleep == True:
win.blit(sleepBar,self.y))
else:
win.blit(actionBar,self.y))
class Button(object):
def __init__(self,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.image.load("ASSETS/transbutton.png")
def draw_button(self,win):
win.blit(self.image,self.y,self.width,self.height))
def on_button(self,mouse):
if mouse[0] > self.x and mouse[0] < self.x + self.width:
if mouse[1] > self.y and mouse[1] < self.y + self.height:
return True
return False
def draw_grid():
for x in range(0,WIN_WIDTH,TILESIZE):
pygame.draw.line(win,GREY,(x,0),WIN_HEIGHT))
for y in range(0,WIN_HEIGHT,(0,y),(WIN_WIDTH,y))
def redrawGameWindow():
resizescreen.fill((0,0))
draw_grid()
actbar.draw_AB(resizescreen)
examine.draw_button(resizescreen)
useitem.draw_button(resizescreen)
sleep.draw_button(resizescreen)
adv.draw(resizescreen)
win.blit(pygame.transform.scale(resizescreen,win.get_rect().size),0))
pygame.display.flip()
pygame.display.update()
#mainloop
adv = player(300,410,32,32)
actbar = actionbar(0,480,704,98)
examine = Button(75,512,150,30)
useitem = Button(291,30)
sleep = Button(505,30)
run = True
while run:
clock.tick(12)
for event in pygame.event.get():
mouse = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = False
if event.type == VIDEORESIZE:
win = pygame.display.set_mode(event.size,HWSURFACE|DOUBLEBUF|RESIZABLE)
if event.type == pygame.MOUSEMOTION:
if examine.on_button(mouse):
actbar.examine = True
actbar.useitem = False
actbar.sleep = False
elif useitem.on_button(mouse):
actbar.examine = False
actbar.useitem = True
actbar.sleep = False
elif sleep.on_button(mouse):
actbar.examine = False
actbar.useitem = False
actbar.sleep = True
else:
actbar.examine = False
actbar.useitem = False
actbar.sleep = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and adv.x > adv.vel:
adv.x -= adv.vel
adv.left = True
adv.right = False
adv.up = False
adv.down = False
elif keys[pygame.K_RIGHT] and adv.x < 700 - adv.width - adv.vel:
adv.x += adv.vel
adv.left = False
adv.right = True
adv.up = False
adv.down = False
elif keys[pygame.K_UP] and adv.y > adv.vel:
adv.y -= adv.vel
adv.left = False
adv.right = False
adv.up = True
adv.down = False
elif keys[pygame.K_DOWN] and adv.y < 475 - adv.height - adv.vel:
adv.y += adv.vel
adv.left = False
adv.right = False
adv.up = False
adv.down = True
else:
adv.left = False
adv.right = False
adv.up = False
adv.down = False
adv.walkCount = 0
redrawGameWindow()
pygame.quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。