• 基于Python pygame简易版斗兽棋小游戏源代码


    基于Python pygame简易版斗兽棋小游戏源代码
    游戏规则如下:

    胜利条件:

    1.吃掉对方全部棋子

    2.走入对方兽穴(不可进入自己洞穴)
    吃法:

    1.象>狮>虎>豹>狼>狗>猫>鼠>象

    2.同类棋子先行者吃掉对方

    3.老鼠可以进河,老鼠在河里时,岸上的动物不能捕食他,他也不能捕食岸上的动物

    4.狮\虎在河中没有老鼠阻挡时可以跳过河,并可吃掉对岸的较小的动物

    5.进入敌方陷阱后,任意一个棋子都可以吃掉他。

    在这里插入图片描述
    main.py:主程序,控制游戏的进行状况

    menu:创建图形界面

    level:棋局进行中的主要类和方法

    piece.py:棋子类,提供控制棋子的方法

    board.py:生成棋盘

    piece.json:储存了创建棋子实例的信息

    完整程序代码下载地址:基于Python pygame简易版斗兽棋小游戏源代码

    main.py

    import sys
    import pygame
    import pygame_gui
    
    from settings import *
    from level import Level
    from menu import Start_menu
    
    class Game(object):
        def __init__(self):
    
            #窗口
            pygame.init()
            self.screen = pygame.display.set_mode(WINDOW_SIZE)
            pygame.display.set_caption('斗兽棋')
            self.clock = pygame.time.Clock()
            
            #Controller
            self.level = Level()
            self.start_menu_ = Start_menu()
            #游戏状态
            self.game_over = True    
    
        @property
        def controller(self):
            """ 根据游戏状态选择Controller """
            if self.game_over:
                return self.start_menu_
            else:
                return self.level
    
        def run(self):
    
            self.controller.game_over = self.game_over  #保证切换Controller后其状态与当前游戏状态一致
     
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        self.controller.reponse_click(event.pos)
    
                if event.type == pygame_gui.UI_BUTTON_PRESSED:
                    self.controller.reponse_button(event.ui_element)  
                self.controller.manager.process_events(event)
            
    
            delta_time = self.clock.tick(20) / 1000
            self.controller.run(delta_time)
    
            self.game_over = self.controller.game_over
    
            pygame.display.update()
            
    
    if __name__ == '__main__':
        game = Game()
        while True:
            game.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    board.py

    import pygame
    from settings import *
    
    font = pygame.font.Font(FONT, 30)
    
    class Board(pygame.sprite.Sprite):
        def __init__(self,group,width,height):
            super().__init__(group)
            self.width = width
            self.height = height
    
            self.image = pygame.Surface((width*50,height*50))
            self.rect = self.image.get_rect()
            self.draw_board()
    
        def draw_board(self):
            """ 绘制棋盘 """
    
            self.image.fill('white')
    
            #画格子
            for x in range(self.width):
                for y in range(self.height):
                    pygame.draw.rect(self.image,"black", (x*50, y*50, 50, 50), 1)
    
            #河流
            for pos in RIVER:
                pygame.draw.rect(self.image,"deepskyblue",(pos[0]*50, pos[1]*50,50,50),0)
            #陷阱
            for t in TRAP:
                for pos in t:       
                    trap_surface=font.render("陷",True,"blue","white")
                    self.image.blit(trap_surface,(pos[0]*50+DETA_X, pos[1]*50+ DETA_Y))
    
            #兽穴
            for pos in HOME:
                home_surface = font.render("穴",True,"blue","white")
                self.image.blit(home_surface,(pos[0]*50+ DETA_X, pos[1]*50+ DETA_Y))
        
        def update(self):
            pass
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述
    完整程序代码下载地址:基于Python pygame简易版斗兽棋小游戏源代码

  • 相关阅读:
    手把手教你配置Jenkins自动化邮件通知
    精通Spring Boot单元测试:构建健壮的Java应用
    Javassist-ConstPool常量池
    2024年数字化经济与智慧金融国际会议(ICDESF 2024)
    想要提高客户留资率?一个留资机器人就够了!
    经典算法系列之(二):七大查找——顺序查找
    PMP_模考二 180题(附答案及解析)
    项目分析(哪些是it培训给不了)
    linux C++注册程序退出信号处理的代码
    Docker镜像、容器、仓库及数据管理
  • 原文地址:https://blog.csdn.net/weixin_42756970/article/details/128149594