iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >如何使用Python+Pygame实现走四棋儿游戏
  • 895
分享到

如何使用Python+Pygame实现走四棋儿游戏

2023-07-06 12:07:11 895人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

今天小编给大家分享一下如何使用python+Pygame实现走四棋儿游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、游

今天小编给大家分享一下如何使用python+Pygame实现走四棋儿游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、游戏解说

“走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。

在一个4×4的棋盘上,双方各有4子,分别摆放在棋盘两个最上面的两端线的四个位置上。下图

就是“走四儿”开局的样子。

如何使用Python+Pygame实现走四棋儿游戏

二、游戏规则

“走四儿”的游戏规则是:

1.双方轮流走,每一步只能在上下左右中的一个无子的方向上走一个格,不能斜走。如果一方无法移动,则由另一方走。

2.当甲方的一个子移动到一条线上之后,这条线上只有甲方的两个子和乙方的一个子,且甲方的这两子相连,乙方的子与甲方那两子中的一个子相连,那么乙方的这个子就被吃掉。

下图是可以吃子的样式例举:

如何使用Python+Pygame实现走四棋儿游戏

3.少于2个子的一方为输者。双方都无法胜对方就可以认为是和棋。

三、环境安装

1)素材(图片)

如何使用Python+Pygame实现走四棋儿游戏

2)运行环境 小编使用的环境:python3PyCharm社区版、Pygame、 numpy模块部分自带就不一一 展示啦。

模块安装:pip install -i https://pypi.douban.com/simple/+模块名

四、代码展示

