Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy1
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
9a9a9730
authored
Nov 18, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
da115b14
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
3 deletions
snake.py
snake.py
View file @
9a9a9730
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
pygame
.
init
()
my_font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
18
)
a_x
=
240
a_x
=
240
a_y
=
180
a_y
=
180
a1
=
0
a1
=
0
a2
=
0
a2
=
0
score
=
0
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
...
@@ -56,13 +58,17 @@ while True:
...
@@ -56,13 +58,17 @@ while True:
else
:
else
:
y
+=
30
y
+=
30
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
# 将背景图画上去
# 将背景图画上去
if
a_x
==
x
and
a_y
==
y
:
if
a_x
==
x
and
a_y
==
y
:
a1
=
random
.
randint
(
0
,
21
)
a1
=
random
.
randint
(
0
,
21
)
a2
=
random
.
randint
(
0
,
15
)
a2
=
random
.
randint
(
0
,
15
)
a_x
=
a1
*
30
a_x
=
a1
*
30
a_y
=
a2
*
30
a_y
=
a2
*
30
score
+=
10
else
:
position
.
pop
(
0
)
if
(
x
,
y
)
in
position
[
0
:
-
1
]
:
break
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
screen
.
blit
(
snake_head
,
position
[
-
1
])
...
@@ -72,6 +78,9 @@ while True:
...
@@ -72,6 +78,9 @@ while True:
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
a_x
,
a_y
))
screen
.
blit
(
food
,
(
a_x
,
a_y
))
# 刷新画面
# 刷新画面\
info
=
"Score:"
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
0
))
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
3
)
\ 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