前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pygame 贪吃蛇无边界版本源代码

pygame 贪吃蛇无边界版本源代码

作者头像
叶子陪你玩
发布2021-09-15 10:55:09
3400
发布2021-09-15 10:55:09
举报

源代码:

代码语言:javascript
复制
import pygame
import sys
import random
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
WIDTH,HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH,HEIGHT))
# 设置窗口标题
pygame.display.set_caption('标题')
# 字体
my_font = pygame.font.SysFont("arial", 20)
my_font2 = pygame.font.SysFont("arial", 80)
CELLSIZE = 20

darkblue = (0,0,155)
blue = (0,0,255)
darkgreen = (0,155,0)
green = (0 ,255, 0)
darkred = (155,0,0)
red = (255,0,0)
darkyellow = (155,155,0)
yellow = (255,255,0)
darkgray = (40,40,40)
gray = (155,155,155)
white = (255, 255, 255)
food_x = random.randint(1,640//CELLSIZE-2)
food_y = random.randint(1,480//CELLSIZE-2)

startx = random.randint(5,640//CELLSIZE-6)
starty = random.randint(5,480//CELLSIZE-6)
snake_coords = [
    {'x': startx, 'y': starty},
    {'x': startx-1, 'y': starty},
    {'x': startx-2, 'y': starty},
]

direction = 'right'
up = 'up'
down = 'down'
left = 'left'
right = 'right'

score = 0
is_over = False
# 时钟
clock = pygame.time.Clock()

# 程序主循环
while True:
    # ---- 事件处理开始 ----
    for event in pygame.event.get():
        # 判断事件是否为退出事件
        if event.type == pygame.QUIT:
            # 退出pygame
            pygame.quit()
            # 退出系统
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if direction != right:
                    direction = left
            if event.key == pygame.K_RIGHT:
                if direction != left:
                    direction = right
            if event.key == pygame.K_UP:
                if direction != down:
                    direction = up
            if event.key == pygame.K_DOWN:
                if direction != down:
                    direction = down
    # ---- 事件处理结束 ----

    #----- 逻辑处理开始-----
    if direction == up:
        new_head = {'x':snake_coords[0]['x'],'y':snake_coords[0]['y'] - 1 }
    if direction == down:
        new_head = {'x': snake_coords[0]['x'], 'y': snake_coords[0]['y'] + 1}
    if direction == left:
        new_head = {'x':snake_coords[0]['x']-1,'y':snake_coords[0]['y'] }
    if direction == right:
        new_head = {'x': snake_coords[0]['x']+1, 'y': snake_coords[0]['y']}
    # 增加蛇头
    snake_coords.insert(0,new_head)
    # 删除蛇尾
    snake_coords.pop()
    # 检测是否吃到食物
    if snake_coords[0]['x'] == food_x and snake_coords[0]['y'] == food_y:
        food_x = random.randint(1, 640 // CELLSIZE - 2)
        food_y = random.randint(1, 480 // CELLSIZE - 2)
        score = score + 1
        snake_coords.insert(0, new_head)
    # 设置穿墙
    if snake_coords[0]['y'] < 0:
        snake_coords[0]['y'] = HEIGHT//CELLSIZE
    elif snake_coords[0]['y'] >= HEIGHT//CELLSIZE:
        snake_coords[0]['y'] = 0
    elif snake_coords[0]['x'] < 0:
        snake_coords[0]['x'] = WIDTH//CELLSIZE
    elif snake_coords[0]['x'] >= WIDTH//CELLSIZE:
        snake_coords[0]['x'] = 0
    # 检测是否头有碰到身体
    if snake_coords[0] in snake_coords[3:]:
        is_over = True
    # ----逻辑处理结束------

    # ----- 绘制处理开始-----
    # 设置屏幕背景
    screen.fill((0,0,0))
    # 画网格
    for x in range(0,WIDTH,CELLSIZE):
        pygame.draw.line(screen,darkgray,(x,0),(x,HEIGHT))
    for y in range(0,HEIGHT,CELLSIZE):
        pygame.draw.line(screen,darkgray,(0,y),(WIDTH,y))
    # 画食物
    pygame.draw.rect(screen,red,(food_x*CELLSIZE,food_y*CELLSIZE,CELLSIZE,CELLSIZE))
    # 画蛇
    for coord in snake_coords:
        pygame.draw.rect(screen, darkyellow, (coord['x'] * CELLSIZE, coord['y'] * CELLSIZE, CELLSIZE, CELLSIZE))
        pygame.draw.rect(screen,yellow, (coord['x'] * CELLSIZE+4, coord['y'] * CELLSIZE+4, CELLSIZE-8, CELLSIZE-8))
    # 设置蛇头不同的颜色
    pygame.draw.rect(screen, darkgreen, (snake_coords[0]['x'] * CELLSIZE, snake_coords[0]['y'] * CELLSIZE, CELLSIZE, CELLSIZE))
    pygame.draw.rect(screen, green, (snake_coords[0]['x'] * CELLSIZE + 4, snake_coords[0]['y'] * CELLSIZE + 4, CELLSIZE - 8, CELLSIZE - 8))
    # 画分数
    textImage = my_font.render("Score:"+str(score), True, red)
    screen.blit(textImage, (540, 30))
    # 画失败界面
    if is_over:
        while True:
            textImage = my_font2.render("Game Over", True, red)
            screen.blit(textImage, (150, 200))
            pygame.display.update()
    # ----绘制处理结束------

    # 刷新屏幕
    pygame.display.update()
    # 游戏帧率
    clock.tick(10)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 叶子陪你玩编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档