首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为雪碧创造跳跃的游戏?

如何为雪碧创造跳跃的游戏?
EN

Stack Overflow用户
提问于 2018-11-13 20:44:58
回答 1查看 191关注 0票数 0

我正在努力寻找一种方法,让雪碧跳进游戏。当我基本上运行这个程序的时候,精灵会直接跳到屏幕的顶部,我只想让它正常跳一跳。跳转是在一个类中,是一个函数。我使用KEYDOWN来检查何时按下它将其向下移动。

代码语言:javascript
运行
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2018-11-13 22:04:05

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

代码语言:javascript
运行
复制
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设置为负值:

代码语言:javascript
运行
复制
def jump(self):
    if self.on_ground:  # This prevents air jumps.
        self.on_ground = False
        self.speed_y = -25
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53289226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档