当前位置:网站首页>pygame学习计划(1)
pygame学习计划(1)
2022-08-10 05:29:00 【程序猿阿转】
目录
第一章 认识pygame
1.1 pygame是干什么用的
我们编写python代码时,有没有想过:只能做一些文字游戏,不能像我们常玩的《我的世界》、《植物大战僵尸》一样,有一个窗口可以玩游戏。pygame就是可以用python代码做一个游戏窗口来编写游戏。
1.2制作窗口
1.2.1导库
做游戏时,要导这些库:
1.pygame
可以帮助我们制作游戏
2.sys
使玩家点击叉叉时退出游戏
import pygame
import sys
1.2.2设置窗口
导库之后需要设置一些关于窗口的变量,而且别忘了初始化pygame
import pygame
import sys
pygame.init() #初始化pygame
SIZE = GAMEWIDTH, GAMEHEIGHT = 480, 300 #修改窗口尺寸
screen = pygame.display.set_mode(SIZE) #创建窗口
pygame.display.set_caption("game") #游戏名称
但是这样不能一直显示窗口,所以还要加入游戏主循环
import pygame
import sys
pygame.init() #初始化pygame
SIZE = GAMEWIDTH, GAMEHEIGHT = 480, 300 #修改窗口尺寸
screen = pygame.display.set_mode(SIZE) #创建窗口
pygame.display.set_caption("game") #游戏名称
while True: #游戏主循环
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #退出游戏
sys.exit()
pygame.display.update() #更新画面
这里我们只能看到一个黑乎乎的画面,这太乏味了。
所以我们要给窗口填充颜色,要用到fill函数
import pygame
import sys
pygame.init() #初始化pygame
SIZE = GAMEWIDTH, GAMEHEIGHT = 480, 300 #修改窗口尺寸
screen = pygame.display.set_mode(SIZE) #创建窗口
pygame.display.set_caption("game") #游戏名称
GRAY = (200, 200, 200)
while True: #游戏主循环
screen.fill(GRAY) #为窗口填充颜色
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #退出游戏
sys.exit()
pygame.display.update() #更新画面
可以看到现在窗口是灰色的
1.3添加游戏矩形
创建好了游戏创口,现在要在游戏窗口内绘制矩形
import pygame
import sys
pygame.init() #初始化pygame
SIZE = GAMEWIDTH, GAMEHEIGHT = 480, 300 #修改窗口尺寸
screen = pygame.display.set_mode(SIZE) #创建窗口
pygame.display.set_caption("game") #游戏名称
GRAY = (200, 200, 200)
GREEN = (0, 255, 0)
rect = pygame.Rect(GAMEWIDTH//2, GAMEHEIGHT//2, 50, 50) #创建矩形
while True: #游戏主循环
screen.fill(GRAY) #为窗口填充颜色
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #退出游戏
sys.exit()
pygame.draw.rect(screen, GREEN, rect) #绘制矩形
pygame.display.update() #更新画面
我们在游戏中创建了一个矩形,他看起来是这样的:
边栏推荐
- 从GET切换为POST提交数据的方法
- What are the common commands of mysql
- 栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
- 论文精度 —— 2017 ACM《Globally and Locally Consistent Image Completion》
- Pony语言学习(九)——泛型与模式匹配(终章)
- Thread.sleep, Thread.yield role explanation
- EasyGBS连接mysql数据库提示“can’t connect to mysql server”,该如何解决?
- pytorch框架学习(5)torchvision模块&训练一个简单的自己的CNN (二)
- Qiskit官方文档选译之量子傅里叶变换(Quantum Fourier Transform, QFT)
- Introduction to curl command
猜你喜欢
How to use Apifox's Smart Mock function?
Kubernetes:(十七)Helm概述、安装及配置
论文精度 —— 2016 CVPR 《Context Encoders: Feature Learning by Inpainting》
Linear Algebra (4)
An article to master the entire JVM, JVM ultra-detailed analysis!!!
pytorch框架学习(3)torch.nn.functional模块和nn.Module模块
pytorch框架学习(2)使用GPU训练
树莓派入门(4)LED闪烁&呼吸灯
strongest brain (1)
pytorch框架学习(4)torchvision模块&训练一个简单的自己的CNN (一)
随机推荐
pytest测试框架
pytorch learning
Nexus_Warehouse Type
每周推荐短视频:探索AI的应用边界
pytorch 学习
常用工具系列 - 常用正则表达式
从GET切换为POST提交数据的方法
Important transformation and upgrading
Matlab simulation of multi-factor house price prediction based on BP neural network
Kubernetes:(十六)Ingress的概念和原理
Attention candidates for the soft exam! The detailed registration process for the second half of 2022 is coming!
oracle rac 11g安装执行root.sh时报错
基于Qiskit——《量子计算编程实战》读书笔记(六)
SSM框架整合实例
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
OneFlow源码解析:算子指令在虚拟机中的执行
暑期学前作业
SEO搜索引擎优化
Zhongang Mining: Strong downstream demand for fluorite
顺序表的删除,插入和查找操作