diff --git a/snake.py b/snake.py index 537316e..c0cc467 100644 --- a/snake.py +++ b/snake.py @@ -1,9 +1,12 @@ import pygame from pygame import locals - +apx=300 +apy=300 +n1=0 +n2=0 # 初始化pygame,为使用硬件做准备 pygame.init() - +from random import * # 创建一个窗口 screen = pygame.display.set_mode((660, 480)) FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) @@ -41,7 +44,7 @@ while True: if event.key == locals.K_DOWN and setheading != "up": setheading = 'down' snake_head = down - + # 设置贪吃蛇的头部坐标 if setheading == "right": x += 30 @@ -51,8 +54,14 @@ while True: y -= 30 else: y += 30 - position.append((x, y)) - position.pop(0) + position.append((x, y)) + if apx==x and apy==y: + n1=randint(1,22) + n2=randint(1,16) + apx=30*n1-30 + apy=30*n2-30 + else: + position.pop(0) # 将背景图画上去 screen.blit(background, (0, 0)) # 将贪吃蛇的头画上去 @@ -62,7 +71,7 @@ while True: screen.blit(body, position[i]) # 将果实画上去 - screen.blit(food, (360, 300)) + screen.blit(food, (apx, apy)) # 刷新画面 pygame.display.update() FPSCLOCK.tick(3) \ No newline at end of file