2016-05-11 62 views
0

我正在编写一个游戏代码(学校项目)。 我有一个类与对象的不同图像。 我想要的是创造一个碰撞的条件。例如,如果火灾图像与地球图像发生碰撞,则会获得新图像。 我该怎么办?类中的碰撞

谢谢!

import pygame 
from pygame.locals import * 

pygame.init() 

screen = pygame.display.set_mode((900,800)) 
pygame.display.set_caption("Tiny Alchemist") 
clock = pygame.time.Clock() 
FPS = 90 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
ALTWHITE = (240, 240, 240) 
clear = False   # Flag that shows if the user wants to clear the screen 
class Element(pygame.Surface): 
    def __init__(self, image, xpos, ypos): 
     self.image = image 
     self.xpos = xpos 
     self.ypos = ypos 
     self.width = image.get_width() 
     self.height = image.get_height() 
     self.selected = False 
     self.visible = False 
     self.rect = pygame.Rect(xpos, ypos, image.get_width(), image.get_height()) 

    def move(self, move): 
     self.xpos += move[0] 
     self.ypos += move[1] 
     self.rect = pygame.Rect(self.xpos, self.ypos, self.width, self.height) 

class Recycle(pygame.Surface): 
    def __init__(self, xpos, ypos): 
     self.image = pygame.image.load("ElementIcon/recycle.png").convert_alpha() 
     self.image = pygame.transform.scale(self.image, (75, 75)) 
     self.image.set_colorkey(BLACK) 
     self.xpos = xpos 
     self.ypos = ypos 
     self.rect = pygame.Rect(xpos, ypos, self.image.get_width(), self.image.get_height()) 

class PanelElements(pygame.Surface): 
    def __init__ (self, image, xpos, ypos): 
     self.image = image 
     self.xpos = xpos 
     self.ypos = ypos 
     self.rect = pygame.Rect(xpos, ypos, image.get_width(), image.get_height()) 
     self.clicked = False 


def init(): 
    global ImageList 
    global Panel 
    global recycle 

    fire = pygame.image.load("ElementIcon/fire.png").convert_alpha() 
    fire = pygame.transform.scale(fire, (50, 69)) 
    fire.set_colorkey(BLACK) 
    fire_mini = pygame.transform.scale(fire, (40,50)) 
    fire_mini.set_colorkey(ALTWHITE) 

    earth = pygame.image.load("ElementIcon/earth.png").convert_alpha() 
    earth = pygame.transform.scale(earth, (50, 69)) 
    earth.set_colorkey(BLACK) 
    earth_mini = pygame.transform.scale(earth, (40, 50)) 
    earth_mini.set_colorkey(ALTWHITE) 

    water = pygame.image.load("ElementIcon/water.png").convert_alpha() 
    water = pygame.transform.scale(water, (50, 69)) 
    water.set_colorkey(BLACK) 
    water_mini = pygame.transform.scale(water, (40, 50)) 
    water_mini.set_colorkey(ALTWHITE) 

    wind = pygame.image.load("ElementIcon/wind.png").convert_alpha() 
    wind = pygame.transform.scale(wind, (50, 69)) 
    wind.set_colorkey(BLACK) 
    wind_mini = pygame.transform.scale(wind, (40, 50)) 
    wind_mini.set_colorkey(ALTWHITE) 

    energy = pygame.image.load("ElementIcon/energy.png").convert_alpha() 
    energy = pygame.transform.scale(energy, (50, 69)) 
    energy.set_colorkey(BLACK) 
    energy_mini = pygame.transform.scale(energy, (40, 50)) 
    energy_mini.set_colorkey(ALTWHITE) 

    recycle = Recycle(650, 718) 

    fire_mini_obj = PanelElements(fire_mini, 750, 10) 
    earth_mini_obj = PanelElements(earth_mini, 750, 60) 
    water_mini_obj = PanelElements(water_mini, 750, 110) 
    wind_mini_obj = PanelElements(wind_mini, 750, 160) 

    fire_obj = Element(fire, 362, 460) 
    fire_obj.visible = True 
    earth_obj = Element(earth, 300, 410) 
    earth_obj.visible = True 
    water_obj = Element(water, 365, 350) 
    water_obj.visible = True 
    wind_obj = Element(wind, 420, 409) 
    wind_obj.visible = True 


    Panel = []     #adding elements to the list 
    Panel.append(fire_mini_obj) 
    Panel.append(earth_mini_obj) 
    Panel.append(water_mini_obj) 
    Panel.append(wind_mini_obj) 

    ImageList =[]    #adding elements to the list 
    ImageList.append(fire_obj) 
    ImageList.append(earth_obj) 
    ImageList.append(water_obj) 
    ImageList.append(wind_obj) 

