python制作简易贪吃蛇小游戏
来源:互联网 编辑:wan玩得好手游小编 更新:2025-11-03 07:28:58 人气:
。

贪吃蛇
路飞为大家分享一下,自己用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,timefrom pygame.locals import *width = 720height = 600ceil = 20x_count_ceil = 36y_count_ceil = 30snake_body = [{'x':360,'y':300},{'x':380,'y':300},{'x':400,'y':300}]x = random.randint(0,x_count_ceil)*20y = random.randint(0,y_count_ceil)*20food = {'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 = 1DOWN = 2LEFT = 3RIGHT = 4start_picture = pygame.image.load(r'C:UsersAdministratorDesktop素材飞机初始界面.png')end_picture = pygame.image.load(r'C:UsersAdministratorDesktop素材飞机game_over.png')head = 0direction = RIGHTdef 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:returndef 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 = 3elif event.key == K_RIGHT and direction != LEFT:direction = 4elif event.key == K_UP and direction != DOWN:direction = 1elif event.key == K_DOWN and direction != UP:direction = 2screen.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)*20y = random.randint(1,y_count_ceil-1)*20if food_was_ate(screen):food['x'] = xfood['y'] = ypygame.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 Truereturn Falsedef snake_dead(screen):tag = Falseif snake_body[0]['x'] == 0 or snake_body[0]['y'] == 0 or snake_body[0]['x'] > width or snake_body[0]['y'] > height:tag = Truereturn tagdef 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)-3font = 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()

欢迎玩家到【 wan玩得好手游】查看最新变态版手游攻略,只需要在百度输入【 wan玩得好手游】就可以浏览最新上线送满vip的变态手游攻略了,更多有关手游的攻略和资讯,敬请关注玩得好手游网!

