开发者社区 问答 正文

python怎么安装pygame

python怎么安装pygame

展开
收起
保持可爱mmm 2019-12-10 16:40:59 1161 分享 版权
1 条回答
写回答
取消 提交回答
  • Pygame 是一种流行的 Python 包,用于编写游戏-鼓励学生学习编程,同时创建有趣的东西。 Pygame 在新窗口中显示图形,因此它将

    无法在 WSL 的命令行方法下运行。 但是,如果您通过本教程中所述的 Microsoft Store 安装了 Python,它将正常工作。

    1.安装 Python 后,请通过键入 python -m pip install -U pygame --user,从命令行(或 VS Code 中的终端)安装 pygame。

    2.通过运行示例游戏来测试安装: python -m pygame.examples.aliens

    3.一切正常,游戏就会打开一个窗口。 完成播放后,关闭窗口。

    下面介绍了如何开始编写自己的游戏。(编辑器为vs code)

    1打开 PowerShell (或 Windows 命令提示符)并创建一个名为 "弹跳" 的空文件夹。 导航到此文件夹并创建一个名为 "bounce.py" 的

    +文件。 在 VS Code 中打开文件夹:

    mkdir bounce

    cd bounce

    new-item bounce.py

    code .

    使用 "VS Code",输入以下 Python 代码(或复制并粘贴):

    import sys, pygame

    pygame.init()

    size = width, height = 640, 480

    dx = 1

    dy = 1

    x= 163

    y = 120

    black = (0,0,0)

    white = (255,255,255)

    screen = pygame.display.set_mode(size)

    while 1:

    for event in pygame.event.get():
    
        if event.type == pygame.QUIT: sys.exit()
    
    
    
    x += dx
    
    y += dy
    
    
    
    if x < 0 or x > width:   
    
        dx = -dx
    
    
    
    if y < 0 or y > height:
    
        dy = -dy
    
    
    
    screen.fill(black)
    
    
    
    pygame.draw.circle(screen, white, (x,y), 8)
    
    
    
    pygame.display.flip()
    

    问题来源于python学习网

    2019-12-10 16:41:24
    赞同 展开评论