在Ubuntu 16.04.5 LTS上安装pygame模块
生活随笔
收集整理的這篇文章主要介紹了
在Ubuntu 16.04.5 LTS上安装pygame模块
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)介
Pygame是跨平臺(tái)Python模塊,專為電子游戲設(shè)計(jì),包含圖像、聲音。建立在SDL基礎(chǔ)上,允許實(shí)時(shí)電子游戲研發(fā)而無(wú)需被低級(jí)語(yǔ)言(如機(jī)器語(yǔ)言和匯編語(yǔ)言)束縛。
?
安裝
sudo pip install pygame
驗(yàn)證
在命令行上輸入python,再import pygame, 如果沒(méi)有報(bào)錯(cuò),說(shuō)明python安裝成功。
示例
下面借用網(wǎng)上的例子展示一下效果,給出《飄雪》的動(dòng)畫(huà)效果,背景音樂(lè)是陳慧嫻那首《飄雪》
#!/usr/bin/env python
#coding: utf-8
#description: 制作下雪特效
#refer: https://github.com/crossin/snippet/blob/master/snow/snow.py
#date:2019-01-08import pygame
import random# 初始化pygame
pygame.init()#可以配上點(diǎn)背景音樂(lè)
pygame.mixer.init()
pygame.mixer.music.load('飄雪.mp3')
pygame.mixer.music.play()# 根據(jù)背景圖片的大小,設(shè)置屏幕長(zhǎng)寬
SIZE = (1364, 569)screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("飄雪")
bg = pygame.image.load('snow.jpg')# 雪花列表
snow_list = []# 初始化雪花:[x坐標(biāo), y坐標(biāo), 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()# 游戲主循環(huán)
done = False
while not done:# 消息事件循環(huán),判斷退出for event in pygame.event.get():if event.type == pygame.QUIT:done = True# 黑背景/圖片背景# screen.fill((0, 0, 0))screen.blit(bg, (0, 0))# 雪花列表循環(huán)for i in range(len(snow_list)):# 繪制雪花,顏色、位置、大小pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3]-3)# 移動(dòng)雪花位置(下一次循環(huán)起效)snow_list[i][0] += snow_list[i][2]snow_list[i][1] += snow_list[i][3]# 如果雪花落出屏幕,重設(shè)位置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()
效果截圖如下
需要的素材如下:
背景音樂(lè)就不提供了。
參考文獻(xiàn)
[1].https://my.oschina.net/crossin/blog/1848215
[2].https://www.jb51.net/article/127484.htm
總結(jié)
以上是生活随笔為你收集整理的在Ubuntu 16.04.5 LTS上安装pygame模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在Ubuntu 16.0.4.5 LTS
- 下一篇: 十种经典排序算法精粹(c语言版本)