2016-06-28 133 views
0

作爲python的初學者,有一些基礎知識,我決定通過編寫程序來擴展這些知識。在一個這樣的程序中,我發現碰撞檢測有困難。因爲我使用的是.xml文件,所以我的大部分想法似乎都不起作用。該程序如下。任何人都可以幫我碰撞檢測?python碰撞檢測問題

import pygame, time, os ,random, time 
from pytmx import load_pygame 

#____________________Variables_______________________ 

white = (255,255,255) 
black = (0,0,0) 
blue = (0,191,255) 


#___________________Screen Init______________________ 

screenSize = (640,704) 
screen = pygame.display.set_mode(screenSize) 
pygame.display.set_caption("game") 
screen.fill(black) 
FPS = 30 
fpsClock = pygame.time.Clock() 

#____________________Game Data_______________________ 

os.chdir("D:\Games\mageQuest\Data\Images") 
Character_img = pygame.image.load("Character.png") 


#__________________Commands Init_____________________ 

class game: 
    def __init__(self): 
     self.loop_ = 0 
     self.game_disp = 0 

    def loop(self): 
     self.loop_ = self.loop_ + 1 
     if self.loop_ == 50: 
      self.game_disp = 1 
      self.loop_ = 0 

class load_map: 
    def __init__(self): 
     global map_name  
     global gameMap 
     global x 
     global y 
     x = 100 
     y = 69 
     os.chdir("D:\Games\mageQuest\Data\Maps") 
     map_list = os.listdir() 
     print(map_list) 
     #map_name = input("Enter map name: ") 
     map_name = "map1"   
     gameMap = load_pygame(map_name+".tmx")  
    def create(self, x, y): 
     image_none = gameMap.get_tile_image(1,1,0) 
     images0 = [] 
     x_tmp = x 
     y_tmp = y 
     screen.fill(blue) 
     while y_tmp - 44 != y: 
      y_tmp = y_tmp + 1 
      x_tmp = x 
      while x_tmp - 40 != x:   
       image = gameMap.get_tile_image(x_tmp,y_tmp,0) 
       images0.append(image) 
       x_tmp = x_tmp + 1   
     i = 0 
     for y_ in range(44): 
      for x_ in range(40): 
       if images0[i] != gameMap.get_tile_image(1,1,0): 
        screen.blit(images0[i],(x_ * 16, y_ * 16)) 
       i = i + 1 
     screen.blit(Character_img, (320, 352)) 

class character: 
    def __init__(self, x, y): 
     self.x = y 
     self.y = x 
    def movement(self, key, x, y): 
     print(x,y) 
     if event.key == pygame.K_w: 
      character.movement_1(x, y) 
     if event.key == pygame.K_a: 
      character.movement_2(x, y) 
     if event.key == pygame.K_s: 
      character.movement_3(x, y) 
     if event.key == pygame.K_d: 
      character.movement_4(x, y) 
    def movement_1(self, x, y): 
     y = y - 1 
     time.sleep(0.25) 
     self.dir = 1 
     self.x = x 
     self.y = y 
    def movement_2(self, x, y): 
     x = x - 1 
     time.sleep(0.25) 
     self.dir = 2 
     self.x = x 
     self.y = y 
    def movement_3(self, x, y): 
     y = y + 1 
     time.sleep(0.25) 
     self.dir = 3 
     self.x = x 
     self.y = y 
    def movement_4(self, x, y): 
     x = x + 1 
     time.sleep(0.25) 
     self.dir = 4 
     self.x = x 
     self.y = y 



#____________________Main Loop_______________________ 



mageQuest = game() 
world = load_map() 
world.create(x, y) 
character = character(x, y) 


game_running = True 
while game_running: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      game_running = False 

    if event.type == pygame.KEYDOWN: 
     character.movement(event.key, x, y) 
     x = character.x 
     y = character.y 

    mageQuest.loop() 


    if mageQuest.game_disp == 1: 
     pygame.display.update() 
     fpsClock.tick(FPS) 
     game_disp = 0 
     world.create(x, y) 

pygame.quit() 

在此先感謝。 :D

回答

0

實現碰撞檢測的最簡單方法是使用Sprites。有幾個很好的在線教程可以引導您瞭解Sprites以及如何使用它們進行簡單的碰撞檢測。 :-)這對於剛開始使用Pygame的人來說是一個很棒的功能。

這裏是精靈和碰撞檢測一些好的視頻:

https://www.youtube.com/watch?v=NO31P0UvutM

另外,如果你想在Pygame中的碰撞和物理學偉大的教程看看彼得的網站!這是我從中學到的,並且基於我的Pygame碰撞檢測程序的很多!

http://www.petercollingridge.co.uk/pygame-physics-simulation

+0

感謝您的幫助! – Madworks