Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy4
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
2b756c4c
authored
Apr 17, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
4dd4abd7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
6 deletions
snake.py
snake2.py
snake.py
View file @
2b756c4c
...
...
@@ -3,7 +3,7 @@ from pygame import locals
import
random
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
myfont
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
15
)
myfont
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
15
)
#设置字体
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
...
...
@@ -58,7 +58,11 @@ while True:
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
random
.
randint
(
1
,
22
)
*
30
-
30
apple_y
=
random
.
randint
(
1
,
16
)
*
30
-
30
score
+=
10
score
+=
10
#贪吃蛇吃到苹果
if
x
<
apple_x
+
30
and
x
>
apple_x
and
y
<
apple_y
-
30
and
y
>
apple_y
:
apple_x
=
random
.
randint
(
1
,
22
)
*
30
-
30
apple_y
=
random
.
randint
(
1
,
16
)
*
30
-
30
score
+=
10
#贪吃蛇吃到苹果
else
:
position
.
pop
(
0
)
#贪吃蛇身体增加
# 将背景图画上去
...
...
@@ -71,9 +75,8 @@ while True:
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
# 刷新画面
info
=
"Score"
+
str
(
score
)
text
=
myfont
.
render
(
info
,
True
,(
0
,
255
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
5
)
\ No newline at end of file
pygame
.
display
.
update
()
# 刷新画面
FPSCLOCK
.
tick
(
10
)
\ No newline at end of file
snake2.py
0 → 100644
View file @
2b756c4c
import
pygame
from
pygame
import
locals
pygame
.
init
()
#初始化
screen
=
pygame
.
display
.
set_mode
((
800
,
600
))
#创建窗口
background
=
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'
)
#载入蛇头
body
=
pygame
.
image
.
load
(
'body.png'
)
#载入蛇身
food
=
pygame
.
image
.
load
(
'apple.png'
)
#载入果实
while
True
:
for
event
in
pygame
.
event
.
get
():
#监听玩家事件
if
event
.
type
==
locals
.
QUIT
:
exit
()
#收到退出事件后退出程序
screen
.
blit
(
background
,(
0
,
0
))
#渲染背景图
screen
.
blit
(
right
,(
240
,
120
))
#渲染蛇头
screen
.
blit
(
food
,(
360
,
300
))
#渲染果实
screen
.
blit
(
body
,(
210
,
120
))
#渲染蛇身
screen
.
blit
(
body
,(
180
,
120
))
#渲染蛇身
screen
.
blit
(
body
,(
180
,
90
))
#渲染蛇身
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