脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 详解如何利用Python制作24点小游戏

详解如何利用Python制作24点小游戏

2022-10-24 11:00Charles的皮卡丘 Python

这篇文章主要为大家详细介绍了如何通过Python制作24点小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先睹为快

24点

游戏规则(改编自维基百科)

从1~10这十个数字中随机抽取4个数字(可重复),对这四个数运用加、减、乘、除和括号进行运算得出24。每个数字都必须使用一次,但不能重复使用。

逐步实现

Step1:制作24点生成器

既然是24点小游戏,当然要先定义一个24点游戏生成器啦。主要思路就是随机生成4个有解的数字,且范围在1~10之间,代码实现如下:

?
1
2
3
4
5
6
7
8
def generate(self):
    self.__reset()
    while True:
        self.numbers_ori = [random.randint(1, 10) for i in range(4)]
        self.numbers_now = copy.deepcopy(self.numbers_ori)
        self.answers = self.__verify()
        if self.answers:
            break

在验证4个数字是否有解并求出所有解部分,我直接暴力枚举然后去重了,感兴趣的同学可以自己再优化一下求解算法(有数字重复的时候)。我的代码如下图所示,其实就是递归枚举所有排序然后一一验证是否有解:

?
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
'''验证生成的数字是否有答案'''
def __verify(self):
    answers = []
    for item in self.__iter(self.numbers_ori, len(self.numbers_ori)):
        item_dict = []
        list(map(lambda i: item_dict.append({str(i): i}), item))
        solution1 = self.__func(self.__func(self.__func(item_dict[0], item_dict[1]), item_dict[2]), item_dict[3])
        solution2 = self.__func(self.__func(item_dict[0], item_dict[1]), self.__func(item_dict[2], item_dict[3]))
        solution = dict()
        solution.update(solution1)
        solution.update(solution2)
        for key, value in solution.items():
            if float(value) == self.target:
                answers.append(key)
    # 避免有数字重复时表达式重复(T_T懒得优化了)
    answers = list(set(answers))
    return answers
'''递归枚举'''
def __iter(self, items, n):
    for idx, item in enumerate(items):
        if n == 1:
            yield [item]
        else:
            for each in self.__iter(items[:idx]+items[idx+1:], n-1):
                yield [item] + each
'''计算函数'''
def __func(self, a, b):
    res = dict()
    for key1, value1 in a.items():
        for key2, value2 in b.items():
            res.update({'('+key1+'+'+key2+')': value1+value2})
            res.update({'('+key1+'-'+key2+')': value1-value2})
            res.update({'('+key2+'-'+key1+')': value2-value1})
            res.update({'('+key1+'×'+key2+')': value1*value2})
            value2 > 0 and res.update({'('+key1+'÷'+key2+')': value1/value2})
            value1 > 0 and res.update({'('+key2+'÷'+key1+')': value2/value1})
    return res

Step2:定义游戏精灵类

因为玩家需要通过鼠标点击来操作卡片,这时候就涉及到一些碰撞检测。所以先定义一些必要的游戏精灵类。

①卡片类

卡片类的定义也很简单,在屏幕上根据被赋予的属性值来显示自身即可。当然之后也需要根据用户的操作来改变这些属性值(内容、颜色、字体等)并在屏幕上根据属性的改变而改变显示状态即可。具体而言代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Card(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.attribute = attribute
        self.font_info = font
        self.font = pygame.font.Font(font[0], font[1])
        self.font_colors = font_colors
        self.is_selected = False
        self.select_order = None
        self.bg_colors = bg_colors
    '''画到屏幕上'''
    def draw(self, screen, mouse_pos):
        pygame.draw.rect(screen, self.bg_colors[1], self.rect, 0)
        if self.rect.collidepoint(mouse_pos):
            pygame.draw.rect(screen, self.bg_colors[0], self.rect, 0)
        font_color = self.font_colors[self.is_selected]
        text_render = self.font.render(self.text, True, font_color)
        font_size = self.font.size(self.text)
        screen.blit(text_render, (self.rect.x+(self.rect.width-font_size[0])/2,
                                  self.rect.y+(self.rect.height-font_size[1])/2))

②按钮类

按钮类和卡片类类似,唯一的不同点就是在用户点击按钮时需要根据该按钮的功能来响应用户的本次点击操作(即实现一次该功能)。因此只需要继承卡片类,然后再定义一个响应用户点击按钮事件的回调函数即可。代码实现如下:

?
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
class Button(Card):
    def __init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute, **kwargs):
        Card.__init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute)
    '''根据button function执行响应操作'''
    def do(self, game24_gen, func, sprites_group, objs):
        if self.attribute == 'NEXT':
            for obj in objs:
                obj.font = pygame.font.Font(obj.font_info[0], obj.font_info[1])
                obj.text = obj.attribute
            self.font = pygame.font.Font(self.font_info[0], self.font_info[1])
            self.text = self.attribute
            game24_gen.generate()
            sprites_group = func(game24_gen.numbers_now)
        elif self.attribute == 'RESET':
            for obj in objs:
                obj.font = pygame.font.Font(obj.font_info[0], obj.font_info[1])
                obj.text = obj.attribute
            game24_gen.numbers_now = game24_gen.numbers_ori
            game24_gen.answers_idx = 0
            sprites_group = func(game24_gen.numbers_now)
        elif self.attribute == 'ANSWERS':
            self.font = pygame.font.Font(self.font_info[0], 20)
            self.text = '[%d/%d]: ' % (game24_gen.answers_idx+1, len(game24_gen.answers)) + game24_gen.answers[game24_gen.answers_idx]
            game24_gen.answers_idx = (game24_gen.answers_idx+1) % len(game24_gen.answers)
        else:
            raise ValueError('Button.attribute unsupport <%s>, expect <%s>, <%s> or <%s>...' % (self.attribute, 'NEXT', 'RESET', 'ANSWERS'))
        return sprites_group