import pygame as pgfrom pygame.locals import *import sysimport timeimport numpy as nppg.init()size = width, height = 600, 400screen = pg.display.set_mode(size)f_clock = pg.time.Clock()fps = 30pg.display.set_caption("走四棋儿")background = pg.image.load("background.png").convert_alpha()glb_pos = [[(90, 40), (190, 40), (290, 40), (390, 40)],           [(90, 140), (190, 140), (290, 140), (390, 140)],           [(90, 240), (190, 240), (290, 240), (390, 240)],           [(90, 340), (190, 340), (290, 340), (390, 340)]]class ChessPieces():    def __init__(self, img_name):        self.name = img_name        self.id = None        if self.name == 'heart':            self.id = 2        elif self.name == 'spade':            self.id = 3        self.img = pg.image.load(img_name + ".png").convert_alpha()        self.rect = self.img.get_rect()        self.pos_x, self.pos_y = 0, 0        self.alive_state = True    def get_rect(self):        return (self.rect[0], self.rect[1])    def get_pos(self):        return (self.pos_x, self.pos_y)    def update(self):        if self.alive_state == True:            self.rect[0] = glb_pos[self.pos_y][self.pos_x][0]            self.rect[1] = glb_pos[self.pos_y][self.pos_x][1]            screen.blit(self.img, self.rect)class Pointer():    def __init__(self):        self.img = pg.image.load("pointer.png").convert_alpha()        self.rect = self.img.get_rect()        self.show = False        self.selecting_item = False    def point_to(self, Heart_Blade_class):        if Heart_Blade_class.alive_state:            self.pointing_to_item = Heart_Blade_class            self.item_pos = Heart_Blade_class.get_rect()            self.rect[0], self.rect[1] = self.item_pos[0], self.item_pos[1] - 24    def update(self):        screen.blit(self.img, self.rect)class GlobalSituation():    def __init__(self):        self.glb_situation = np.array([[2, 2, 2, 2],                                       [0, 0, 0, 0],                                       [0, 0, 0, 0],                                       [3, 3, 3, 3]], dtype=np.uint8)        self.spade_turn = None    def refresh_situation(self):        self.glb_situation = np.zeros([4, 4], np.uint8)        for i in range(4):            if heart[i].alive_state:                self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id        for i in range(4):            if spade[i].alive_state:                self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id        for i in range(4):            print(self.glb_situation[i][:])        print('=' * 12)        if self.spade_turn != None:            self.spade_turn = not self.spade_turn    def check_situation(self, moved_item):        curr_pos_x, curr_pos_y = moved_item.get_pos()        curr_pos_col = self.glb_situation[:, curr_pos_x]        curr_pos_raw = self.glb_situation[curr_pos_y, :]        enemy_die = False        if moved_item.id == 2:            if np.sum(curr_pos_col) == 7:                if (curr_pos_col == np.array([0, 2, 2, 3])).all():                    enemy_die = True                    self.glb_situation[3, curr_pos_x] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 3:                            spade_i.alive_state = False                elif (curr_pos_col == np.array([2, 2, 3, 0])).all():                    enemy_die = True                    self.glb_situation[2, curr_pos_x] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 2:                            spade_i.alive_state = False                elif (curr_pos_col == np.array([0, 3, 2, 2])).all():                    enemy_die = True                    self.glb_situation[1, curr_pos_x] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 1:                            spade_i.alive_state = False                elif (curr_pos_col == np.array([3, 2, 2, 0])).all():                    enemy_die = True                    self.glb_situation[0, curr_pos_x] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 0:                            spade_i.alive_state = False            if np.sum(curr_pos_raw) == 7:                if (curr_pos_raw == np.array([0, 2, 2, 3])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 3] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == 3 and spade_i.pos_y == curr_pos_y:                            spade_i.alive_state = False                elif (curr_pos_raw == np.array([2, 2, 3, 0])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 2] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == 2 and spade_i.pos_y == curr_pos_y:                            spade_i.alive_state = False                elif (curr_pos_raw == np.array([0, 3, 2, 2])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 1] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == 1 and spade_i.pos_y == curr_pos_y:                            spade_i.alive_state = False                elif (curr_pos_raw == np.array([3, 2, 2, 0])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 0] = 0                    for spade_i in spade:                        if spade_i.alive_state and spade_i.pos_x == 0 and spade_i.pos_y == curr_pos_y:                            spade_i.alive_state = False        elif moved_item.id == 3:            if np.sum(curr_pos_col) == 8:                if (curr_pos_col == np.array([0, 3, 3, 2])).all():                    enemy_die = True                    self.glb_situation[3, curr_pos_x] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 3:                            heart_i.alive_state = False                elif (curr_pos_col == np.array([3, 3, 2, 0])).all():                    enemy_die = True                    self.glb_situation[2, curr_pos_x] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 2:                            heart_i.alive_state = False                elif (curr_pos_col == np.array([0, 2, 3, 3])).all():                    enemy_die = True                    self.glb_situation[1, curr_pos_x] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 1:                            heart_i.alive_state = False                elif (curr_pos_col == np.array([2, 3, 3, 0])).all():                    enemy_die = True                    self.glb_situation[0, curr_pos_x] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 0:                            heart_i.alive_state = False            if np.sum(curr_pos_raw) == 8:                if (curr_pos_raw == np.array([0, 3, 3, 2])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 3] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == 3 and heart_i.pos_y == curr_pos_y:                            heart_i.alive_state = False                elif (curr_pos_raw == np.array([3, 3, 2, 0])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 2] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == 2 and heart_i.pos_y == curr_pos_y:                            heart_i.alive_state = False                elif (curr_pos_raw == np.array([0, 2, 3, 3])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 1] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == 1 and heart_i.pos_y == curr_pos_y:                            heart_i.alive_state = False                elif (curr_pos_raw == np.array([2, 3, 3, 0])).all():                    enemy_die = True                    self.glb_situation[curr_pos_y, 0] = 0                    for heart_i in heart:                        if heart_i.alive_state and heart_i.pos_x == 0 and heart_i.pos_y == curr_pos_y:                            heart_i.alive_state = False        if enemy_die == True:            self.glb_situation = np.zeros([4, 4], np.uint8)            for i in range(4):                if heart[i].alive_state:                    self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id            for i in range(4):                if spade[i].alive_state:                    self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id            for i in range(4):                print(self.glb_situation[i][:])            print('=' * 12)    def check_game_over(self):        heart_alive_num, spade_alive_num = 0, 0        for heart_i in heart:            if heart_i.alive_state:                heart_alive_num += 1        for spade_i in spade:            if spade_i.alive_state:                spade_alive_num += 1        if heart_alive_num <= 1:            print('Spades win!')            GlobalSituation.__init__(self)            Pointer.__init__(self)            chess_pieces_init()        if spade_alive_num <= 1:            print('Hearts win!')            GlobalSituation.__init__(self)            Pointer.__init__(self)            chess_pieces_init()heart, spade = [None] * 4, [None] * 4for i in range(4):    heart[i] = ChessPieces('heart')    spade[i] = ChessPieces('spade')def chess_pieces_init():    for i in range(4):        heart[i].pos_y, heart[i].pos_x = 0, i        spade[i].pos_y, spade[i].pos_x = 3, i        heart[i].alive_state = True        spade[i].alive_state = Truechess_pieces_init()pointer = Pointer()situation = GlobalSituation()def check_click_item(c_x, c_y):    selected_item = None    if situation.spade_turn==None:        for heart_i in heart:            if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):                situation.spade_turn = False                selected_item = heart_i        for spade_i in spade:            if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):                situation.spade_turn = True                selected_item = spade_i    else:        if situation.spade_turn:            for spade_i in spade:                if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):                    selected_item = spade_i        else:            for heart_i in heart:                if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):                    selected_item = heart_i    return selected_itemdef move_to_dst_pos(selected_item, c_x, c_y):    update_situation = False    enemy_exist = False    if selected_item.name == 'heart':        for spade_i in spade:            if spade_i.rect.collidepoint(c_x, c_y) and spade_i.alive_state:                enemy_exist = True    elif selected_item.name == 'spade':        for heart_i in heart:            if heart_i.rect.collidepoint(c_x, c_y) and heart_i.alive_state:                enemy_exist = True    if enemy_exist == False:        delta_y, delta_x = c_y - selected_item.rect[1], c_x - selected_item.rect[0]        if 80 <= abs(delta_x) <= 120 and abs(delta_y) <= 20:            if delta_x < 0:                if selected_item.pos_x > 0:                    selected_item.pos_x -= 1            else:                if selected_item.pos_x < 3:                    selected_item.pos_x += 1            update_situation = True        if 80 <= abs(delta_y) <= 120 and abs(delta_x) <= 20:            if delta_y < 0:                if selected_item.pos_y > 0:                    selected_item.pos_y -= 1            else:                if selected_item.pos_y < 3:                    selected_item.pos_y += 1            update_situation = True    return update_situationwhile True:    for event in pg.event.get():        if event.type == pg.QUIT:            sys.exit()        elif event.type == pg.MOUSEBUTTONDOWN:            cursor_x, cursor_y = pg.mouse.get_pos()            clicked_item = check_click_item(cursor_x, cursor_y)            if clicked_item != None:                pointer.selecting_item = True                pointer.point_to(clicked_item)            else:                if pointer.selecting_item:                    update_situation_flag = move_to_dst_pos(pointer.pointing_to_item, cursor_x, cursor_y)                    if update_situation_flag:                        situation.refresh_situation()                        situation.check_situation(pointer.pointing_to_item)                        situation.check_game_over()                pointer.selecting_item = False    screen.blit(background, (0, 0))    for heart_i in heart:        heart_i.update()    for spade_i in spade:        spade_i.update()    if pointer.selecting_item:        pointer.update()    f_clock.tick(fps)    pg.display.update()

