Commit e8c8e96d by BellCodeEditor

save project

parent 34aa057f
# 默认忽略的文件
/shelf/
/workspace.xml
snake.py
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/贪吃蛇.iml" filepath="$PROJECT_DIR$/.idea/贪吃蛇.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import pygame import pygame
#导入locals子模块
from pygame import locals from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
background = pygame.image.load('bg.png')
#设置游戏帧数(pygame时钟) right = pygame.image.load('right.png')
fps=pygame.time.Clock() body = pygame.image.load('body.png')
food = pygame.image.load('apple.png')
# 创建一个窗口 x,y = 330,240
screen=pygame.display.set_mode((660,480)) position =[(240,240),(270,240),(300,240),(x,y)]
setheading = 'right'
#导入背景图片 screen = pygame.display.set_mode((660,480))
bg=pygame.image.load("bg.png") fps = pygame.time.Clock()
#导入蛇头右图片
right=pygame.image.load("right.png")
#导入食物图片
food=pygame.image.load("apple.png")
#导入身体部分
body=pygame.image.load("body.png")
#初始化贪吃蛇头部坐标
x=240
y=120
#贪吃蛇初始坐标列表
position=[(150,120),(180,120),(210,120),(x,y)]
while True: while True:
#遍历列表中(监听)的事件
for event in pygame.event.get(): for event in pygame.event.get():
#print(event) print(event)
#接到退出事件后退程序 if event.type == locals.QUIT:
if event.type==locals.QUIT:
exit() exit()
if event.type == locals.KEYDOWN:
x=x+30 if event.key == locals.K_d and setheading != 'left':
setheading = 'right'
if event.key == locals.K_a and setheading != 'right':
setheading = 'left'
if event.key == locals.K_s and setheading != 'up':
setheading = 'down'
if event.key == locals.K_w and setheading != 'down':
setheading = 'up'
if setheading == 'right':
x += 30
elif setheading == 'left':
x -= 30
elif setheading == 'up':
y -= 30
elif setheading == 'down':
y += 30
position.append((x,y)) position.append((x,y))
position.pop(0) position.pop(0)
screen.blit(background,(0,0))
#将背景渲染进游戏窗口 screen.blit(food,(330,390))
screen.blit(bg,(0,0)) screen.blit(right,position[-1])
#渲染游戏元素
screen.blit(right,(x,y))
screen.blit(food,(360,300))
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body,position[i]) screen.blit(body,position[i])
# screen.blit(body,(210,120)) #screen.blit(body,position[2])
# screen.blit(body,(180,120)) #screen.blit(body,position[1])
# screen.blit(body,(150,120)) #screen.blit(body,position[0])
#刷新画面
pygame.display.update() pygame.display.update()
fps.tick(3) fps.tick(4)
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment