。
贪吃蛇
路飞为大家分享一下,自己用python写的一个贪吃蛇小游戏。
游戏简介
如我们往常玩过的贪吃蛇游戏一样,通过上下左右键控制贪吃蛇的移动。
初始化界面和结束界面也许简陋了一些,不过不影响观看。
效果预览
自定义函数
def main():
def start_show(screen):
def run_game(screen,clock,direction):
def move_snake(screen,direction):
def draw_snake(screen):
def draw_food(screen):
def food_was_ate(screen):
def snake_dead(screen):
def terminate():
def game_score(screen,score):
def draw_end_score(screen):
程序代码
#_*_ coding :utf-8 _*_
import pygame,sys,random,time
from pygame.locals import *
width = 720
height = 600
ceil = 20
x_count_ceil = 36
y_count_ceil = 30
snake_body = [{'x':360,'y':300},{'x':380,'y':300},{'x':400,'y':300}]
x = random.randint(0,x_count_ceil)*20
y = random.randint(0,y_count_ceil)*20
food = {'x':x,'y':y}
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
blue = (0,0,255)
green = (0,255,0)
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
start_picture = pygame.image.load(r'C:UsersAdministratorDesktop素材飞机初始界面.png')
end_picture = pygame.image.load(r'C:UsersAdministratorDesktop素材飞机game_over.png')
head = 0
direction = RIGHT
def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((width, height))
start_show(screen)
run_game(screen,clock,direction)
def start_show(screen):
screen.blit(start_picture, (0, 0))
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
else:
return
def run_game(screen,clock,direction):
while 1:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE :
terminate()
elif event.key == K_LEFT and direction != RIGHT:
direction = 3
elif event.key == K_RIGHT and direction != LEFT:
direction = 4
elif event.key == K_UP and direction != DOWN:
direction = 1
elif event.key == K_DOWN and direction != UP:
direction = 2
screen.fill(white)
draw_food(screen)
draw_snake(screen)
move_snake(screen,direction)
if snake_dead(screen):
screen.blit(end_picture,(0,0))
draw_end_score(screen)
pygame.display.update()
time.sleep(2)
terminate()
game_score(screen,len(snake_body)-3)
pygame.display.update()
clock.tick(10)
def move_snake(screen,direction):
if direction == 1 and direction!=2:
snake_body.insert(0,{'x':snake_body[0]['x'],'y':snake_body[0]['y']-20})
snake_body.pop()
elif direction == 2 and direction!=1:
snake_body.insert(0,{'x':snake_body[0]['x'],'y':snake_body[0]['y']+20})
snake_body.pop()
elif direction == 4 and direction!=3:
snake_body.insert(0,{'x':snake_body[0]['x']+20,'y':snake_body[0]['y']})
snake_body.pop()
elif direction == 3 and direction!=4:
snake_body.insert(0,{'x':snake_body[0]['x']-20,'y':snake_body[0]['y']})
snake_body.pop()
def draw_snake(screen):
for i in snake_body:
x = i['x']
y = i['y']
rect_out = pygame.Rect(x,y,ceil,ceil)
rect_in = pygame.Rect(x,y,ceil-2,ceil-2)
pygame.draw.rect(screen,blue,rect_in)
pygame.draw.rect(screen,green,rect_out)
def draw_food(screen):
x = random.randint(1,x_count_ceil-1)*20
y = random.randint(1,y_count_ceil-1)*20
if food_was_ate(screen):
food['x'] = x
food['y'] = y
pygame.draw.rect(screen,red,(food['x'],food['y'],ceil,ceil))
else:
pygame.draw.rect(screen,red,(food['x'],food['y'],ceil,ceil))
def food_was_ate(screen):
if snake_body[0]['x'] == food['x'] and snake_body[0]['y'] == food['y']:
snake_body.insert(len(snake_body)-1,{'x':snake_body[-1]['x'],'y':snake_body[-1]['y']})
return True
return False
def snake_dead(screen):
tag = False
if snake_body[0]['x'] == 0 or snake_body[0]['y'] == 0 or snake_body[0]['x'] > width or snake_body[0]['y'] > height:
tag = True
return tag
def terminate():
pygame.quit()
sys.exit()
def game_score(screen,score):
font = pygame.font.Font('方正粗黑宋简体.ttf', 30)
score_surf = font.render('得分:%s' % score, True, black)
score_rect = score_surf.get_rect()
screen.blit(score_surf, score_rect)
def draw_end_score(screen):
screen.blit(end_picture,(0,0))
score = len(snake_body)-3
font = pygame.font.Font('方正粗黑宋简体.ttf',50)
score_surf = font.render('得分:%s' % score, True, green)
score_tip = font.render('两秒后退出游戏',True,green)
screen.blit(score_surf, (width/2,height/2-100))
main()