From 4a0cd6c4a72f9b3193604ae3d6002c26d26f9e83 Mon Sep 17 00:00:00 2001 From: BellCodeEditor <bellcode_dev@bell.ai> Date: Sat, 15 Jan 2022 14:14:12 +0800 Subject: [PATCH] save project --- snake.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/snake.py b/snake.py index 537316e..2fcb960 100644 --- a/snake.py +++ b/snake.py @@ -1,4 +1,5 @@ import pygame +import random from pygame import locals # 初始化pygame,为使用硬件做准备 @@ -18,6 +19,9 @@ up = pygame.image.load('up.png') # 头 朝上 down = pygame.image.load('down.png') # 头 朝下 x, y = 240, 120 + +apple_x , apple_y = 360 , 240 + position = [(180, 90), (180, 120), (210, 120), (x, y)] setheading = "right" @@ -41,6 +45,7 @@ while True: if event.key == locals.K_DOWN and setheading != "up": setheading = 'down' snake_head = down + # 设置贪吃蛇的头部坐标 if setheading == "right": @@ -53,6 +58,11 @@ while True: y += 30 position.append((x, y)) position.pop(0) + + if x = apple_x and y = apple_y: + apple_x = random.randint(0,660) + apple_y = random.randint(0,480) + # 将背景图画上去 screen.blit(background, (0, 0)) # 将贪吃蛇的头画上去 @@ -62,7 +72,7 @@ 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 -- libgit2 0.25.0