由于碰撞我的角色消失了。 pygame的

时间:2014-12-17 03:41:01

标签: python class pygame character collision

我有这个问题,因为我第一次尝试编写代码,我的问题是这个类中的碰撞系统。

def updates(self, wall):
        self.hero = hero
        self.wall = wall
        self.rect.x += self.change_x

        list_of_collisions = pygame.sprite.spritecollide(self, self.wall, False)
        for wall in list_of_collision:
            if self.change_x > 0:
                self.rect.right = wall.rect.left
            elif self.change_x < 0:
                self.rect.right = pared.rect.left

        self.rect.y += self.change_y
        list_of_collision = pygame.sprite.spritecollide(self, self.wall, False)
        for block in list_of_collision:
            if self.change_y > 0:
                self.rect.right = wall.rect.left
            else:
                self.rect.left = wall.rect.right 

我的角色与一面墙完全碰撞,但在其他墙上消失了。我试图自己解决,我不会问我是否没有尝试过我所想过的一切。

这是完整的代码(抱歉有点长)

#imports 
import pygame
import random

#colors and images
WHITE = (255, 255, 255)
BLACK = (0,0,0)
BLUE = (0, 0, 255)

#Class and fuctions
class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y, high, width):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([high, width])
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

class Prota(pygame.sprite.Sprite):
    change_x = 0
    change_y = 0

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([15, 15])
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.x = 450
        self.rect.y = 300
    def velocity(self, x, y):
            self.rect.x += x
            self.rect.y += y    
    def update(self, wall):
        self.hero = hero
        self.wall = wall
        self.rect.x += self.change_x

        list_of_collisions = pygame.sprite.spritecollide(self, self.wall, False)
        for wall in list_of_collisions:
            if self.change_x > 0:
                self.rect.right = wall.rect.left
            elif self.change_x < 0:
                self.rect.right = wall.rect.left

        self.rect.y += self.change_y
        list_of_collisions = pygame.sprite.spritecollide(self, self.wall, False)
        for block in list_of_collisions:
            if self.change_y > 0:
                self.rect.right = wall.rect.left
            else:
                self.rect.left = wall.rect.right


#creating room
dimensions = [900, 600]
screen = pygame.display.set_mode(dimensions)
pygame.display.set_caption("The oblivion of the voices")

#variables
    #walls
list_walls= pygame.sprite.Group()
wallr = Wall(850, 0, 50, 600)
walll = Wall(0, 0, 50, 600)
walltop = Wall(0, 0, 900, 50)
wallbot = Wall(0, 550, 900, 50)
lista_paredes.add(paredd,paredi, paredar, paredab)


 #protagonist
hero = pygame.sprite.Group()
Aaron = Prota()
hero.add(Aaron)

    #list of everything
list_of_lists = pygame.sprite.Group()
list_of_lists.add(walll, wallr, walltop, wallbot, Aaron)





#creating a clock
clock = pygame.time.Clock()

#initializing pygame
pygame.init()

#The game start
done = False


#-------------------------------------------------- HERE IS WHERE THE GOOD STUFF IS------------------------------
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
#LOGIC OF THE GAME
    #keyboard for moving charac
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                Aaron.velocity (-10, 0)
            elif event.key == pygame.K_RIGHT:
                Aaron.velocity (10, 0)
                print(Aaron.cambio_x, Aaron.rect.x)
            elif event.key == pygame.K_DOWN:
                Aaron.velocity (0, 10)
            elif event.key == pygame.K_UP:
                Aaron.velocity (0, -10)
    x = Aaron.rect.y
    y = Aaron.rect.x
    cam_x = Aaron.change_x
    cam_y = Aaron.change_y
    print("[",x,"*",y,"-","]", "[" ,cam_x,"^", cam_y,"&","]")
    Aaron.update(list_walls)


    #mouse for touching ( not in used yet)
    pos = pygame.mouse.get_pos()
    royx = pos[0]
    royy = pos[1]




#Draw
    screen.fill(WHITE)
    list_of_lists.draw(screen)

 #FInal ditails
    clock.tick(20)
    pygame.display.flip()
pygame.quit()

如果您注意到,我还尝试打印变量x,y,cam_x,cam_y,这就是控制台显示的内容。当我按下右键时,就会出现值0,200。

[ 300 * 200 - ] [ 0 ^ 0 & ]
[ 300 * 150 - ] [ 0 ^ 0 & ]
[ 300 * 150 - ] [ 0 ^ 0 & ]
[ 300 * 150 - ] [ 0 ^ 0 & ]
[ 300 * 150 - ] [ 0 ^ 0 & ]
0 200

1 个答案:

答案 0 :(得分:0)

从此

更改您的代码
for block in list_of_collision:
            if self.change_y > 0:
                self.rect.right = wall.rect.left
            else:
                self.rect.left = wall.rect.right 

到这个

for block in list_of_collision:
                if self.change_y > 0:
                    self.rect.bottom = wall.rect.top
                elif :
                    self.rect.top = wall.rect.bottom

当您设置顶部和底部时,您正在设置框的左右两侧