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