五、效果展示

如何使用Python+Pygame实现走四棋儿游戏

以上就是“如何使用Python+Pygame实现走四棋儿游戏”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网Python频道。

--结束END--

本文标题: 如何使用Python+Pygame实现走四棋儿游戏

本文链接: https://www.lsjlt.com/news/358139.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用Python+Pygame实现走四棋儿游戏
    今天小编给大家分享一下如何使用Python+Pygame实现走四棋儿游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、游...
    99+
    2023-07-06
  • 怎么使用Python+Pygame实现走四棋儿游戏
    一、游戏解说“走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。在一个4&times;4的棋盘上,双方各有4子,分别摆放在棋盘两个最上面的两端线的四个位置上。下图就是“走四儿”开局的样子。二、游戏规则“...
    99+
    2023-05-15
    Python Pygame
  • Python+Pygame实现之走四棋儿游戏的实现
    目录导语一、游戏解说二、游戏规则三、环境安装四、代码展示五、效果展示导语 大家以前应该都听说过一个游戏:叫做走四棋儿 这款游戏出来到现在时间挺长了,小时候的家乡农村条件有限,附近也没...
    99+
    2024-04-02
  • 如何使用pygame实现简单五子棋游戏
    小编给大家分享一下如何使用pygame实现简单五子棋游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体内容如下看代码:①Gomuku2.py:import&n...
    99+
    2023-06-28
  • Python+Pygame实现彩色五子棋游戏
    目录项目简介项目背后的故事项目扩展思路运行截图安装依赖运行游戏项目简介 之前学python的时候 写了个游戏来练手 用的是 pygame 没有别的依赖 只用了一两百行的代码就实现了 ...
    99+
    2023-02-10
    Python Pygame彩色五子棋游戏 Python Pygame五子棋游戏 Python Pygame 游戏
  • 如何使用Python Pygame实现24点游戏
    这篇文章主要讲解了“如何使用Python Pygame实现24点游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Python Pygame实现24点游戏”吧!游戏介绍(1)什么是2...
    99+
    2023-07-06
  • 怎么使用python+pygame实现简易五子棋小游戏
    本篇内容主要讲解“怎么使用python+pygame实现简易五子棋小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用python+pygame实现简易五子棋小游戏”吧!tkinter库...
    99+
    2023-07-05
  • python如何实现三子棋游戏
    本文小编为大家详细介绍“python如何实现三子棋游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“python如何实现三子棋游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、基本流程三子棋游戏实现逻辑如下...
    99+
    2023-07-06
  • Python Pygame如何实现经营类游戏
    一、注意事项1.python的字体要尽量小一点 否则视觉上会很难看,调9-11左右 而且一定一定要调Consolas字体 对于win7系统字符画显示可能有问题,因为程序使用制表符, 制表符在win7下存储占2个字符 游戏是win10设计,设...
    99+
    2023-05-17
    Python Pygame
  • Python Pygame如何实现打砖块游戏
    本文小编为大家详细介绍“Python Pygame如何实现打砖块游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python Pygame如何实现打砖块游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来...
    99+
    2023-06-29
  • 如何使用tkinter实现三子棋游戏
    小编给大家分享一下如何使用tkinter实现三子棋游戏,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!整体游戏很简单。下棋,判断胜利或者平局。没有加入电脑下棋的算法。游戏界面:代码:import tkinter&n...
    99+
    2023-06-06
  • 基于Python如何实现围棋游戏
    本篇内容主要讲解“基于Python如何实现围棋游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Python如何实现围棋游戏”吧!1.导入模块tkinter:ttk覆盖tkinter部分对象...
    99+
    2023-06-30
  • 怎么使用Python+Pygame实现泡泡游戏
    这篇文章主要讲解了“怎么使用Python+Pygame实现泡泡游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Python+Pygame实现泡泡游戏”吧!一、环境安装1)素材(图片...
    99+
    2023-07-02
  • 如何用Python实现简易五子棋游戏
    这篇文章主要介绍了如何用Python实现简易五子棋游戏的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用Python实现简易五子棋游戏文章都会有所收获,下面我们一起来看看吧。具体代码如下class ...
    99+
    2023-06-30
  • Python Pygame如何实现水果忍者游戏
    这篇文章给大家分享的是有关Python Pygame如何实现水果忍者游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、准备中1.0 游戏规则Python版本的水果忍者小编初始化设置是玩家3条生命值,...
    99+
    2023-06-29
  • 如何使用java实现马踏棋盘游戏
    小编给大家分享一下如何使用java实现马踏棋盘游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体内容如下在4399小游戏中有这样一个游戏这是代码实现packa...
    99+
    2023-06-29
  • Qt如何实现棋盘游戏
    小编给大家分享一下Qt如何实现棋盘游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!知识点画背景图、线条qDebug()与QString联合使用qDebug()&...
    99+
    2023-06-22
  • python+pygame实现简易五子棋小游戏的三种方式
    目录五子棋小游戏实现(一):  五子棋小游戏实现(二):  五子棋小游戏实现(三): tkinter库:Python的标准Tk GU...
    99+
    2023-03-23
    python 五子棋 python pygame python pygame五子棋
  • 如何用C++实现三子棋游戏
    这篇文章主要介绍了如何用C++实现三子棋游戏的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用C++实现三子棋游戏文章都会有所收获,下面我们一起来看看吧。游戏描述:先来点真实的!三子棋其实就是我们小时候经常玩...
    99+
    2023-06-26
  • Python Pygame如何实现超级炸弹人游戏
    这篇文章给大家分享的是有关Python Pygame如何实现超级炸弹人游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、环境安装1.素材(图片)2.环境安装本文是由Pygame写的小游戏。涉及运行环...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作