Commit a743dc69 by BellCodeEditor

save project

parent 81ed9f81
Showing with 185 additions and 72 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init() pygame.init()
time=pygame.time.Clock() FPSCLOCK=pygame.time.Clock()
x,y=240,120 screen=pygame.display.set_mode((660,480))
apple_x=360
apple_y=300 food=pygame.image.load("apple.png")
setheading = "right" bg=pygame.image.load("bg.png")
body=pygame.image.load("body.png")
down=pygame.image.load("down.png")
left=pygame.image.load("left.png")
right=pygame.image.load("right.png")
up=pygame.image.load("up.png")
x,y=360,90
shenti=[(300,60),(300,90),(330,90),(x,y)]
apple_x=60
apple_y=120
setheading="right"
set_head=right
# 创建一个窗口 while True:
screen = pygame.display.set_mode((660, 480)) for event in pygame.event.get():
shenti = [(180,90),(180,120),(210,120),(x,y)] if event.type==locals.QUIT:
exit()
if event.type==locals.KEYDOWN:
if event.key==locals.K_UP and setheading != "down":
setheading="up"
set_head=up
if event.key==locals.K_DOWN and setheading != "up":
setheading="down"
set_head=down
if event.key==locals.K_LEFT and setheading != "right":
setheading="left"
set_head=left
if event.key==locals.K_RIGHT and setheading != "left":
setheading="right"
set_head=right
if setheading=="left":
x-=30
elif setheading== "right":
x+=30
elif setheading=="up":
y-=30
else:
y+=30
shenti.append((x,y))
if x== apple_x and y==apple_y:
num1=random.randint(1,22)
num2=random.randint(1,16)
apple_x=num1*30-30
apple_y=num2*30-30
else:
shenti.pop(0)
# 背景
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')
ziti = pygame.font.Font('neuropol.ttf',18)
snake_head=right
...@@ -32,64 +65,145 @@ snake_head=right ...@@ -32,64 +65,145 @@ snake_head=right
score=0
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
elif event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left":
setheading = "right"
snake_head=right
if event.type == locals.KEYDOWN:
if event.key == locals.K_LEFT and setheading != "right":
setheading = "left"
snake_head=left
if event.type == locals.KEYDOWN:
if event.key == locals.K_UP and setheading != "down":
setheading = "up"
snake_head=up
if event.type == locals.KEYDOWN:
if event.key == locals.K_DOWN and setheading != "up":
setheading = "down"
snake_head=down
if setheading == "right":
x += 30
elif setheading == 'left':
x -= 30
elif setheading == "up":
y -= 30
else:
y += 30
shenti.append((x,y))
if x==apple_x and y==apple_y: screen.blit(bg,(0,0))
score+=10 screen.blit(set_head,(x,y))
num1=apple_x=random.randint(1,22) screen.blit(food,(apple_x,apple_y))
num2=apple_y=random.randint(1,16)
apple_x=num1*30-30
apple_y=num2*30-30
else:
shenti.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(snake_head,shenti[-1])
# 将贪吃蛇的身体画上去
for i in range(len(shenti)-1): for i in range(len(shenti)-1):
screen.blit(body,shenti[i]) screen.blit(body,shenti[i])
info = "score:"+str(score)
text = ziti.render(info,True,(0,0,0))
screen.blit(text,(540,10))
# 将果实画上去 pygame.display.update()
screen.blit(food, (apple_x, apple_y)) FPSCLOCK.tick(3)
# 刷新画面
pygame.display.update() pygame.display.update()
time.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