Commit 4b0f1d9a by BellCodeEditor

save project

parent ea129f17
Showing with 24 additions and 22 deletions
...@@ -8,24 +8,25 @@ pygame.init() ...@@ -8,24 +8,25 @@ 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')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
my_font=pygame.font.Font('neuropol.ttf',18)
x, y = 240, 120
apple_x,apple_y=300,360
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right"
snake_head = right
while True: while True:
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
my_font=pygame.font.Font('neuropol.ttf',18)
x, y = 240, 120
apple_x,apple_y=300,360
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right"
snake_head = right
v=3
while True:
fenshu=0
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
...@@ -55,13 +56,14 @@ while True: ...@@ -55,13 +56,14 @@ while True:
else: else:
y += 30 y += 30
if x==apple_x and y==apple_y: if x==apple_x and y==apple_y:
apple_x,apple_y=random.randint(0,22)*30,random.randint(0,16)*30 apple_x,apple_y=random.randint(0,21)*30,random.randint(0,15)*30
fenshu+=10 fenshu+=10
v+=1
else: else:
position.pop(0) position.pop(0)
position.append((x, y)) position.append((x, y))
if x>630 or x<0 or y>450 or y<0: if x>630 or x<0 or y>450 or y<0 or (x,y) in position[:-1]:
exit() break
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
...@@ -76,4 +78,4 @@ while True: ...@@ -76,4 +78,4 @@ while True:
screen.blit(text,(540,10)) screen.blit(text,(540,10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(v)
\ 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