Python 树莓皮上的Pygame选择两次
我正在编写一个程序,该程序显示一个地图,在地图上覆盖多个目标,当鼠标光标位于目标上时,会出现一个“弹出窗口”并显示有关该目标的信息。它似乎在我的PC开发机器上工作,但当我将它移到目标Raspberry Pi时,弹出窗口显示选择两次。这可能与此有关,也可能与此无关,但计时器所用的时间比正常时间长。例如,当我将光标放在目标上时,信息将出现,大约5秒钟后,窗口将闪烁,就像再次选择了它一样,然后再等待5秒钟,即使我暂时将其设置为2秒钟。我还确保光标已从该点移动 这是我的密码:Python 树莓皮上的Pygame选择两次,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在编写一个程序,该程序显示一个地图,在地图上覆盖多个目标,当鼠标光标位于目标上时,会出现一个“弹出窗口”并显示有关该目标的信息。它似乎在我的PC开发机器上工作,但当我将它移到目标Raspberry Pi时,弹出窗口显示选择两次。这可能与此有关,也可能与此无关,但计时器所用的时间比正常时间长。例如,当我将光标放在目标上时,信息将出现,大约5秒钟后,窗口将闪烁,就像再次选择了它一样,然后再等待5秒钟,即使我暂时将其设置为2秒钟。我还确保光标已从该点移动 这是我的密码: import pygame
import pygame, time, ConfigParser, os, sys
from pygame.locals import *
from sys import exit
def MakeBackground( pic_wid , pic_height ):
map_dir = os.path.join('Maps2')
ground = [os.path.join(map_dir , ('Map'+str(image + 1) + '.jpg')) for image in range(19)]
for i in range(19):
ground[i] = pygame.image.load(str(ground[i]))
ground[i] = pygame.transform.scale(ground[i], (int(pic_wid), int(pic_height)))
return ground
def MakeTargets( new_resolution_x , new_resolution_y ):
Config = ConfigParser.ConfigParser()
Config.read("Info.ini")
basicFont = pygame.font.SysFont(None, 48)
targets = {}
for section in Config.sections():
targets[section] = {}
for option in Config.options(section):
targets[section][option] = Config.get(section, option)
image_dir = os.path.join('Images')
for item in targets:
targets[item]['x'] = int((float(targets[item]['x'])*(new_resolution_x/16120.0)))
targets[item]['y'] = int((float(targets[item]['y'])*(new_resolution_y/19000.0)))
targets[item]['name'] = basicFont.render(str(targets[item]['name']), True,(255,255,255))
targets[item]['description'] = os.path.join(image_dir , str(targets[item]['description']))
targets[item]['title'] = os.path.join(image_dir , str(targets[item]['title']))
targets[item]['photo'] = targets[item]['photo'].split(',')
for pic in range(len(targets[item]['photo'])):
targets[item]['photo'][pic] = os.path.join(image_dir , str(targets[item]['photo'][pic]))
targets[item]['photo'][pic] = pygame.image.load(str(targets[item]['photo'][pic]))
targets[item]['description'] = pygame.image.load(str(targets[item]['description']))
targets[item]['title'] = pygame.image.load(str(targets[item]['title']))
return targets
os.chdir(os.path.dirname(sys.argv[0]))
pygame.init()
new_resolution_x = 1500
new_resolution_y = 1000
scale_x = new_resolution_x / 4030.0
scale_y = new_resolution_y / 4750.0
pic_wid = 4030 * scale_x
pic_height = 250 * scale_y
ground = MakeBackground( pic_wid , pic_height )
targets = MakeTargets( new_resolution_x, new_resolution_y )
screensaver_dir = os.path.join('Images' , 'ScreenSaverImage.tga')
speed = [1, 1]
black = 0, 0, 0
white = 250, 250, 250
ball = pygame.image.load(screensaver_dir)
ball = pygame.transform.scale(ball, (150, 300))
ballrect = ball.get_rect()
screen = pygame.display.set_mode((new_resolution_x, new_resolution_y), 0, 32)
pygame.display.set_caption("MapInfo")
pygame.mouse.set_cursor(*pygame.cursors.broken_x)
last_motion = time.time()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEMOTION:
x, y = event.pos
last_motion = time.time()
for item in targets:
if (targets[item]['x']-2) <= x <= (targets[item]['x']+2) and (targets[item]['y']-2) <= y <= (targets[item]['y'] + 2):
pygame.draw.rect(screen, [ 155, 155, 155], (20, 175, 1200, 700))
screen.blit(targets[item]['name'], (25, 200))
screen.blit(targets[item]['title'], (50, 230))
screen.blit(targets[item]['description'], (700, 150))
num_of_pics = len(targets[item]['photo'])
if num_of_pics <= 3:
for pic in range(num_of_pics):
screen.blit(targets[item]['photo'][pic],(150 + pic*335 , 530 ))
if 4 <= num_of_pics <= 5:
for pic in range(int(num_of_pics/2)):
screen.blit(targets[item]['photo'][pic],(120 + pic*335 , 430 ))
screen.blit(targets[item]['photo'][pic+2],(120 + pic*335 , 630 ))
if 6 <= num_of_pics <= 7:
for pic in range(int(num_of_pics/2)):
screen.blit(targets[item]['photo'][pic],(90 + pic*335 , 430 ))
screen.blit(targets[item]['photo'][pic+2],(90 + pic*335 , 630 ))
pygame.display.update()
time.sleep(2)
elapsed = time.time() - last_motion
if elapsed >= 10:
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > new_resolution_x:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > new_resolution_y:
speed[1] = -speed[1]
screen.fill(white)
screen.blit(ball, ballrect)
pygame.display.update()
else:
for i in range(19):
screen.blit(ground[i], (0,i * int(pic_height)))
for item in targets:
pygame.draw.circle(screen, [255, 0, 0], [targets[item]['x'] , targets[item]['y']], 5)
pygame.display.update()
导入pygame、时间、配置解析器、操作系统、系统
从pygame.locals导入*
从系统导入退出
def生成背景(图片宽度、图片高度):
map\u dir=os.path.join('Maps2')
ground=[os.path.join(map_dir,('map'+str(image+1)+'.jpg'))用于范围(19)中的图像)
对于范围(19)中的i:
ground[i]=pygame.image.load(str(ground[i]))
地面[i]=pygame.transform.scale(地面[i],(int(图片宽度),int(图片高度)))
返回地
def生成目标(新分辨率x、新分辨率y):
Config=ConfigParser.ConfigParser()
Config.read(“Info.ini”)
basicFont=pygame.font.SysFont(无,48)
目标={}
对于Config.sections()中的节:
目标[部分]={}
对于Config.options(部分)中的选项:
targets[section][option]=Config.get(section,option)
image\u dir=os.path.join('Images')
目标中的项目:
目标[项目]['x']=int((浮动(目标[项目]['x'])*(新的分辨率\u x/16120.0)))
目标[项目]['y']=int((浮动(目标[项目]['y'])*(新的决议案y/19000.0)))
targets[item]['name']=basicFont.render(str(targets[item]['name']),True,(255255))
targets[item]['description']=os.path.join(image_dir,str(targets[item]['description']))
targets[item]['title']=os.path.join(image_dir,str(targets[item]['title']))
目标[项目]['photo']=目标[项目]['photo'].拆分(','))
对于范围内的图片(len(目标[项目]['photo']):
目标[项目]['photo'][pic]=os.path.join(图像目录,str(目标[项目]['photo'][pic]))
目标[项目]['photo'][pic]=pygame.image.load(目标[项目]['photo'][pic]))
目标[项目]['description']=pygame.image.load(目标[项目]['description']))
目标[项目]['title']=pygame.image.load(目标[项目]['title']))
返回目标
os.chdir(os.path.dirname(sys.argv[0]))
pygame.init()
新的分辨率为1500
新的分辨率为1000
比例x=新的分辨率x/4030.0
比例y=新的分辨率y/4750.0
pic_wid=4030*scale_x
图片高度=250*比例y
地面=背景(图片宽度,图片高度)
目标=生成目标(新分辨率x,新分辨率y)
screensaver_dir=os.path.join('Images','ScreenSaverImage.tga'))
速度=[1,1]
黑色=0,0,0
白色=250250250
ball=pygame.image.load(屏幕保护程序\u目录)
ball=pygame.transform.scale(ball,(150300))
ballrect=ball.get_rect()
screen=pygame.display.set_模式((新分辨率x,新分辨率y),0,32)
pygame.display.set_标题(“MapInfo”)
pygame.mouse.set_cursor(*pygame.cursors.breakedx)
最后一个动作=time.time()
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
退出()
elif event.type==pygame.MOUSEMOTION:
x、 y=event.pos
最后一个动作=time.time()
目标中的项目:
if(目标[项目]['x']-2)