手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

在Ubuntu 18.04 LTS上Python模块pygame安装

[日期:2019-03-18] 来源:Linux社区  作者:雪峰流云 [字体:大 中 小]

简介

Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。

效果截图如下

在Ubuntu 18.04 LTS上Python模块pygame安装 

安装

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo pip install pygame
[sudo] linuxidc 的密码:
The directory '/home/linuxidc/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/linuxidc/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pygame
  Downloading https://files.pythonhosted.org/packages/26/38/947e70c170e03f0e0aee61ae2e2157278d6146447d2b8cc8c46be5986b3e/pygame-1.9.4-cp27-cp27mu-manylinux1_x86_64.whl (12.0MB)
    100% |████████████████████████████████| 12.0MB 27kB/s
Installing collected packages: pygame
Successfully installed pygame-1.9.4

在Ubuntu 18.04 LTS上Python模块pygame安装

验证

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html

在Ubuntu 18.04 LTS上Python模块pygame安装

在命令行上输入python,再import pygame, 如果没有报错,说明python安装成功。

示例

下面借用网上的例子展示一下效果,给出《飘雪》的动画效果,背景音乐是陈慧娴那首《飘雪》
#!/usr/bin/env python
#coding: utf-8
#description: 制作下雪特效
#refer: https://github.com/crossin/snippet/blob/master/snow/snow.py
#date:2019-01-08
 
 
import pygame
import random
 
 
# 初始化pygame
pygame.init()
 
#可以配上点背景音乐
pygame.mixer.init()
pygame.mixer.music.load('飘雪.mp3')                                                                                                                                     
pygame.mixer.music.play()
 
# 根据背景图片的大小,设置屏幕长宽
SIZE = (1364, 569)
 
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("飘雪")
bg = pygame.image.load('snow.jpg')
 
# 雪花列表
snow_list = []
 
# 初始化雪花:[x坐标, y坐标, x轴速度, y轴速度]
for i in range(200):
    x = random.randrange(0, SIZE[0])
    y = random.randrange(0, SIZE[1])
    sx = random.randint(-1, 1)
    sy = random.randint(3, 6)
    snow_list.append([x, y, sx, sy])
 
clock = pygame.time.Clock()
 
# 游戏主循环
done = False
while not done:
    # 消息事件循环,判断退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
 
    # 黑背景/图片背景
    # screen.fill((0, 0, 0))
    screen.blit(bg, (0, 0))
 
    # 雪花列表循环
    for i in range(len(snow_list)):
        # 绘制雪花,颜色、位置、大小
        pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3]-3)
 
        # 移动雪花位置(下一次循环起效)
        snow_list[i][0] += snow_list[i][2]
        snow_list[i][1] += snow_list[i][3]
 
        # 如果雪花落出屏幕,重设位置
        if snow_list[i][1] > SIZE[1]:
            snow_list[i][1] = random.randrange(-50, -10)
            snow_list[i][0] = random.randrange(0, SIZE[0])
 
    # 刷新屏幕
    pygame.display.flip()
    clock.tick(20)
 
# 退出
pygame.quit()

在Ubuntu 18.04 LTS上Python模块pygame安装

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址:https://www.linuxidc.com/Linux/2019-03/157561.htm

linux
相关资讯       Python模块  pygame模块 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款