2016-06-28 132 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