本人python初学者,在各种找教程和尝试之后整理pygame 安装方法
说明:
使用系统:Windows 10 64位
python 版本:python 3.8
python安装位置:G:\Python38
以上信息请根据你所使用的电脑有所区别
查询升级pip 版本
pip list
结果:
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
结果中有警告说需要升级pip版本
执行一下命令 升级pip版本
python -m pip install --upgrade pip
结果:
Collecting pip
Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 12kB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
下载安装 pygame
点击以下网址下载 pygame(根据安装的python版本和系统版本选择下载相应的文件)
pypi.org
我的python是python 3.8 系统是windows 10 64位 所以我下载的是pygame-1.9.6-cp38-cp38-win_amd64.whl
下载后的pygame-1.9.6-cp38-cp38-win_amd64.whl文件放到python的安装位置G:\Python38
在CMD进入G:\Python38文件夹
使用pip 安装pygame
G:
cd Python38
pip install pygame-1.9.6-cp38-cp38-win_amd64.whl
结果
Processing g:\python38\pygame-1.9.6-cp38-cp38-win_amd64.whl
Installing collected packages: pygame
Successfully installed pygame-1.9.6
安装完成
为了验证是否安装成功 可以再次输入
pip list
得到如下结果的话就说明pygame安装成功
Package Version
---------- -------
pip 20.0.2
pygame 1.9.6
setuptools 41.2.0
一段黑客帝国二进制代码雨的小程序 看一下效果
import random,pygame
FONT_PX = 20
pygame.init()
winSur = pygame.display.set_mode((720,540))
font = pygame.font.SysFont("123", 25)
bg_suface = pygame.Surface((720,540),flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 16))
winSur.fill((0, 0, 0))
letter = ['1', '0', '1', '1', '1', '0', '0', '0', '1',
'0', '1', '0', '1', '0', '0', '1', '1', '0', '0', '0', '1', '1','1', '0', '1', '0', '0', '1', '0', '1']
texts = [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(10)]
columns = int(720/ FONT_PX)
drops = [0 for i in range(columns)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
chang = pygame.key.get_pressed()
if (chang[32]):
exit()
pygame.time.delay(33)
winSur.blit(bg_suface, (0, 0))
for i in range(len(drops)):
text = random.choice(texts)
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * 10 > 540 or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()