Commit a743dc69 by BellCodeEditor

save project

parent 81ed9f81
Showing with 186 additions and 73 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init()
time=pygame.time.Clock()
x,y=240,120
apple_x=360
apple_y=300
setheading = "right"
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
shenti = [(180,90),(180,120),(210,120),(x,y)]
# 背景
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
pygame.init()
FPSCLOCK=pygame.time.Clock()
screen=pygame.display.set_mode((660,480))
food=pygame.image.load("apple.png")
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
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:
# 接收到退出事件后退出程序
exit() exit()
elif event.type == locals.KEYDOWN: if event.type==locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left": if event.key==locals.K_UP and setheading != "down":
setheading = "right" setheading="up"
snake_head=right set_head=up
if event.type == locals.KEYDOWN: if event.key==locals.K_DOWN and setheading != "up":
if event.key == locals.K_LEFT and setheading != "right": setheading="down"
setheading = "left" set_head=down
snake_head=left if event.key==locals.K_LEFT and setheading != "right":
if event.type == locals.KEYDOWN: setheading="left"
if event.key == locals.K_UP and setheading != "down": set_head=left
setheading = "up" if event.key==locals.K_RIGHT and setheading != "left":
snake_head=up setheading="right"
if event.type == locals.KEYDOWN: set_head=right
if event.key == locals.K_DOWN and setheading != "up": if setheading=="left":
setheading = "down" x-=30
snake_head=down elif setheading== "right":
x+=30
elif setheading=="up":
if setheading == "right": y-=30
x += 30
elif setheading == 'left':
x -= 30
elif setheading == "up":
y -= 30
else: else:
y += 30 y+=30
shenti.append((x,y)) shenti.append((x,y))
if x== apple_x and y==apple_y:
if x==apple_x and y==apple_y: num1=random.randint(1,22)
score+=10 num2=random.randint(1,16)
num1=apple_x=random.randint(1,22)
num2=apple_y=random.randint(1,16)
apple_x=num1*30-30 apple_x=num1*30-30
apple_y=num2*30-30 apple_y=num2*30-30
else: else:
shenti.pop(0) shenti.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(snake_head,shenti[-1])
# 将贪吃蛇的身体画上去
screen.blit(bg,(0,0))
screen.blit(set_head,(x,y))
screen.blit(food,(apple_x,apple_y))
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))
# 将果实画上去
screen.blit(food, (apple_x, apple_y))
pygame.display.update()
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