Commit ae43067d by BellCodeEditor

auto save

parent 7bc38f28
Showing with 19 additions and 4 deletions
......@@ -13,22 +13,28 @@ up = pygame.image.load('up.png') #头朝上
down = pygame.image.load('down.png') #头朝下
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
#创建字体资源
my_font = pygame.font.Font('neuropol.ttf',18)
#设置控制速度时钟模块
FPS = pygame.time.Clock()
#设置贪吃蛇的初始朝向
setheading = "right"
#设置贪吃蛇头部的朝向
snake_head = right
#贪吃蛇的头部
x,y=240,120
#创建苹果的初始坐标
apple_x = 360
apple_y = 300
#创建score变量
score = 0
#贪吃蛇的移动:
#设置贪吃蛇的初始位置,按照从尾到头的顺序
pos = [(180,90),(180,120),(210,120),(x,y)]
#创建苹果的初始坐标
apple_x = 360
apple_y = 300
while True:
......@@ -65,14 +71,18 @@ while True:
#时刻记录头部坐标
pos.append((x,y))
#贪吃蛇吃到了苹果,这段代码要放在贪吃蛇头部更新的后面
if x == apple_y and y == apple_y:
if x == apple_x and y == apple_y:
num1 = random.randint(1,22) #横排格子数
num2 = random.randint(1,16) #纵排格子数
apple_x = num1*30-30
apple_y = num2*30-30
score+=10
#最尾部往前移动了,所以删除掉,吃了苹果就不删除
else:
pos.pop(0)
#撞墙检测:
if x<0 or x>630 or y<0 or y>450:
exit()
screen.blit(backgrade,(0,0))
#用列表中的元素表示坐标
screen.blit(snake_head,pos[-1])
......@@ -85,6 +95,11 @@ while True:
# screen.blit(body,(180,90))
screen.blit(food,(apple_x,apple_y))
#绘制分数
info = "Score"+str(score)
text = my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
pygame.display.update()
#在一秒内刷新不超过3次
FPS.tick(3)
......
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