Step3:实现游戏主循环

先构思一下怎么设计游戏主界面,个人的简单设计草图如下(不是特别走心的设计草图T_T):

详解如何利用Python制作24点小游戏

OK,开搞。先初始化、加载必要的素材和定义必要的变量,代码实现如下:

?
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
    # 初始化, 导入必要的游戏素材
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(SCREENSIZE)
pygame.display.set_caption('24 point - 微信公众号: Charles的皮卡丘')
win_sound = pygame.mixer.Sound(AUDIOWINPATH)
lose_sound = pygame.mixer.Sound(AUDIOLOSEPATH)
warn_sound = pygame.mixer.Sound(AUDIOWARNPATH)
pygame.mixer.music.load(BGMPATH)
pygame.mixer.music.play(-1, 0.0)
# 24点游戏生成器
game24_gen = game24Generator()
game24_gen.generate()
# 精灵组
# --数字
number_sprites_group = getNumberSpritesGroup(game24_gen.numbers_now)
# --运算符
operator_sprites_group = getOperatorSpritesGroup(OPREATORS)
# --按钮
button_sprites_group = getButtonSpritesGroup(BUTTONS)
# 游戏主循环
clock = pygame.time.Clock()
selected_numbers = []
selected_operators = []
selected_buttons = []
is_win = False

游戏主循环主要分三个部分,首先是按键检测:

?
1
2
3
4
5
6
7
8
9
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit(-1)
    elif event.type == pygame.MOUSEBUTTONUP:
        mouse_pos = pygame.mouse.get_pos()
        selected_numbers = checkClicked(number_sprites_group, mouse_pos, 'NUMBER')
        selected_operators = checkClicked(operator_sprites_group, mouse_pos, 'OPREATOR')
        selected_buttons = checkClicked(button_sprites_group, mouse_pos, 'BUTTON')

根据检测结果更新卡片状态和一些变量:

?
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
'''检查控件是否被点击'''
def checkClicked(group, mouse_pos, group_type='NUMBER'):
    selected = []
    # 数字卡片/运算符卡片
    if group_type == GROUPTYPES[0] or group_type == GROUPTYPES[1]:
        max_selected = 2 if group_type == GROUPTYPES[0] else 1
        num_selected = 0
        for each in group:
            num_selected += int(each.is_selected)
        for each in group:
            if each.rect.collidepoint(mouse_pos):
                if each.is_selected:
                    each.is_selected = not each.is_selected
                    num_selected -= 1
                    each.select_order = None
                else:
                    if num_selected < max_selected:
                        each.is_selected = not each.is_selected
                        num_selected += 1
                        each.select_order = str(num_selected)
            if each.is_selected:
                selected.append(each.attribute)
    # 按钮卡片
    elif group_type == GROUPTYPES[2]:
        for each in group:
            if each.rect.collidepoint(mouse_pos):
                each.is_selected = True
                selected.append(each.attribute)
    # 抛出异常
    else:
        raise ValueError('checkClicked.group_type unsupport <%s>, expect <%s>, <%s> or <%s>...' % (group_type, *GROUPTYPES))
    return selected

