Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_2
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c98d4347
authored
Oct 16, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
c6188a1e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
2 deletions
1.py
snake.py
1.py
0 → 100644
View file @
c98d4347
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
bg
=
pygame
.
image
.
load
(
"bg.png"
)
right
=
pygame
.
image
.
load
(
"right.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
food
=
pygame
.
image
.
load
(
"apple.png"
)
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
right
,(
240
,
60
))
screen
.
blit
(
body
,(
210
,
60
))
screen
.
blit
(
body
,(
180
,
60
))
screen
.
blit
(
body
,(
180
,
30
))
screen
.
blit
(
food
,(
300
,
180
))
pygame
.
display
.
update
()
C
:
\
Users
\
bell
\
AppData
\
Roaming
\
Microsoft
\
Windows
\
Start
Menu
\
Programs
\
Python
3.9
\ No newline at end of file
snake.py
View file @
c98d4347
#导入pygame模块
import
pygame
from
pygame
import
locals
'''
1.设置窗口
pygame.display.set_mode(size)
2.size是一个元组类型的参数,使用小括号标志
3.元组是一个一经创建就无法修改的数据类型
4.如果只使用一个模块中的某一个方法/函数 导入时可以只导入这一部分 from 模块名 import 函数名
5.在游戏进行的过程中会发生各种的事件
6.窗口关闭 locals.QUIT 点击窗口右上角的关闭即可触发
7.获取发生的事件 pygame.event.get()
8.pygame坐标起始点(0,0)在左上角 在pygame中横轴用x表示,越向右,x坐标越大;竖轴用y表示,越向下,y坐标越大。
9.将一个图片显示在窗口上叫做 渲染
10.如果想要渲染一张图片,就需要导入 导入的格式: pygame.image.load("图片名.后缀名"),为了方便操作可以赋值给一个变量
11.导入图片算做pygame的初始化内容,要放到while True 的上边 while True 里边的内容叫做游戏逻辑
12. 渲染的格式: screen.blit(要渲染的对象,(x,y)) 归属于游戏逻辑 放到while True的里边
13. 先渲染的内容会被后渲染的内容所覆盖 背景图 --> 贪吃蛇的头 --> 贪吃蛇的身体 --> 苹果
14.渲染是从左上角开始渲染
15.刷新画面 pygame.display.update()
'''
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 创建一个窗口
??
\ No newline at end of file
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#导入图片
bg
=
pygame
.
image
.
load
(
"bg.png"
)
#1.导入背景图bg,导入贪吃蛇朝右的头right,贪吃蛇的身体body,苹果food
while
True
:
#遍历事件/遍历发生的事件
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
#渲染背景图,和贪吃蛇的头
#渲染三个身体,身体要呈L形
#渲染苹果,苹果只要在格子里就可以
pygame
.
display
.
update
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment