Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_diy3
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
672e21f2
authored
Nov 19, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
32f1aeed
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
2 deletions
snake.py
snake.py
View file @
672e21f2
# 导入pygame、locals模块
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
pygame
.
init
()
# 初始化FPSCLOCK
FPSCLOCK
=
pygame
.
time
.
Clock
()
FPSCLOCK
=
pygame
.
time
.
Clock
()
# 设置xy变量
x
=
240
x
=
240
y
=
120
y
=
120
# 创建一个窗口
# 设置位置列表
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
240
,
120
)]
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
240
,
120
)]
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#
背景
#
导入背景、角色素材
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
left
=
pygame
.
image
.
load
(
"left.png"
)
left
=
pygame
.
image
.
load
(
"left.png"
)
...
@@ -19,8 +27,10 @@ down = pygame.image.load("down.png")
...
@@ -19,8 +27,10 @@ down = pygame.image.load("down.png")
food
=
pygame
.
image
.
load
(
'apple.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
# 初始化蛇头判断变量
setheading
=
"right"
setheading
=
"right"
# 初始化蛇头方向变量
snake_tou
=
left
snake_tou
=
left
while
True
:
while
True
:
...
@@ -29,6 +39,7 @@ while True:
...
@@ -29,6 +39,7 @@ while True:
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
# 判断键盘情况
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
setheading
=
"right"
setheading
=
"right"
...
@@ -42,6 +53,7 @@ while True:
...
@@ -42,6 +53,7 @@ while True:
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
setheading
=
"down"
setheading
=
"down"
snake_tou
=
down
snake_tou
=
down
# 蛇头根据方向变换
if
snake_tou
==
right
:
if
snake_tou
==
right
:
x
+=
30
x
+=
30
elif
snake_tou
==
left
:
elif
snake_tou
==
left
:
...
...
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