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
655f19e2
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
4 changed files
with
93 additions
and
3 deletions
MINGLIUB.TTC
STXINGKA.TTF
snake.py
补课.py
MINGLIUB.TTC
0 → 100644
View file @
655f19e2
File added
STXINGKA.TTF
0 → 100644
View file @
655f19e2
File added
snake.py
View file @
655f19e2
import
pygame
from
pygame
import
locals
import
random
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 创建一个窗口
??
\ No newline at end of file
screen
=
pygame
.
display
.
set_mode
((
800
,
600
))
clock
=
pygame
.
time
.
Clock
()
# 设置时钟
# 载入图片
bg
=
pygame
.
image
.
load
(
'bg.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
apple
=
pygame
.
image
.
load
(
'apple.png'
)
x
,
y
=
240
,
330
position
=
[(
240
,
420
),
(
240
,
390
),
(
240
,
360
),
(
x
,
y
)]
# 存储三个身体和一个蛇头位置
setheading
=
'right'
#设置朝向
head
=
right
apple_x
=
360
apple_y
=
300
score
=
0
font
=
pygame
.
font
.
Font
(
'STXINGKA.TTF'
,
20
)
speed
=
5
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
'down'
:
setheading
=
'up'
head
=
up
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
setheading
=
'down'
head
=
down
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
setheading
=
'left'
head
=
left
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
'left'
:
setheading
=
'right'
head
=
right
if
setheading
==
'up'
:
y
-=
30
elif
setheading
==
'down'
:
y
+=
30
elif
setheading
==
'left'
:
x
-=
30
else
:
x
+=
30
if
(
x
,
y
)
in
position
:
#判断舌头是否碰到身体
exit
()
position
.
append
((
x
,
y
))
# 每次把元组 x,y添加到列表的末尾
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
(
random
.
randint
(
1
,
26
))
*
30
-
30
apple_y
=
(
random
.
randint
(
1
,
20
))
*
30
-
30
score
+=
10
speed
+=
5
else
:
position
.
pop
(
0
)
# 删除列表的第一项
if
x
<
0
or
x
>
800
or
y
<
0
or
y
>
570
:
#判断碰到墙壁
exit
()
# 图片渲染
screen
.
blit
(
bg
,
(
0
,
0
))
screen
.
blit
(
head
,
position
[
-
1
])
screen
.
blit
(
apple
,
(
apple_x
,
apple_y
))
for
i
in
range
(
len
(
position
)
-
1
):
# 便利列表 取出三个身体位置
screen
.
blit
(
body
,
position
[
i
])
text
=
font
.
render
(
'分数:'
+
str
(
score
),
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
20
,
20
))
pygame
.
display
.
update
()
# 刷新画面 一次刷新就可以
clock
.
tick
(
speed
)
# 帧数控制速度
补课.py
0 → 100644
View file @
655f19e2
import
pygame
import
pygame
from
pygame
import
locals
pygame
.
init
()
scrren
=
pygame
.
display
.
set_mode
((
800
,
600
))
#加载图片
bg
=
pygame
.
image
.
load
(
'bg.png'
)
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
#渲染
scrren
.
blit
(
bg
,(
0
,
0
))
#刷新画面
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