diff --git a/snake.py b/snake.py index 537316e..56df02d 100644 --- a/snake.py +++ b/snake.py @@ -1,9 +1,11 @@ +import random import pygame from pygame import locals # 初始化pygame,为使用硬件做准备 pygame.init() - +ap_x=360 +ap_y=120 # 创建一个窗口 screen = pygame.display.set_mode((660, 480)) FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) @@ -52,7 +54,13 @@ while True: else: y += 30 position.append((x, y)) - position.pop(0) + if x==ap_x and y==ap_y: + nu1=random.randint(1,22) + nu2=random.randint(1,16) + ap_x=30*nu1-30 + ap_y=30*nu2-30 + else: + position.pop(0) # 将背景图画上去 screen.blit(background, (0, 0)) # 将贪吃蛇的头画上去 @@ -60,9 +68,8 @@ while True: # 将贪吃蛇的身体画上去 for i in range(len(position)-1): screen.blit(body, position[i]) - # 将果实画上去 - screen.blit(food, (360, 300)) + screen.blit(food, (ap_x,ap_y)) # 刷新画面 pygame.display.update() FPSCLOCK.tick(3) \ No newline at end of file