当前位置:网站首页>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() #更新画面
我们在游戏中创建了一个矩形,他看起来是这样的:
边栏推荐
猜你喜欢
Zhongang Mining: Strong downstream demand for fluorite
awk of the Three Musketeers of Shell Programming
接口调试还能这么玩?
实战小技巧19:List转Map List的几种姿势
一文带你搞懂OAuth2.0
What are the common commands of mysql
How to use Apifox's Smart Mock function?
Qiskit官方文档选译之量子傅里叶变换(Quantum Fourier Transform, QFT)
oracle rac 11g安装执行root.sh时报错
Jenkins 如何玩转接口自动化测试?
随机推荐
Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
MySql's json_extract function processes json fields
CSDN Markdown 之我见代码块 | CSDN编辑器测评
The time for flinkcdc to read pgsql is enlarged. Does anyone know what happened? gmt_create':1
基于BP神经网络的多因素房屋价格预测matlab仿真
Why are negative numbers in binary represented in two's complement form - binary addition and subtraction
SQL database field to append to main table
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
pytorch框架学习(9)torchvision.transform
Hezhou ESP32C3 +1.8"tft network clock under Arduino framework
openGauss源码,在window系统用VSCode维护吗?
scikit-learn机器学习 读书笔记(一)
Practical skills 19: Several postures of List to Map List
[Thesis Notes] Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
Qiskit学习笔记(三)
顺序表的删除,插入和查找操作
Interface debugging also can play this?
Qiskit 学习笔记1
Get started with the OAuth protocol easily with a case
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记