2017-09-26 319 views
0

我有以下代碼。它基本上從文件夾應用程序及其子文件夾獲取所有圖像。我的問題是,我正試圖添加一個點擊事件到所有的圖像做同樣的事情。基本上「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」。

回答

0

您可以在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

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

相關問題