Commit 1f47abc1 by BellCodeEditor

save project

parent 6580073d
<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.11" 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/pygame_lesson3_diy3.iml" filepath="$PROJECT_DIR$/.idea/pygame_lesson3_diy3.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?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
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="ca2ace39-914f-411c-8448-58dd8c4aa566" name="变更" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="2K09fLKagOncfec42mvs2WMHeli" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "C:/Users/lenovo/Documents/pygame_lesson3_diy3",
"settings.editor.selected.configurable": "preferences.pluginManager"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="ca2ace39-914f-411c-8448-58dd8c4aa566" name="变更" comment="" />
<created>1673100734327</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1673100734327</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
import pygame import pygame
import random
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
pygame.display.set_caption('贪吃蛇') FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
FPSCLOCK=pygame.time.Clock()
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
#导入图片 right = pygame.image.load('right.png') # 头 朝右
right = pygame.image.load('right.png') food = pygame.image.load('apple.png') # 食物 苹果
left=pygame.image.load('left.png') body = pygame.image.load('body.png') # 身体
up=pygame.image.load('up.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') down = pygame.image.load('down.png') # 头 朝下
body = pygame.image.load('body.png') font=pygame.font.Font('neuropol.ttf',25)
#导入字体 x, y = 240, 120
my_font=pygame.font.Font('neuropol.ttf',25) position = [(180, 90), (180, 120), (210, 120), (x, y)]
x,y=240,120
position=[(180,90),(180,120),(210,120),(x,y)] setheading = "right"
direction='right' snake_head = right
snake_head=right
food_x=0 food_x=0
food_y=0 food_y=0
food_x=random.randint(2,21)*30 food_x=random.randint(2,21)*30
food_y=random.randint(2,15)*30 food_y=random.randint(2,15)*30
score=0 score=0
while True: while True:
if x>630 or x<0 or y<0 or y>450: text=font.render('Score:'+str(score),True,(0,0,0))
if (x,y) in position[0:-1]:
exit() exit()
elif (x,y) in position[0:-1]: if x<0 or x>630 or y<30 or y>450:
exit() exit()
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type==pygame.KEYDOWN: if event.type == locals.KEYDOWN:
if event.key==locals.K_UP and direction != 'down': if event.key == locals.K_RIGHT and setheading != "left":
direction='up' setheading = 'right'
snake_head=up snake_head = right
if event.key==locals.K_DOWN and direction != 'up': if event.key == locals.K_LEFT and setheading != "right":
direction='down' setheading = 'left'
snake_head=down snake_head = left
if event.key==locals.K_LEFT and direction != 'right': if event.key == locals.K_UP and setheading != "down":
direction='left' setheading = 'up'
snake_head=left snake_head = up
if event.key==locals.K_RIGHT and direction != 'left': if event.key == locals.K_DOWN and setheading != "up":
direction='right' setheading = 'down'
snake_head=right snake_head = down
info='score:'+str(score)
text=my_font.render(info,True,(0,0,0)) # 设置贪吃蛇的头部坐标
if direction=='left': if setheading == "right":
x-=30 x += 30
elif direction=='right': elif setheading == "left":
x+=30 x -= 30
elif direction=='up': elif setheading == "up":
y-=30 y -= 30
else: else:
y+=30 y += 30
# 将背景图画上去 position.append((x, y))
screen.blit(background, (0, 0)) if (x,y)==(food_x,food_y):
# 将贪吃蛇画上去
screen.blit(snake_head,position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body,position[i])
# 将果实画上去
screen.blit(food, (food_x, food_y))
screen.blit(text,(530,10))
if (x,y) == (food_x,food_y):
food_x=random.randint(2,21)*30 food_x=random.randint(2,21)*30
food_y=random.randint(2,15)*30 food_y=random.randint(2,15)*30
position.append((x,y))
score+=1 score+=1
else: else:
position.append((x,y))
position.pop(0) position.pop(0)
#检测是否碰到边界 # 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (food_x, food_y))
screen.blit(text,(400,40))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(5) FPSCLOCK.tick(10)
\ 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