平台游戏中的加电变量

时间:2016-01-20 23:52:03

标签: pygame

我制作了一款平台游戏。其中一个闪电灯是一个闪电箭,旨在提高你的速度。但是,当我告诉它增加速度时,它会增加速度但是让我的玩家移动而不用按下我的箭头键。有帮助吗?而且我知道你看不到它,但我在main中包含了player.update等,但是我没有显示它,所以代码会缩短。

由于

class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width = 65, height = 35):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.hspeed,self.vspeed = 0,0
    self.speed = 2
    self.Jump = 10

    self.images=[]
    r0 = pygame.image.load("Images\Player\i1.png")
    r1 = pygame.image.load("Images\Player\i2.png")
    r2 = pygame.image.load("Images\Player\i3.png")
    r3 = pygame.image.load("Images\Player\i4.png")
    self.hurt = pygame.image.load("Images\Player\Hurt.png")
    self.images.append(r0)
    self.images.append(r1)
    self.images.append(r2)
    self.images.append(r3)

    self.rotatedimages = []
    rr0 = pygame.transform.flip(r0 ,True, False)
    rr1 = pygame.transform.flip(r1 ,True, False)
    rr2 = pygame.transform.flip(r2 ,True, False)
    rr3 = pygame.transform.flip(r3 ,True, False)
    self.rotatedimages.append(rr0)
    self.rotatedimages.append(rr1)
    self.rotatedimages.append(rr2)
    self.rotatedimages.append(rr3)

    self.deadimages = [self.hurt]

    self.gravity = 0.35
    self.index = 0
    self.image = self.images[self.index]
    self.rect = pygame.Rect(self.x,self.y,width,height)

    self.TimeNum=0
    self.TimeTarget=10
    self.Timer = 0


def update(self, event = None):
    self.calcgravity()

    self.rect.x += self.hspeed
    self.rect.y += self.vspeed

    #Walking animation of animation when left or right key is pressed.
    key = pygame.key.get_pressed()
    if self.Timer >= 0:
        if key[pygame.K_RIGHT]:
            self.TimeNum+=1
            if self.TimeNum == self.TimeTarget:
                self.index +=1
                if self.index >= len(self.images):
                    self.index = 0
                self.image = self.images[self.index]

                self.TimeNum = 0

        if key[pygame.K_LEFT]:
            self.TimeNum+=1
            if self.TimeNum == self.TimeTarget:
                self.index +=1
                if self.index >= len(self.rotatedimages):
                    self.index = 0
                self.image = self.rotatedimages[self.index]
                self.TimeNum = 0
    # if you go outside the boundaries of the game.
    if player.rect.x < 0:
        GameOver()


 def move(self, hspeed, vspeed):
    self.hspeed += hspeed
    self.vspeed += vspeed


 def Level1Coll(self,PowerUps):

    PowerUpsCollision = pygame.sprite.spritecollide(self,PowerUps,True )
    for speedboost in PowerUpsCollision:
        self.speed = 3


def main()

GameExit = False

while GameExit==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.move(-player.speed,0)
            if event.key == pygame.K_RIGHT:
                player.move(player.speed,0)
            if event.key == pygame.K_UP:
                player.move(0,-player.Jump)
                JumpSound.play()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.move(player.speed,0)
            if event.key == pygame.K_RIGHT:
                player.move(-player.speed,0)
            if event.key == pygame.K_UP:
                player.move(0,0)

1 个答案:

答案 0 :(得分:1)

您将self.speed设置为等于3,这通常意味着精灵将以3的速度自行移动。而是创建一个新变量:

extra_speed = 0

并将其应用于精灵速度的增加:

self.rect.y += (self.vspeed + extra_speed)

现在当精灵没有上电时,没有任何变化,因为速度没有增加。但是当精灵确实获得上电时,只需将extra_speed更改为3,并在精灵上电时重置为0。要解决即时运行的问题,请在检查运行之前使用另一个变量来确定何时运行并使用它。

running = False
#A few lines later into your update function
if running:
    self.rect.x += self.hspeed
    self.rect.y += (self.vspeed + extra_speed)

上面的代码表示如果running为True,则适当地移动精灵。否则,请不要移动精灵。