如何在Pygame中为图像添加点击事件?

问题描述:

我有以下代码。它基本上从文件夹应用程序及其子文件夹获取所有图像。我的问题是,我正试图添加一个点击事件到所有的图像做同样的事情。基本上“exec("apps/" + apps[app_count] + "/app.py")”如何在Pygame中为图像添加点击事件?

# -*- coding: utf-8 -*- 
from pygame import * 
import os 
import pygame 
import time 
import random 
import sys 

_image_library = {} 

class SeedOS(): 

    def home(self): 
     (width, height) = (240, 320) 
     screen = pygame.display.set_mode((width, height)) 
     pygame.display.set_caption('Seed OS') 
     pygame.font.init() 
     Font30 = pygame.font.SysFont('Arial', 30) 
     WHITE = (255,255,255) 
     BLACK = (0,0,0) 
     screen.fill(WHITE) 
     apps = os.walk("apps").next()[1] 
     app_count = 0 
     icon_width = 15 
     icon_height = 0 
     max_width = 155 
     pygame.display.flip() 
     while True: 
      while app_count < len(apps): 
       print apps[app_count] 
       image = pygame.image.load("apps/" + apps[app_count] + "/app.png").convert() 
       screen.blit(image, (icon_width, icon_height)) 
       icon_width+=70 
       if icon_width > max_width: 
        icon_width = 15 
        icon_height +=70 
       app_count += 1 
      time2 = time.strftime('%H:%M:%S') 
      pygame.display.flip() 
      pygame.draw.rect(screen,BLACK,(0,290,240,30)) 
      clock = Font30.render(time2, False, WHITE) 
      screen.blit(clock,(60,288)) 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.quit() 

phone = SeedOS() 
phone.home() 

这是检查所有的在文件夹“应用程序”的东西

while app_count < len(apps): 
        print apps[app_count] 
        image = pygame.image.load("apps/" + apps[app_count] + "/app.png").convert() 
        screen.blit(image, (icon_width, icon_height)) 
        icon_width+=70 
        if icon_width > max_width: 
         icon_width = 15 
         icon_height +=70 
        app_count += 1 

,并且从每个文件夹追加全部图像的代码的一部分。我想每个图标点击,执行它的“app.py”,如每个应用程序文件夹中有两个文件:“app.png”和“app.py”。

您可以在apps列表中添加每一个形象的坐标,然后使用这些坐标与pygame.mouse.get_pos()方法:

while True: 
      while app_count < len(apps): 
       print apps[app_count] 
       apps[app_count] = (apps[app_count], icon_width, icon_height) # Adding coordinates to the list 
       image = pygame.image.load("apps/" + apps[app_count][0] + "/app.png").convert() # Adding an index to find the image in the tuple 
       screen.blit(image, (icon_width, icon_height)) 
       icon_width+=70 
       if icon_width > max_width: 
        icon_width = 15 
        icon_height +=70 
       app_count += 1 
      time2 = time.strftime('%H:%M:%S') 
      pygame.display.flip() 
      pygame.draw.rect(screen,BLACK,(0,290,240,30)) 
      clock = Font30.render(time2, False, WHITE) 
      screen.blit(clock,(60,288)) 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.quit() 
       if event.type == pygame.MOUSEBUTTONDOWN: 
        if event.button == 1: # MOUSEBUTTONLEFT 
         for a in apps: 
          if a[1] < pygame.mouse.get_pos()[0] < a[1]+IMAGEWIDTH and a[2] < pygame.mouse.get_pos()[1] < a[2] + IMAGEHEIGHT: 
           # Instruction to launch ("apps/" + a[0] + "/app.py") 

所有你需要做的是定义为宽度,从你的图标高度(如果pygame.Surface.get_size()对于每个应用程序都不相同,则可以使用pygame.Surface.get_size()做什么),并用正确的语法替换最后一行。在窗户上,您可以使用:

os.system("apps\\"+a[0]+"\\app.py") # of course you should import os first 
+0

谢谢。它像一个魅力。你救了我的一天:-) –