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
121c825c
authored
Apr 18, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
b88808e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
10 deletions
snake.py
text.py
snake.py
View file @
121c825c
import
pygame
import
pygame
import
random
from
pygame
import
locals
from
pygame
import
locals
#初始化pygame,为使用硬件作准备
#初始化pygame,为使用硬件作准备
pygame
.
init
()
pygame
.
init
()
head_x
=
0
head_y
=
90
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
fps
=
pygame
.
time
.
Clock
()
#载入图片
head_x
=
0
postion
=
[(
0
,
0
),(
0
,
30
),(
0
,
60
),(
head_x
,
head_y
)]
heading
=
'right'
food_x
=
60
food_y
=
120
score
=
0
# 载入图片
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'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
up
=
pygame
.
image
.
load
(
'up.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'
)
#身体
head
=
right
#创建一个窗口
#创建一个窗口
scree
=
pygame
.
display
.
set_mode
((
660
,
480
))
scree
=
pygame
.
display
.
set_mode
((
660
,
480
))
while
True
:
while
True
:
...
@@ -16,16 +32,55 @@ while True:
...
@@ -16,16 +32,55 @@ while True:
#print(event)
#print(event)
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_DOWN
and
heading
!=
'up'
:
heading
=
'down'
head
=
down
if
event
.
key
==
locals
.
K_UP
and
heading
!=
'down'
:
heading
=
'up'
head
=
up
if
event
.
key
==
locals
.
K_RIGHT
and
heading
!=
'left'
:
heading
=
'right'
head
=
right
if
event
.
key
==
locals
.
K_LEFT
and
heading
!=
'right'
:
heading
=
'left'
head
=
left
if
heading
==
'right'
:
head_x
+=
30
elif
heading
==
'left'
:
head_x
-=
30
elif
heading
==
'up'
:
head_y
-=
30
else
:
head_y
+=
30
if
head_x
==
food_x
and
head_y
==
food_y
:
score
+=
1
food_x
=
random
.
randint
(
0
,
21
)
*
30
food_y
=
random
.
randint
(
0
,
15
)
*
30
else
:
postion
.
pop
(
0
)
if
head_x
<
0
or
head_x
>
630
or
head_y
<
0
or
head_y
>
450
:
exit
()
if
(
head_x
,
head_y
)
in
postion
:
exit
()
postion
.
append
((
head_x
,
head_y
))
info
=
'Score:'
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
255
))
#将背景图画上去
#将背景图画上去
scree
.
blit
(
background
,(
0
,
0
))
scree
.
blit
(
background
,(
0
,
0
))
#将贪吃蛇的头画上去
#将贪吃蛇的头画上去
scree
.
blit
(
right
,(
240
,
120
))
scree
.
blit
(
head
,(
postion
[
-
1
]))
#将苹果画上去
for
i
in
range
(
len
(
postion
)
-
1
):
scree
.
blit
(
food
,(
360
,
300
))
scree
.
blit
(
body
,
postion
[
i
])
#将贪吃蛇的身体画上去
scree
.
blit
(
food
,(
food_x
,
food_y
))
scree
.
blit
(
body
,(
210
,
120
))
scree
.
blit
(
text
,(
0
,
0
))
scree
.
blit
(
body
,(
180
,
120
))
scree
.
blit
(
body
,(
180
,
90
))
#刷新画面
#刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
\ No newline at end of file
fps
.
tick
(
5
)
\ No newline at end of file
text.py
0 → 100644
View file @
121c825c
for
i
in
range
(
3
):
print
(
i
)
\ 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