繁体   English   中英

如何在pygame中为精灵创建跳跃

[英]how do you create jumping in pygame for a sprite

我正在努力寻找使精灵在pygame中跳跃的方法。 当我基本上运行程序时,现在精灵会直接移到屏幕顶部,我只希望它进行正常的跳转。 跳转在一个类中并且是一个函数。 我已经使用KEYDOWN来检查何时按下它来向下移动。

import pygame
import random
import sys


pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()  # A clock to limit the frame rate.
pygame.display.set_caption("this game")


class Background:
    picture = pygame.image.load("C:/images/dunes.jpg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        # Blit the picture onto the screen surface.
        # `self.picture` not just `picture`.
        screen.blit(self.picture, (self.xpos, self.ypos))


class Monster:
    picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
    # If you want to move continuously you need to set these
    # attributes to the desired speed and then add them to
    # self.xpos and self.ypos in an update method that should
    # be called once each frame.
        self.speed_x = 0
        self.speed_y = 0

    def update(self):
        # Call this method each frame to update the positions.
        self.xpos += self.speed_x
        self.ypos += self.speed_y

    # Not necessary anymore.
    def move_left(self):
        self.xpos -= 5  # -= not = -5 (augmented assignment).

    def move_right(self):
        self.xpos += 5  # += not = +5 (augmented assignment).

    def jump(self): #vvvvvvv this is the part i do not know how to fix 


    # What do you want to do here?
    #for x in range(1, 10):
        #self.ypos -= 1  # -= not =-
        # pygame.display.show()  # There's no show method.

    #for x in range(1, 10):
        #self.ypos += 1
        # pygame.display.show()

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


class Enemy:  # Use upper camelcase names for classes.
    picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def teleport(self):
        self.xpos = random.randint(1, 1280)
        self.ypos= random.randint(1, 720)

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


# Create the instances before the while loop.
ice = Background(0, 0)  # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)

while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # Check if the `event.type` is KEYDOWN first.
        elif event.type == pygame.KEYDOWN:
            # Then check which `event.key` was pressed.
            if event.key == pygame.K_d:
                #hammerhood.move_right()
                hammerhood.speed_x = 5
            elif event.key == pygame.K_a:
                #hammerhood.move_left()
                hammerhood.speed_x = -5
            elif event.key == pygame.K_w:
                hammerhood.jump()
        elif event.type == pygame.KEYUP:
            # Stop moving when the keys are released.
            if event.key == pygame.K_d and hammerhood.speed_x > 0:
                hammerhood.speed_x = 0
            elif event.key == pygame.K_a and hammerhood.speed_x < 0:
                hammerhood.speed_x = 0

    # Update the game.
    hammerhood.update()
    fish.teleport()

    if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
        hammerhood.speed_x = 0

    # Draw everything.
    ice.draw()  # Blit the background to clear the screen.
    hammerhood.draw()
    fish.draw()
    pygame.display.flip()
    clock.tick(60)  # Limit the frame rate to 60 FPS.

只需添加一个GRAVITY常量并将其添加到self.speed_y每帧中,以加快播放器的速度。

GRAVITY = .9  # Define this in the global scope.


# The Monster's update method.
def update(self):
    self.speed_y += GRAVITY  # Accelerate downwards.
    self.xpos += self.speed_x
    self.ypos += self.speed_y

    # Stop falling at the bottom of the screen.
    if self.ypos >= 520:
        self.ypos = 520
        self.speed_y = 0
        self.on_ground = True

开始跳跃时,将self.speed_y设置为负值:

def jump(self):
    if self.on_ground:  # This prevents air jumps.
        self.on_ground = False
        self.speed_y = -25

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM