博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
可怜的博主跟小豆人杠起来啦!Python制作的吃豆人小游戏,快来围观!!
阅读量:2121 次
发布时间:2019-04-30

本文共 10858 字,大约阅读时间需要 36 分钟。

相关文件

关注小编,私信小编领取哟!

当然别忘了一件三连哟~~

对了大家可以关注小编的公众号哟~~

Python日志
在这里插入图片描述

开发环境

Python版本:3.6.4

相关模块:
pygame模块;
以及一些Python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

原理介绍

通过“↑”,“↓”,“←”,“→”,操控紫色的小豆子移动,当然我们一定要注意的是红色的豆子是不能触碰的哟!不然就gg啦。废话不多说,上才艺:

在这里插入图片描述

def main():    pygame.init()    screen = pygame.display.set_mode((HEIGHT * BLOCK_SIZE + GAP * 2, WIDTH *BLOCK_SIZE + GAP * 2), 0, 32)    pygame.display.set_caption('吃豆人小游戏——彳余大胆')    # print(WIDTH, HEIGHT)    font1 = pygame.font.SysFont('SimHei', 20)           # 人和恶魔    font2 = pygame.font.SysFont('SimHei', 24)           # 显示结局    win = False    lose = False    curMap = restart()    evils = randomCreateEvils()    pac = Pac(1, 1)    # for evil in evils:    #     print(evil)    # 用来控制移动速度不能太快    last_pac_move_time = time.time()                    # 上一次人移动时间    last_evil_move_time = time.time()                    # 上一次恶魔移动时间    while True:        for event in pygame.event.get():            if event.type == QUIT:                pygame.quit()                sys.exit()            elif event.type == KEYDOWN:                if win or lose:                    if event.key == K_SPACE:                        win = False                        lose = False                        curMap = restart()                        evils = randomCreateEvils()                        pac = Pac(1, 1)        if event.type == KEYDOWN:            if not win and not lose:                if time.time() - last_pac_move_time > 0.1:                    if event.key == K_UP:                        pac = moveUp(pac, curMap)                    elif event.key == K_DOWN:                        pac = moveDown(pac, curMap)                    elif event.key == K_LEFT:                        pac = moveLeft(pac, curMap)                    elif event.key == K_RIGHT:                        pac = moveRight(pac, curMap)                    win = judgeWin(curMap)                    last_pac_move_time = time.time()        if not win and not lose:            if time.time() - last_evil_move_time > 0.5:                evilCatch(evils, pac)                last_evil_move_time = time.time()        draw_map(screen, curMap, font1)        draw_evil(screen, evils, font1)        lose = judgeLose(evils, pac)        if lose:            draw_lose(screen, font2)        if win:            draw_win(screen, font2)        pygame.display.update()

设置地图的大小,背景颜色,豆子的颜色等等:

WIDTH = len(map1)                               # 地图宽度HEIGHT = len(map1[0])                           # 地图高度BLOCK_SIZE = 20                                 # 单位块大小GAP = 5                                         # 地图四周空隙BG_COLOR = (40, 40, 60)                         # 背景颜色PAC_COLOR = (255, 255, 0)                       # 豆子颜色BLOCK_COLOR = (100, 100, 100)                   # 墙颜色MAN_COLOR = (148, 0, 211)                       # 人颜色ENEMY_COLOR = (200, 30, 30)                     # 恶魔颜色INFO_COLOR = (255, 50, 50)                      # 信息颜色PAC_RADIU = 4                                   # 豆子半径EX = int((BLOCK_SIZE - PAC_RADIU) / 2)          # 画豆子的额外计算距离DIRECTION = [[-1, 0], [1, 0], [0, -1], [0, 1]]  # 恶魔随机移动方向

画出地图:

# 画出地图def draw_map(screen, curMap, font):    # 填充背景色    screen.fill(BG_COLOR)    for i in range(WIDTH):        for j in range(HEIGHT):            w = GAP + j * BLOCK_SIZE            h = GAP + i * BLOCK_SIZE            if curMap[i][j] == 0:                # pygame.draw.circle(screen, PAC_COLOR, (w + EX, h + EX), PAC_RADIU, 0)                                # 抗锯齿的圆                pygame.gfxdraw.aacircle(screen, w + EX, h + EX, PAC_RADIU, PAC_COLOR)                pygame.gfxdraw.filled_circle(screen, w + EX, h + EX, PAC_RADIU, PAC_COLOR)            elif curMap[i][j] == 1:                pygame.draw.rect(screen, BLOCK_COLOR, (w, h, BLOCK_SIZE, BLOCK_SIZE), 0)            elif curMap[i][j] == 3:                pygame.gfxdraw.aacircle(screen, w + 10, h + 10, 8, MAN_COLOR)                pygame.gfxdraw.filled_circle(screen, w + 10, h + 10, 8, MAN_COLOR)

总体代码的话就是这些啦:

Evil = namedtuple('Evil', ['x', 'y'])Pac = namedtuple('Pac', ['x', 'y'])WIDTH = len(map1)                               # 地图宽度HEIGHT = len(map1[0])                           # 地图高度BLOCK_SIZE = 20                                 # 单位块大小GAP = 5                                         # 地图四周空隙BG_COLOR = (40, 40, 60)                         # 背景颜色PAC_COLOR = (255, 255, 0)                       # 豆子颜色BLOCK_COLOR = (100, 100, 100)                   # 墙颜色MAN_COLOR = (148, 0, 211)                       # 人颜色ENEMY_COLOR = (200, 30, 30)                     # 恶魔颜色INFO_COLOR = (255, 50, 50)                      # 信息颜色PAC_RADIU = 4                                   # 豆子半径EX = int((BLOCK_SIZE - PAC_RADIU) / 2)          # 画豆子的额外计算距离DIRECTION = [[-1, 0], [1, 0], [0, -1], [0, 1]]  # 恶魔随机移动方向def moveUp(pac, curMap):    new_pac = pac    nx = pac.x - 1    ny = pac.y    if curMap[nx][ny] != 1:        curMap[nx][ny] = 3        curMap[pac.x][pac.y] = 2        new_pac = pac._replace(x = nx)    return new_pacdef moveDown(pac, curMap):    new_pac = pac    nx = pac.x + 1    ny = pac.y    if curMap[nx][ny] != 1:        curMap[nx][ny] = 3        curMap[pac.x][pac.y] = 2        new_pac = pac._replace(x = nx)    return new_pacdef moveLeft(pac, curMap):    new_pac = pac    nx = pac.x    ny = pac.y - 1    if curMap[nx][ny] != 1:        curMap[nx][ny] = 3        curMap[pac.x][pac.y] = 2        new_pac = pac._replace(y = ny)    return new_pacdef moveRight(pac, curMap):    new_pac = pac    nx = pac.x    ny = pac.y + 1    if curMap[nx][ny] != 1:        curMap[nx][ny] = 3        curMap[pac.x][pac.y] = 2        new_pac = pac._replace(y=ny)    return new_pacdef restart():    curMap = copy.deepcopy(map1)    return curMapdef randomCreateEvils():    evils = []    for i in range(4):        x = random.randint(0, WIDTH - 1)        y = random.randint(0, HEIGHT - 1)        evil = Evil(x, y)        evils.append(evil)    return evilsdef evilCatch(evils, pac):    for evil in evils:        if abs(evil.x - pac.x) + abs(evil.y - pac.y) > 8:            while True:                dx, dy = random.choice(DIRECTION)                if 0 <= evil.x + dx and evil.x + dx < WIDTH and 0 <= evil.y + dy and evil.y + dy < HEIGHT:                    break            if dx == 0:                new_evil = evil._replace(y = evil.y + dy)            else:                new_evil = evil._replace(x = evil.x + dx)        else:            if evil.x < pac.x :                new_evil = evil._replace(x = evil.x + 1)            elif evil.x > pac.x :                new_evil = evil._replace(x = evil.x - 1)            elif evil.y < pac.y :                new_evil = evil._replace(y = evil.y + 1)            elif evil.y > pac.y :                new_evil = evil._replace(y = evil.y - 1)        evils.remove(evil)        evils.append(new_evil)def judgeWin(curMap):    for line in curMap:        if 0 in line:            return False    return Truedef judgeLose(evils, pac):    for evil in evils:        if evil.x == pac.x and evil.y == pac.y:            return True    return Falsedef main():    pygame.init()    screen = pygame.display.set_mode((HEIGHT * BLOCK_SIZE + GAP * 2, WIDTH *BLOCK_SIZE + GAP * 2), 0, 32)    pygame.display.set_caption('吃豆人小游戏——彳余大胆')    # print(WIDTH, HEIGHT)    font1 = pygame.font.SysFont('SimHei', 20)           # 人和恶魔    font2 = pygame.font.SysFont('SimHei', 24)           # 显示结局    win = False    lose = False    curMap = restart()    evils = randomCreateEvils()    pac = Pac(1, 1)    # for evil in evils:    #     print(evil)    # 用来控制移动速度不能太快    last_pac_move_time = time.time()                    # 上一次人移动时间    last_evil_move_time = time.time()                    # 上一次恶魔移动时间    while True:        for event in pygame.event.get():            if event.type == QUIT:                pygame.quit()                sys.exit()            elif event.type == KEYDOWN:                if win or lose:                    if event.key == K_SPACE:                        win = False                        lose = False                        curMap = restart()                        evils = randomCreateEvils()                        pac = Pac(1, 1)        if event.type == KEYDOWN:            if not win and not lose:                if time.time() - last_pac_move_time > 0.1:                    if event.key == K_UP:                        pac = moveUp(pac, curMap)                    elif event.key == K_DOWN:                        pac = moveDown(pac, curMap)                    elif event.key == K_LEFT:                        pac = moveLeft(pac, curMap)                    elif event.key == K_RIGHT:                        pac = moveRight(pac, curMap)                    win = judgeWin(curMap)                    last_pac_move_time = time.time()        if not win and not lose:            if time.time() - last_evil_move_time > 0.5:                evilCatch(evils, pac)                last_evil_move_time = time.time()        draw_map(screen, curMap, font1)        draw_evil(screen, evils, font1)        lose = judgeLose(evils, pac)        if lose:            draw_lose(screen, font2)        if win:            draw_win(screen, font2)        pygame.display.update()# 画出地图def draw_map(screen, curMap, font):    # 填充背景色    screen.fill(BG_COLOR)    for i in range(WIDTH):        for j in range(HEIGHT):            w = GAP + j * BLOCK_SIZE            h = GAP + i * BLOCK_SIZE            if curMap[i][j] == 0:                # pygame.draw.circle(screen, PAC_COLOR, (w + EX, h + EX), PAC_RADIU, 0)                                # 抗锯齿的圆                pygame.gfxdraw.aacircle(screen, w + EX, h + EX, PAC_RADIU, PAC_COLOR)                pygame.gfxdraw.filled_circle(screen, w + EX, h + EX, PAC_RADIU, PAC_COLOR)            elif curMap[i][j] == 1:                pygame.draw.rect(screen, BLOCK_COLOR, (w, h, BLOCK_SIZE, BLOCK_SIZE), 0)            elif curMap[i][j] == 3:                pygame.gfxdraw.aacircle(screen, w + 10, h + 10, 8, MAN_COLOR)                pygame.gfxdraw.filled_circle(screen, w + 10, h + 10, 8, MAN_COLOR)def draw_evil(screen, evils, font):    for evil in evils:        w = GAP + evil.y * BLOCK_SIZE        h = GAP + evil.x * BLOCK_SIZE        pygame.gfxdraw.aacircle(screen, w + 10, h + 10, 8, ENEMY_COLOR)        pygame.gfxdraw.filled_circle(screen, w + 10, h + 10, 8, ENEMY_COLOR)def draw_win(screen, font):    # print("win")    screen.fill((255, 255, 255))    text = font.render("You Win! Press 'Space' to restart", True, INFO_COLOR)    screen.blit(text, (10, BLOCK_SIZE * WIDTH // 2))def draw_lose(screen, font):    # print("lose")    screen.fill((255, 255, 255))    text = font.render("You Lose! Press 'Space' to restart", True, INFO_COLOR)    screen.blit(text, (10, BLOCK_SIZE * WIDTH // 2))if __name__ == '__main__':    main()

总体的一个实现界面:

在这里插入图片描述
很多小伙伴在学习python的时候总会遇到一些问题和瓶颈,没有方向感,不知道该从哪里入手去提升,对此我整理了一些资料,希望能够去帮助到小伙伴们,可以加Python学习交流裙:773162165

好啦,今天的小游戏就给大家安利到这里啦,有啥不懂的大家可以在下方评论,需要源码的可以找小编领取哟,记得关注小编的公众号哈~

在这里插入图片描述

转载地址:http://wzfrf.baihongyu.com/

你可能感兴趣的文章
BERT 基础
查看>>
什么是 Transformer
查看>>
简述 XLNet 的原理和应用
查看>>
实战:为图片生成文本摘要
查看>>
论文复现:用 CNN 进行文本分类
查看>>
多复杂的 CNN 都离不开的这几个基本结构
查看>>
实践:动手搭建神经机器翻译模型
查看>>
透彻理解神经机器翻译的原理
查看>>
实践:动手搭建聊天机器人
查看>>
情感分析 Kaggle 实战
查看>>
动手实现 Bahdanau 注意力模型
查看>>
用一个小例子理解 seq2seq 的本质
查看>>
双向 LSTM-CRF 实现命名实体识别
查看>>
序列模型实现词性标注
查看>>
双向 RNN 识别手写数字
查看>>
Peephole LSTM、GRU 实战
查看>>
LSTM 的几种改进方案
查看>>
用 word2vec 进行文档聚类
查看>>
详解 GloVe 的原理和应用
查看>>
word2vec:基于层级 softmax 和负采样的 Skip-Gram
查看>>