Commit 38136d1d by BellCodeEditor

save project

parent 24232fa4
Showing with 15 additions and 7 deletions
...@@ -9,7 +9,7 @@ pygame.init() ...@@ -9,7 +9,7 @@ pygame.init()
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右 right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果 food = pygame.image.load('apple.png') # 食物 苹果
...@@ -27,7 +27,7 @@ apple_y=330 ...@@ -27,7 +27,7 @@ apple_y=330
setheading = "right" setheading = "right"
snake_head = right snake_head = right
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:
...@@ -57,7 +57,16 @@ while True: ...@@ -57,7 +57,16 @@ while True:
else: else:
y += 30 y += 30
position.append((x, y)) position.append((x, y))
position.pop(0)
if x==apple_x and y==apple_y:
a=randint(1,22)
b=randint(1,16)
apple_x=a*30-30
apple_y=b*30-30
else:
position.pop(0)
if x>630 or x<0 or y>450 or y<0:
exit()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
...@@ -67,11 +76,10 @@ while True: ...@@ -67,11 +76,10 @@ while True:
screen.blit(body, position[i]) screen.blit(body, position[i])
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x, apple_y))
info = "Score:"+str(score) info = "Score:"+str(score)
text = my_font.render(info,True,(0,0,0)) text = my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10)) screen.blit(text,(540,10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(5)
\ 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