diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ +<component name="InspectionProjectProfileManager"> + <settings> + <option name="USE_PROJECT_PROFILE" value="false" /> + <version value="1.0" /> + </settings> +</component> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e82920a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13 (pythonProject)" project-jdk-type="Python SDK" /> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..17bbc19 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?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 diff --git a/.idea/pygame_lesson3_diy3.iml b/.idea/pygame_lesson3_diy3.iml new file mode 100644 index 0000000..fc1fe8b --- /dev/null +++ b/.idea/pygame_lesson3_diy3.iml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="jdk" jdkName="Python 3.13 (pythonProject)" jdkType="Python SDK" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?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 diff --git a/snake.py b/snake.py index 537316e..6217380 100644 --- a/snake.py +++ b/snake.py @@ -1,4 +1,5 @@ import pygame +import random from pygame import locals # 初始化pygame,为使用硬件做准备 @@ -19,6 +20,8 @@ down = pygame.image.load('down.png') # 头 朝下 x, y = 240, 120 position = [(180, 90), (180, 120), (210, 120), (x, y)] +apple_x = 360 #苹果横坐标 +apple_y = 300 #苹果纵坐标 setheading = "right" snake_head = right @@ -52,7 +55,14 @@ while True: else: y += 30 position.append((x, y)) - position.pop(0) + #贪吃蛇吃到了苹果 + if x == apple_x and y == apple_y: + num1 = random.randint(1, 22) #横排的几个格子 + num2 = random.randint(1, 16) #纵排的几个格子 + apple_x = 30*num1-30 #苹果的X坐标 + apple_y = 30*num2-30 #苹果的Y坐标 + else: + position.pop(0) # 将背景图画上去 screen.blit(background, (0, 0)) # 将贪吃蛇的头画上去 @@ -62,7 +72,8 @@ while True: screen.blit(body, position[i]) # 将果实画上去 - screen.blit(food, (360, 300)) + screen.blit(food, (apple_x,apple_y)) # 刷新画面 pygame.display.update() - FPSCLOCK.tick(3) \ No newline at end of file + FPSCLOCK.tick(3) + \ No newline at end of file