Pygame雪碧可以跳,但不能走路

时间:2013-06-09 19:32:56

标签: python collision-detection sprite

我在Pygame(Python版本3.3.1)上一直在研究this online tutorial,并且已经达到了我的精灵可以跳跃的程度,但是在任何一个方向上只能走几个像素。我真的很想继续使用这段代码,因为我喜欢它的结构(不是Main方法中的很多代码)。有人能发现可能导致我的精灵卡住的原因吗?

import pygame

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
green    = (   0, 255,   0)
red      = ( 255,   0,   0)

class Player(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super(Player, self).__init__(groups)
        self.image = pygame.image.load('Images\player1.png')
        self.rect = pygame.rect.Rect((50, 650), self.image.get_size())
        self.resting = False
        self.dy = 0 #dy represents change in y velocity

    def update(self, dt, game):
        last = self.rect.copy()
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.x -= 300 * dt
        if key[pygame.K_RIGHT]:
            self.rect.x += 300 * dt
        #if key[pygame.K_UP]:
        #    self.rect.y -= 300 * dt
        #if key[pygame.K_DOWN]:
        #    self.rect.y += 300 * dt

        if self.resting and key[pygame.K_SPACE]:
            self.dy = -500 #If space bar is pressed, increase velocity.
        self.dy = min(400, self.dy + 40) #Speed capped at 400. Gravity set at 40.
        self.rect.y += self.dy * dt

        new = self.rect
        self.resting = False
        for cell in pygame.sprite.spritecollide(self, game.walls, False):
            #self.rect = last
            cell = cell.rect
            if last.right <= cell.left and new.right > cell.left:
                new.right = cell.left               
            if last.left >= cell.right and new.left < cell.right:
                new.left = cell.right
            if last.bottom <= cell.top and new.bottom > cell.top:
                #if you hit something while jumping, stop.
                self.resting = True
                new.bottom = cell.top
                self.dy = 0
            if last.top >= cell.bottom and new.top < cell.bottom:
                new.top = cell.bottom
                self.dy = 0 #If you hit the floor while jumping, stop


class Game(object):
    def main(self, screen):
        clock = pygame.time.Clock()
        dt = clock.tick(30)
        #image = pygame.image.load('Images\player1.gif')
        background = pygame.image.load('Images\_rec_bg.png')
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)
        self.walls = pygame.sprite.Group()
        block = pygame.image.load('Images\dia_tile.png')
        for x in range(0, 800, 20):
            for y in range(0, 800, 20):
                if x in (0, 800-20) or y in (0, 800-20):
                    wall = pygame.sprite.Sprite(self.walls)
                    wall.image = block
                    wall.rect = pygame.rect.Rect((x, y), block.get_size())
        sprites.add(self.walls)

        running = True
        while running:
            clock.tick(30) #run no more than 30 time per second
            dt - clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT or \
                    (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                        running = False


            #sprites.update()
            #sprites.update(dt / 1000.)
            #screen.fill(black)
            sprites.update(dt / 1000., self)
            #screen.blit(image, (320, 240)) #Transfer to video RAM
            screen.blit(background, (0,0))
            sprites.draw(screen)
            pygame.display.flip() #Dispaly to Screen

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 800))
    Game().main(screen)   

1 个答案:

答案 0 :(得分:0)

我不确定,但如果玩家在空中(跳跃),看起来你只是让玩家左右移动

如果我是对的,即使self.resting = True

,你也需要让玩家左右移动

如果您执行以下操作会发生什么:

if key[pygame.K_LEFT]:
    self.rect.x -= 75
if key[pygame.K_RIGHT]:
    self.rect.x += 75 

关于这一点的是玩家将从侧面移动到sdie但是当你跳跃并向右或向左按下时,玩家将全部飞行而不是进入稳定的跳跃和下降

所以你需要弄清楚如何包含*dt来调节球员在空中的运动,但同时让球员能够在地面上移动

尝试使用两组if语句:

if key[pygame.K_LEFT]:
    self.rect.x -= 300 * dt
if key[pygame.K_RIGHT]:
    self.rect.x += 300 *dt

if key[pygame.K_LEFT]and self.resting:
    self.rect.x -= 50 
if key[pygame.K_RIGHT]and self.resting:
    self.rect.x += 50 

第一个用于空中,第二个用于当玩家在地面时

尝试并告诉我它是否有效

我在我的电脑上尝试了它并且它有效但我可能使用了不同的艺术也可能是问题,因为它可能是背后的东西,所以检查一切

祝你好运!!