Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_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
b47261a6
authored
Jul 14, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
16f0d60e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
5 deletions
.py
snake.py
.py
0 → 100644
View file @
b47261a6
import
pygame
import
sys240
120
from
pygame.locals
import
*
pygame
.
init
()
size
=
width
,
height
=
600
,
400
bg
=
(
255
,
255
,
255
)
speed
=
[
5
,
0
]
fullscreen
=
False
# 默认不全屏
# 创建指定大小的窗口 Surface,窗口可改变
screen
=
pygame
.
display
.
set_mode
(
size
,
RESIZABLE
)
pygame
.
display
.
set_caption
(
"初次见面,请大家多多关照"
)
# 设置放大缩小的比率
ratio
=
1.0
dog
=
pygame
.
image
.
load
(
"dog.png"
)
dog_t
=
dog
# 方便观察只操作 dog_t
# roate 逆时针旋转角度
dog_right
=
pygame
.
transform
.
rotate
(
dog
,
90
)
dog_top
=
pygame
.
transform
.
rotate
(
dog
,
180
)
dog_left
=
pygame
.
transform
.
rotate
(
dog
,
270
)
dog_bottom
=
dog
dog
=
dog_top
# 初始状态从上边界开始
# 获得图像的位置矩形
dog_rect
=
dog
.
get_rect
()
position
=
dog_t_rect
=
dog_rect
# 设置为死循环,确保窗口一直显示
while
True
:
# 遍历所有的事件
for
event
in
pygame
.
event
.
get
():
# 如果单击关闭窗口,则退出
if
event
.
type
==
pygame
.
QUIT
:
sys
.
exit
()
# 根据键盘操作来控制小狗的移动
if
event
.
type
==
KEYDOWN
:
# 全屏(F11)
if
event
.
key
==
K_F11
:
fullscreen
=
not
fullscreen
if
fullscreen
:
#list_modes()可以自动获取电脑屏幕所支持的显示大小,默认由大到小排列成一个列表
# 所以取第一个即是全屏
size
=
width
,
height
=
pygame
.
display
.
list_modes
()[
0
]
screen
=
pygame
.
display
.
set_mode
(
size
,
FULLSCREEN
|
HWSURFACE
)
else
:
size
=
width
,
height
=
600
,
400
screen
=
pygame
.
display
.
set_mode
(
size
)
# 放大缩小,空格键恢复原始尺寸
if
event
.
key
==
K_EQUALS
or
event
.
key
==
K_MINUS
or
event
.
key
==
K_SPACE
:
# 最大只能放大一倍,最小缩小 50%
if
event
.
key
==
K_EQUALS
and
ratio
<
2
:
ratio
+=
0.1
if
event
.
key
==
K_MINUS
and
ratio
>
0.5
:
ratio
-=
0.1
if
event
.
key
==
K_SPACE
:
ratio
=
1.0
dog
=
pygame
.
transform
.
smoothscale
(
dog_t
,
\
(
int
(
dog_t_rect
.
width
*
ratio
),
\
int
(
dog_t_rect
.
height
*
ratio
)))
# 用户手动调整窗口尺寸
if
event
.
type
==
VIDEORESIZE
:
size
=
event
.
size
width
,
height
=
size
screen
=
pygame
.
display
.
set_mode
(
size
,
RESIZABLE
)
# 移动图像
#position.move(x,y) 返回由给定偏移量移动的新矩形。x和y参数可以是任何整数值,正数或负数。
position
=
position
.
move
(
speed
)
if
position
.
right
>
width
:
dog
=
dog_right
position
=
dog_rect
=
dog
.
get_rect
()
# 这里重新获取了 dog,即 left 和 top 又初始了0
print
(
position
.
left
,
position
.
right
,
position
.
top
,
position
.
bottom
)
# 因此这里需要重新更新下 left 保证沿右边界运行
position
.
left
=
width
-
dog_rect
.
width
speed
=
[
0
,
2
]
if
position
.
bottom
>
height
:
dog
=
dog_bottom
position
=
dog_rect
=
dog
.
get_rect
()
position
.
left
=
width
-
dog_rect
.
width
position
.
top
=
height
-
dog_rect
.
height
speed
=
[
-
2
,
0
]
if
position
.
left
<
0
:
dog
=
dog_left
position
=
dog_rect
=
dog
.
get_rect
()
position
.
top
=
height
-
dog_rect
.
height
speed
=
[
0
,
-
2
]
if
position
.
top
<
0
:
dog
=
dog_top
position
=
dog_rect
=
dog
.
get_rect
()
speed
=
[
2
,
0
]
# 填充背景
screen
.
fill
(
bg
)
# 更新图像
screen
.
blit
(
dog
,
position
)
# 更新界面
pygame
.
display
.
flip
()
# 延迟 5 毫秒
pygame
.
time
.
delay
(
5
)
###原文链接:https://blog.csdn.net/nanhuaibeian/article/details/99091384
\ No newline at end of file
snake.py
View file @
b47261a6
...
...
@@ -8,18 +8,23 @@ pygame.init()
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
# 背景
b
ackground
=
pygame
.
image
.
load
(
'bg.png'
)
b
g
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
FPS
=
pygame
.
time
.
Clock
()
x
=
0
y
=
0
while
True
:
for
event
in
pygame
.
event
.
get
():
print
(
event
)
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
# 将背景图画上去
screen
.
blit
(
b
ackground
,
(
0
,
0
))
screen
.
blit
(
b
g
,
(
0
,
0
))
# 将贪吃蛇画上去
screen
.
blit
(
right
,
(
240
,
120
))
# 将贪吃蛇的身体画上去
...
...
@@ -29,4 +34,5 @@ while True:
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
# 刷新画面
pygame
.
display
.
update
()
\ No newline at end of file
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