Commit 6dd3c705 by BellCodeEditor

save project

parent d3cad4cd
Showing with 9 additions and 16 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') right = pygame.image.load('right.png')
...@@ -19,8 +17,8 @@ f=pygame.time.Clock() ...@@ -19,8 +17,8 @@ f=pygame.time.Clock()
x,y=240,120 x,y=240,120
p=[(180,90),(180,120),(210,120),(x,y)] p=[(180,90),(180,120),(210,120),(x,y)]
setheading="right" setheading="right"
apple_x=300
apple_y=360
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:
...@@ -35,14 +33,9 @@ while True: ...@@ -35,14 +33,9 @@ while True:
setheading="up" setheading="up"
if event.key==locals.K_DOWN and setheading != "up": if event.key==locals.K_DOWN and setheading != "up":
setheading="down" setheading="down"
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
# screen.blit(right, (x, y))
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
if setheading=="right": if setheading=="right":
x+=30 x+=30
...@@ -57,15 +50,15 @@ while True: ...@@ -57,15 +50,15 @@ while True:
y+=30 y+=30
screen.blit(down,(x,y)) screen.blit(down,(x,y))
p.append((x,y)) p.append((x,y))
p.pop(0)
#screen.blit(body, (210, 120))
#screen.blit(body, (180, 120))
#screen.blit(body, (180, 90))
for i in range (len(p)-1): for i in range (len(p)-1):
screen.blit(body,p[i]) screen.blit(body,p[i])
if x==apple_x and y==apple_y:
apple_x=random.randint(0,21)*30
apple_y=random.randint(0,15)*30
else:
p.pop(0)
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x,apple_y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
f.tick(3) f.tick(3)
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