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
0fcd8904
authored
Nov 06, 2020
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
0b913573
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
14 deletions
snake.py
snake.py
View file @
0fcd8904
import
pygame
import
pygame
from
pygame
import
locals
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
((
800
,
600
))
background
=
pygame
.
image
.
load
(
'bg.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
pygame
.
init
()
#初始化
food
=
pygame
.
image
.
load
(
'apple.png'
)
#图片初始化
right
=
pygame
.
image
.
load
(
'right.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
screen
=
pygame
.
display
.
set_mode
((
800
,
600
))
#创建窗口
x
=
180
y
=
180
setheading
=
right
position
=
[(
x
,
y
),(
150
,
180
),(
120
,
180
),(
120
,
150
)]
#设置位置列表
fps
=
pygame
.
time
.
Clock
()
#s设置一个计时器用来记录画面刷新的帧数
while
True
:
for
i
in
pygame
.
event
.
get
():
if
i
.
type
==
locals
.
QUIT
:
exit
()
screen
.
blit
(
background
,(
0
,
0
))
screen
.
blit
(
food
,(
180
,
180
))
screen
.
blit
(
right
,(
150
,
180
))
pygame
.
display
.
update
()
\ No newline at end of file
for
i
in
pygame
.
event
.
get
():
#遍历pygame中的事件,优先处理事件
if
i
.
type
==
locals
.
KEYDOWN
:
if
i
.
key
==
locals
.
K_w
and
setheading
!=
down
:
setheading
=
up
elif
i
.
key
==
locals
.
K_s
and
setheading
!=
up
:
setheading
=
down
elif
i
.
key
==
locals
.
K_a
and
setheading
!=
right
:
setheading
=
left
elif
i
.
key
==
locals
.
K_d
and
setheading
!=
left
:
setheading
=
right
if
i
.
type
==
locals
.
QUIT
:
#如果事件编号和locals中推出的编号一样
exit
()
#终止程序
if
setheading
==
up
:
y
-=
30
if
setheading
==
down
:
y
+=
30
if
setheading
==
right
:
x
+=
30
if
setheading
==
left
:
x
-=
30
screen
.
blit
(
background
,(
0
,
0
))
#画背景
screen
.
blit
(
food
,(
300
,
300
))
#画食物
screen
.
blit
(
setheading
,
position
[
0
])
for
i
in
range
(
1
,
len
(
position
)):
screen
.
blit
(
body
,
position
[
i
])
pygame
.
display
.
update
()
#刷新窗口
position
.
insert
(
0
,(
x
,
y
))
position
.
pop
(
-
1
)
fps
.
tick
(
3
)
#每秒刷新3帧
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