def run(): 

    global done 
    done = False 
    while done == False: 
     check_events() 
     update() 
     clock.tick(60)  

def check_events(): 
    global done 
    global ImageList 
    global Panel 
    global recycle 
    global clear 

    mouse_pos = pygame.mouse.get_pos() 

    for event in pygame.event.get(): 

     if event.type == pygame.QUIT: 
      done = True 
      print("User quits the game :(") 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       done = True 
       print("Game stopped early by user :(") 

     if event.type == pygame.MOUSEBUTTONDOWN: 
      for im in ImageList: 
       if im.rect.collidepoint(mouse_pos) and (im.visible == True): 
        im.selected = True 
      if recycle.rect.collidepoint(mouse_pos): 
       clear = True 
      for mini in Panel: 
       if mini.rect.collidepoint(mouse_pos): 
        mini.clicked = True 




     if event.type == pygame.MOUSEBUTTONUP: 
      for im in ImageList: 
       if im.rect.collidepoint(mouse_pos) and im.visible == True: 
        im.selected = False 


     if event.type == pygame.MOUSEMOTION: 
      for im in ImageList: 
       if im.rect.collidepoint(mouse_pos) and im.selected and (im.visible == True): 
        xmv = event.rel[0] 
        ymv = event.rel[1] 

        if event.buttons[0]: 
         if xmv < 0: 
          if im.xpos > 0: 
           im.move((xmv,0)) 

         elif event.rel[0] > 0: 
          if im.xpos < screen.get_width(): 
           im.move((xmv,0)) 

         elif ymv < 0: 
          if im.ypos > 0: 
           im.move((0,ymv)) 

         elif event.rel[1] > 0: 
          if im.ypos < screen.get_height(): 
           im.move((0,ymv)) 
    #pygame.display.update() 

def update(): 
    global ImageList 
    global Panel 
    global recycle 
    global clear 
    screen.fill(WHITE) 

    #Update the screen with drawings 
    for im in ImageList: 
     if im.visible == True: 
      screen.blit(im.image, (im.xpos, im.ypos)) 
    pygame.draw.rect(screen, ALTWHITE, (740, 0, 160, 800), 0) 

    for mini in Panel: 
     screen.blit(mini.image, (mini.xpos, mini.ypos))     

    screen.blit(recycle.image, (recycle.xpos, recycle.ypos)) 
    if (clear == True): 
     for im in ImageList: 
      im.visible = False 
    clear = False 

    for i in range(0,len(Panel)): 
     if Panel[i].clicked == True: 

      Panel[i].clicked = False 

    pygame.display.update() 

if __name__=="__main__": 
    init() 
    run() 
pygame.quit()  
+0

检查像素是否触摸/重叠?数学?可能有些内置pygame的函数?你应该发布你已经尝试过的,我们将帮助调试。 – Tony

+0

这里是我的代码 –

回答

0

或许是这样的:

earth = pygame.image.load("earth.png").convert() 
earthRect = earth.get_rect() 
fire = pygame.image.load("fire.png").convert() 
fireRect = fire.get_rect() 

if earth.colliderect(fire): 
    earth = pygame.image.load("thirdimage.png") 

与前四行定义我们的图像和用于检测碰撞RECT对象,最后两行检测的碰撞和改变图像文件

+0

如果对象发生碰撞,我想创建一个类的新对象,并将它添加到列表 –

+0

你可以在if语句中做所有的事情 – goomba