【问题标题】:Disappearance of my character due collision. pygame我的角色因碰撞而消失。游戏
【发布时间】:2014-12-17 03:41:01
【问题描述】:

自从我第一次尝试编写代码以来,我就遇到了这个问题,我的问题在于此类内部的碰撞系统。

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

【问题讨论】:

    标签: python class pygame character collision


    【解决方案1】:

    从此更改您的代码

    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
    

    您正在设置框的右侧和左侧,而您应该设置顶部和底部

    【讨论】:

    • 你建议我做什么?
    • 其中一个 if 语句需要检查它是否也等于 0。您需要决定是哪一个,这通常无关紧要。 tutorialspoint.com/python/comparison_operators_example.htm> 这是比较运算符的列表,我建议您使用 = 这可能无法解决问题,但最好的做法是确保您的 if else 语句涵盖整个范围必要时的数字
    • 另外,我注意到的另一件事是,当您检查if self.cambio_y &gt; 0: 时,您设置的是矩形的左右位置而不是顶部和底部,因此矩形可能是,从某种意义上说,将自己传送到顶墙的远端到你看不到的地方
    • 这正是正在发生的事情。我将代码翻译成英文以便更好地理解。你给我的列表显示错误。
    • 更新:我修正了你所说的错误,但我的角色仍然消失。现在在各个方面。
    猜你喜欢
    • 1970-01-01
    • 2013-12-02
    • 2018-05-26
    • 2017-12-28
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多