当有两个数字和一个运算符被点击时,则执行被点击数字1{+/-/×/÷}被点击数字2操作(数字1、2根据点击顺序确定),并进一步更新卡片属性和一些必要的变量:

?
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
if len(selected_numbers) == 2 and len(selected_operators) == 1:
    noselected_numbers = []
    for each in number_sprites_group:
        if each.is_selected:
            if each.select_order == '1':
                selected_number1 = each.attribute
            elif each.select_order == '2':
                selected_number2 = each.attribute
            else:
                raise ValueError('Unknow select_order <%s>, expect <1> or <2>...' % each.select_order)
        else:
            noselected_numbers.append(each.attribute)
        each.is_selected = False
    for each in operator_sprites_group:
        each.is_selected = False
    result = calculate(selected_number1, selected_number2, *selected_operators)
    if result is not None:
        game24_gen.numbers_now = noselected_numbers + [result]
        is_win = game24_gen.check()
        if is_win:
            win_sound.play()
        if not is_win and len(game24_gen.numbers_now) == 1:
            lose_sound.play()
    else:
        warn_sound.play()
    selected_numbers = []
    selected_operators = []
    number_sprites_group = getNumberSpritesGroup(game24_gen.numbers_now)

最后根据各个卡片的属性在屏幕上显示各个卡片,若游戏胜利/游戏失败,则同时显示游戏胜利/游戏失败提示框:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 精灵都画到screen上
for each in number_sprites_group:
    each.draw(screen, pygame.mouse.get_pos())
for each in operator_sprites_group:
    each.draw(screen, pygame.mouse.get_pos())
for each in button_sprites_group:
    if selected_buttons and selected_buttons[0] in ['RESET', 'NEXT']:
        is_win = False
    if selected_buttons and each.attribute == selected_buttons[0]:
        each.is_selected = False
        number_sprites_group = each.do(game24_gen, getNumberSpritesGroup, number_sprites_group, button_sprites_group)
        selected_buttons = []
    each.draw(screen, pygame.mouse.get_pos())
# 游戏胜利
if is_win:
    showInfo('Congratulations', screen)
# 游戏失败
if not is_win and len(game24_gen.numbers_now) == 1:
    showInfo('Game Over', screen)
pygame.display.flip()
clock.tick(30)

到此这篇关于详解如何利用Python制作24点小游戏的文章就介绍到这了,更多相关Python24点游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://zhuanlan.zhihu.com/p/62839375

延伸 · 阅读

精彩推荐
  • PythonPython中使用PIPE操作Linux管道

    Python中使用PIPE操作Linux管道

    这篇文章主要介绍了Python中使用PIPE操作Linux管道,本文先是讲解了一些管道的知识,然后给出示例代码,需要的朋友可以参考下 ...

    脚本之家7422020-05-22
  • Pythonpython 如何通过KNN来填充缺失值

    python 如何通过KNN来填充缺失值

    这篇文章主要介绍了python 通过KNN来填充缺失值的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    六mo神剑12422021-11-09
  • PythonPython中random模块用法实例分析

    Python中random模块用法实例分析

    这篇文章主要介绍了Python中random模块用法,实例分析了Python中random模块的使用技巧及字符串操作相关方法,需要的朋友可以参考下 ...

    MXi4oyu3092020-07-02
  • Python用Python做个自动化弹钢琴脚本实现天空之城弹奏

    用Python做个自动化弹钢琴脚本实现天空之城弹奏

    突然灵机一动,能不能用Python自动化脚本弹奏一曲美妙的钢琴曲呢?今天就一起带大家如何用Python实现自动化弹出一首《天空之城》有需要的朋友可以借鉴...

    Dragon少年4072022-01-11
  • PythonPython实现配置文件备份的方法

    Python实现配置文件备份的方法

    这篇文章主要介绍了Python实现配置文件备份的方法,实例分析了基于Linux平台下Python文件压缩备份的相关技巧,具有一定参考借鉴价值...

    Linjianying1104242020-07-28
  • PythonPyCharm代码格式调整方法

    PyCharm代码格式调整方法

    今天小编就为大家分享一篇PyCharm代码格式调整方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    HuaCode11582021-02-23
  • Pythonpyqt 实现QlineEdit 输入密码显示成圆点的方法

    pyqt 实现QlineEdit 输入密码显示成圆点的方法

    今天小编就为大家分享一篇pyqt 实现QlineEdit 输入密码显示成圆点的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    这、一年11772021-07-19
  • Pythonpython 6行代码制作月历生成器

    python 6行代码制作月历生成器

    这篇文章主要介绍了python如何用6行代码制作月历生成器,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下...

    Alex279332602020-09-18