1

我一直在学习 Pygame(Python 版本 3.3.1)上的这个在线教程,并且已经到了我的精灵可以跳跃的地步,但在任一方向上只能走几个像素。我真的很想继续使用这段代码,因为我喜欢它的结构(在 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)   
4

1 回答 1

0

我不确定,但看起来你只是让玩家在空中(跳跃)时从一侧移动到另一侧

如果我是对的,您需要使您的播放器能够左右移动,即使self.resting = True

如果你这样做会发生什么:

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

问题是玩家会从一边移动到另一边,但是当你跳跃并按下右或左键时,玩家会飞过来而不是稳定地跳跃和摔倒

所以你需要弄清楚如何包括*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 

第一个是在空中,第二个是当玩家在地面上时

试试看,告诉我它是否有效

我在我的电脑上试过它并且它有效,但我可能使用了不同的艺术,这也可能是问题,因为它可能会在某些东西后面出现,所以检查一切

祝你好运!!

于 2013-06-09T20:23:09.060 回答