Commit accc4482 by BellCodeEditor

auto save

parent da84112a
Showing with 38 additions and 15 deletions
...@@ -16,10 +16,13 @@ body = pygame.image.load('body.png') # 身体 ...@@ -16,10 +16,13 @@ body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左 left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上 up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下 down = pygame.image.load('down.png') # 头 朝下
my_font = pygame.font.Font('neuropol.ttf',18) # 导入字体
x, y = 240, 120 x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)] position = [(180, 90), (180, 120), (210, 120), (x, y)]
apple_x, apple_y = 360, 300 apple_x, apple_y = 360, 300
score = 0
setheading = "right" setheading = "right"
snake_head = right snake_head = right
...@@ -32,32 +35,48 @@ while True: ...@@ -32,32 +35,48 @@ while True:
if event.key == locals.K_RIGHT and setheading != "left": if event.key == locals.K_RIGHT and setheading != "left":
setheading = 'right' setheading = 'right'
snake_head = right snake_head = right
x += 30
position.append((x, y))
position.pop(0)
if event.key == locals.K_LEFT and setheading != "right": if event.key == locals.K_LEFT and setheading != "right":
setheading = 'left' setheading = 'left'
snake_head = left snake_head = left
x -= 30
position.append((x, y))
position.pop(0)
if event.key == locals.K_UP and setheading != "down": if event.key == locals.K_UP and setheading != "down":
setheading = 'up' setheading = 'up'
snake_head = up snake_head = up
y -= 30
position.append((x, y))
position.pop(0)
if event.key == locals.K_DOWN and setheading != "up": if event.key == locals.K_DOWN and setheading != "up":
setheading = 'down' setheading = 'down'
snake_head = down snake_head = down
y += 30
position.append((x, y))
position.pop(0)
# 设置贪吃蛇的头部坐标 # 设置贪吃蛇的头部坐标
if setheading == "right": # if setheading == "right":
x += 30 # x += 30
elif setheading == "left": # elif setheading == "left":
x -= 30 # x -= 30
elif setheading == "up": # elif setheading == "up":
y -= 30 # y -= 30
else: # else:
y += 30 # y += 30
position.append((x, y)) # position.append((x, y))
if x == apple_x and y == apple_y: # if x == apple_x and y == apple_y:
apple_x = random.randint(0, 660) # 通过随机数将苹果移动到新的坐标位置 # num1 = random.randint(1, 22) # 通过随机数将苹果移动到新的坐标位置
apple_y = random.randint(0, 480) # num2 = random.randint(1, 16)
# apple_x = num1 * 30 - 30 # 通过随机数将苹果移动到新的坐标位置
# apple_y = num2 * 30 - 30
position.pop(0) # score += 10
# else:
# position.pop(0)
# if x < 0 or x > 630 or y < 0 or y > 450:
# exit()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
...@@ -68,6 +87,9 @@ while True: ...@@ -68,6 +87,9 @@ while True:
# 将果实画上去 # 将果实画上去
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()
FPSCLOCK.tick(3) FPSCLOCK.tick(3)
\ No newline at end of file
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