Commit 61553e50 by BellCodeEditor

save project

parent f20e0fba
Showing with 13 additions and 3 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
...@@ -19,6 +20,8 @@ left = pygame.image.load('left.png') ...@@ -19,6 +20,8 @@ left = pygame.image.load('left.png')
x,y = 240,120 x,y = 240,120
pos = [(180,90),(180,120,),(210, 120), (x, y)] pos = [(180,90),(180,120,),(210, 120), (x, y)]
apple_x = 360
apple_y = 360
setheading = 'right' setheading = 'right'
snake_head = right snake_head = right
while True: while True:
...@@ -46,19 +49,25 @@ while True: ...@@ -46,19 +49,25 @@ while True:
y-=30 y-=30
else: else:
y+=30 y+=30
screen.blit(background,(0,0))
pos.append((x,y)) pos.append((x,y))
if x==apple_x and y==apple_y:
apple_x = 30*random.randint(0,630)
apple_y = 30*random.randint(0,450)
else:
pos.pop(0) pos.pop(0)
screen.blit(background,(0,0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
screen.blit(snake_head, pos[-1]) # screen.blit(snake_head, pos[-1]) #
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
for i in range(len(pos)-1): for i in range(len(pos)-1):
screen.blit(body, pos[i]) screen.blit(body, pos[i])
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x, apple_y))
# 刷新画面 # 刷新画面
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