python碰撞检测麻烦

时间:2016-06-28 17:30:55

标签: python pygame

作为具有一些基本知识的python的初学者,我决定通过制作程序来扩展这些知识。在一个这样的程序中,我发现碰撞检测存在困难。因为我使用的是.xml文件,所以我的大多数想法似乎都不起作用。该计划如下。任何人都可以帮我解决碰撞问题吗?

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

1 个答案:

答案 0 :(得分:0)

实现碰撞检测的最简单方法是使用Sprites。在线有几个很好的教程可以引导您完成Sprites以及如何使用它们进行简单的碰撞检测。 :-)对于刚开始使用Pygame的人来说,这是一个很棒的功能。

以下是关于精灵和碰撞检测的一些好视频:

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

另外,如果你想在Pygame中找到关于碰撞和物理的精彩教程,请查看彼得的网站!这就是我从我的Pygame碰撞检测程序中学到的东西,并将其作为基础!

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