Commit 592a6d65 by BellCodeEditor

save project

parent c017b741
Showing with 22 additions and 16 deletions
...@@ -17,19 +17,17 @@ body = pygame.image.load('body.png') # 身体 ...@@ -17,19 +17,17 @@ 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",20)
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 = random.randint(0,660) apple_x = 360
apple_y = random.randint(0,480) apple_y = 300
setheading = "right" setheading = "right"
snake_head = right snake_head = right
score = 0 score = 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
...@@ -44,9 +42,10 @@ while True: ...@@ -44,9 +42,10 @@ while True:
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
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
# 设置贪吃蛇的头部坐标 # 设置贪吃蛇的头部坐标
if setheading == "right": if setheading == "right":
x += 30 x += 30
...@@ -56,23 +55,30 @@ while True: ...@@ -56,23 +55,30 @@ while True:
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:
text = font.render("score",+str(score),Ture,(0,0,0))
score += 10 score += 10
# 将背景图画上去 x1 = random.randint(1,21)
screen.blit(background, (0, 0)) y1 = random.randint(1,15)
# 将贪吃蛇的头画上去 n1 = x1*30
screen.blit(snake_head, position[-1]) n2 = y1*30
apple_x = n1
apple_y = n2
screen.blit(food, (n1,n2))
else: else:
position.pop(0) position.pop(0)
text = my_font.render("score:"+str(score),True,(0,0,0))
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body, position[i]) screen.blit(body, position[i])
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food,(apple_x,apple_y))
screen.blit(text,(510,10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(15)
font = pygame.font.Font("simfang.ttf",18) \ No newline at end of file
\ 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