首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyGame起伏不定

问PyGame起伏不定
EN

Stack Overflow用户
提问于 2020-03-27 07:14:40
回答 2查看 48关注 0票数 2

我是pygame的新手,我正在看如何开始使用它的一些教程,我写了这个基本代码,它只有移动和跳跃“功能”,但它真的不稳定,我不认为它是我的硬件(Macbook Pro 2018)。有人知道发生了什么吗?

下面是代码:

代码语言:javascript
复制
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))

    pygame.display.set_caption("First PyGame")

    width = 40
    height = 60
    x = win_width/2
    y = win_height/2
    vel = 10

    isJump = False
    jumpCount = 10

    run = True
    while run:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("adios putito")
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x -= vel
        if keys[pygame.K_RIGHT] and x < win_width - width - vel:
            x+= vel
        if not (isJump):
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN]and y < win_height - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

        win.fill((0,0,0))
        pygame.draw.rect(win,(255,255,255), (x,y,width,height))
        pygame.display.update()

    pygame.quit()
EN

回答 2

Stack Overflow用户

发布于 2020-03-27 07:24:39

这条线

代码语言:javascript
复制
pygame.time.delay(100)

正在延迟您的脚本毫秒,删除该行,它应该可以很好地工作!

结果:

代码语言:javascript
复制
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))

    pygame.display.set_caption("First PyGame")

    width = 40
    height = 60
    x = win_width/2
    y = win_height/2
    vel = 10

    isJump = False
    jumpCount = 10

    run = True
    while run:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("adios putito")
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x -= vel
        if keys[pygame.K_RIGHT] and x < win_width - width - vel:
            x+= vel
        if not (isJump):
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN]and y < win_height - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

        win.fill((0,0,0))
        pygame.draw.rect(win,(255,255,255), (x,y,width,height))
        pygame.display.update()

    pygame.quit()
票数 3
EN

Stack Overflow用户

发布于 2020-03-27 16:52:29

这仍然是一个好主意,有一个游戏时钟,以延迟您的程序足够保持一个持久的帧率。您可以使用pygame的pygame.time.Clock对象并使用它的tick()方法来延迟您的游戏。tick方法接受一个整数,它表示您希望游戏达到的FPS上限。如果您的游戏运行速度慢于您输入的FPS值,则不会发生延迟。

代码语言:javascript
复制
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))

    pygame.display.set_caption("First PyGame")
    clock = pygame.time.Clock()

    width = 40
    height = 60
    x = win_width/2
    y = win_height/2
    vel = 10

    isJump = False
    jumpCount = 10

    run = True
    while run:
        # Delay your game to try and keep 60 FPS.
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("adios putito")
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x -= vel
        if keys[pygame.K_RIGHT] and x < win_width - width - vel:
            x+= vel
        if not (isJump):
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN]and y < win_height - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

        win.fill((0,0,0))
        pygame.draw.rect(win,(255,255,255), (x,y,width,height))
        pygame.display.update()

    pygame.quit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60877635

复制
相关文章

相似问题

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