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
d6f759f0
authored
May 13, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
c6188a1e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
3 deletions
snake.py
snake.py
View file @
d6f759f0
import
pygame
from
pygame
import
locals
import
random
#from pygame.locals import *
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 创建一个窗口
??
\ No newline at end of file
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#创建计时器,控制游戏速度
FPS
=
pygame
.
time
.
Clock
()
#加载图片
bg
=
pygame
.
image
.
load
(
'bg.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'
)
#头朝下
apple
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
#创建字体对象
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
x
,
y
=
240
,
90
#初始化贪吃蛇头部的坐标
position
=
[(
180
,
60
),(
180
,
90
),(
210
,
90
),(
x
,
y
)]
#储存贪吃蛇身体和头部坐标
snake_head
=
right
#初始化贪吃蛇头部造型
setheading
=
"right"
#初始化贪吃蛇朝向
apple_x
=
300
apple_y
=
420
score
=
0
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
elif
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
setheading
=
"down"
snake_head
=
down
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
"down"
:
setheading
=
"up"
snake_head
=
up
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
"right"
:
setheading
=
"left"
snake_head
=
left
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
setheading
=
"right"
snake_head
=
right
#设置贪吃蛇头部坐标
if
setheading
==
"right"
:
x
+=
30
#向右移动
elif
setheading
==
"left"
:
x
-=
30
#向左移动
elif
setheading
==
"up"
:
y
-=
30
#向上移动
else
:
y
+=
30
#向下移动
position
.
append
((
x
,
y
))
#追加蛇头移动后的坐标
if
x
==
apple_x
and
y
==
apple_y
:
#如果贪吃蛇吃到苹果,苹果移到随机位置
apple_x
=
random
.
randint
(
0
,
630
/
30
)
*
30
apple_y
=
random
.
randint
(
0
,
450
/
30
)
*
30
score
+=
10
else
:
position
.
pop
(
0
)
#删除最后一段身体的坐标
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
exit
()
#将背景画上去
screen
.
blit
(
bg
,(
0
,
0
))
#将头部画上去
screen
.
blit
(
snake_head
,(
x
,
y
))
#将身体画上去
'''screen.blit(body, (210, 90))
screen.blit(body, (180, 90))
screen.blit(body, (180, 60))'''
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
#将苹果画上去
screen
.
blit
(
apple
,(
apple_x
,
apple_y
))
#绘制分数
info
=
"Score: "
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
#刷新画面
pygame
.
display
.
update
()
FPS
.
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