我正在編寫一個遊戲代碼(學校項目)。 我有一個類與對象的不同圖像。 我想要的是創造一個碰撞的條件。例如,如果火災圖像與地球圖像發生碰撞,則會獲得新圖像。 我該怎麼辦?類中的碰撞
謝謝!
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()
檢查像素是否觸摸/重疊?數學?可能有些內置pygame的函數?你應該發佈你已經嘗試過的,我們將幫助調試。 – Tony
這裏是我的代碼 –