【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

这篇具有很好参考价值的文章主要介绍了【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

导语

大家以前应该都听说过一个游戏:叫做走四棋儿

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

这款游戏出来到现在时间挺长了,小时候的家乡农村条件有限,附近也没有正式的玩具店能买

到玩具,因此小朋友们聚在一起玩耍时,其玩具大多都是就地取材的。

直接在家里的水泥地上用烧完的炭火灰画出几条线,摆上几颗石头子即可。当时的火爆程度可

谓是达到了一个新的高度。包括我当时也比较喜欢这款游戏。因为时间推移,小时候很多游戏

都已经在现在这个时代看不到啦!

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

今天小编就带大家追忆童年——一起敲一敲《走四棋儿》小游戏,你小时候还玩儿过那些游戏

呢?(抓石头、跳绳、丢手绢儿 .......捂脸.jpg)

所有文章完整的素材+源码都在👇👇

粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

正文

一、游戏解说

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

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

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

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

二、游戏规则

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

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

无法移动,则由另一方走。

2.当甲方的一个子移动到一条线上之后,这条线上只有甲方的两个子和乙方的一个子,且甲方

的这两子相连,乙方的子与甲方那两子中的一个子相连,那么乙方的这个子就被吃掉。

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

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

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

三、环境安装

 1)素材(图片) 

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

 2)运行环境

小编使用的环境:Python3、Pycharm社区版、Pygame、 numpy模块部分自带就不一一 展示啦。 

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

四、代码展示

import pygame as pg
from pygame.locals import *
import sys
import time
import numpy as np

pg.init()
size = width, height = 600, 400
screen = pg.display.set_mode(size)
f_clock = pg.time.Clock()
fps = 30
pg.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] * 4
for 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 = True

chess_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_item


def 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_situation


while 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()

五、效果展示

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)

​是不是效果跟上面的示例图很像啦~棋子效果太死板啦!感觉会更加好看点儿撒!小编只是把双

方的棋子换成了红心💓跟黑桃♠啦!大家可以自己更换的哈~

走棋的效果我就不一一展示了哈,跟着游戏规则大家可以自己来探索哦!

总结

想起这些童年的棋盘游戏真是又兴奋又疲惫,兴奋地是童年趣事那么欢乐,疲惫的是长大后发

现童年“狼心狗肺”一样的玩耍再也一去不复返。

好啦!不说那些“伤感”的话题了,游戏源码给你们,开心一下叭👇

🎯完整的免费源码领取处:找我吖!文末公众hao可自行领取,滴滴我也可!

🔨推荐往期文章——

项目0.1  末世生存游戏

【Pygame实战】末世来临,真正从零开始的残酷生存游戏,你能活多久?

项目1.0  泡泡机游戏

 【Pygame实战】超有趣的泡泡游戏来袭——愿你童心不泯,永远快乐简单哦~

项目8.1   《篮球“王子”》小游戏

 【Pygame实战】趣味篮球——迎“篮”而上 ,乐在“球”中,喜欢打篮球的小可爱前来报道~

项目7.3  想灭病毒保卫城市》游戏

【Pygame实战】疫情期间给不能出门的你推荐一款爽游 《消灭病毒保卫城市》【强推】愿早日结束

项目5.8  翻牌小游戏

Pygame实战:记忆差怎么办?别急,增强记忆力的小游戏送给你~【越玩越上瘾】

🎄文章汇总——

项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

(更多内容+源码都在文章汇总哦!!欢迎阅读~)

【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)文章来源地址https://www.toymoban.com/news/detail-449043.html

到了这里,关于【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 乐趣无限:10款基于Pygame的经典游戏合集

    游戏开发一直是许多程序员和游戏爱好者追求的梦想。而Pygame作为一款功能强大的游戏开发库,为我们提供了实现各种有趣游戏的工具和接口。在本文中,我将向大家介绍10款基于Pygame的经典游戏合集,从简单的猜数字到刺激的飞机大战,让我们一起探索这个无限乐趣的游戏

    2024年02月11日
    浏览(32)
  • 【pygame游戏开发】这几个经典游戏,勾起了少年的快乐

    嗨喽,大家好呀~这里是爱看美女的茜茜呐 今天给大家分享几个好玩有趣的小游戏, 既提升了学习的兴趣,又提升了学习效率,告别枯燥的学习。 python 3.8: 解释器 pycharm: 代码编辑器 一、飞机大战 1. 所需素材 字体 图片 音乐 2. 源码部分 模块导入 游戏界面 主函数 3. 效果展示

    2024年02月03日
    浏览(35)
  • 【pygame游戏开发】这几个经典游戏,小红书Python面试题目

    pygame.time.set_timer(change_hole_event, 800) mole = Mole(cfg.MOLE_IMAGEPATHS, hole_pos) hammer = Hammer(cfg.HAMMER_IMAGEPATHS, (500, 250)) clock = pygame.time.Clock() your_score = 0 flag = False init_time = pygame.time.get_ticks() while True: time_remain = round((61000 - (pygame.time.get_ticks() - init_time)) / 1000.) if time_remain == 40 and not flag: hole

    2024年04月25日
    浏览(31)
  • Python版经典小游戏愤怒的小鸟源代码,基于pygame+pymunk

    Python版经典小游戏愤怒的小鸟源代码,基于pygame+pymunk 程序依赖:pygame 2.0.1, pymunk 5.5.0 直接运行main.py 完整代码下载地址:Python版经典小游戏愤怒的小鸟源代码 tool.py 完整代码下载地址:Python版经典小游戏愤怒的小鸟源代码

    2024年02月16日
    浏览(34)
  • 【PyGame】Rect类实战演示

    Rect是pygame中很重要的一个类,矩形、椭圆以及圆弧的绘制均需要通过Rect来指定,其构造函数要求输入四个参数,分别是(left, top, width, height),即左、上坐标、宽度以及高度。 下面基于Rect对象来创建一个矩形,并通过ijkl四个键对其挪动,效果如下 代码为 其中,player是一个

    2024年02月04日
    浏览(25)
  • 原型图都可以用什么软件做?分享这9款给你

    设计师在进行原型设计师时,会使用原型图软件,从产生想法到向开发人员提交项目。无论是构建基本线框还是功能齐全的原型,原型图软件都可以为你节省大量的时间和精力。 如果你是这个领域的新手或者想更新你的原型图软件包, 请快速看一下这9个原型图软件的分享

    2024年02月10日
    浏览(33)
  • 【pygame学习+实战】第一篇:游戏最小系统

    14天学习训练营导师课程: 李宁《Python Pygame游戏开发入门与实战》 李宁《计算机视觉OpenCV Python项目实战》1 李宁《计算机视觉OpenCV Python项目实战》2 李宁《计算机视觉OpenCV Python项目实战》3 “我有一个梦想,那就是有生之年做出一款属于自己的游戏。” 不知道屏幕前的你是

    2023年04月19日
    浏览(30)
  • 适合新手小白练习的pygame实战项目!

    PyGame是一个用于制作2D游戏的Python库。它提供了许多功能,如游戏开发、音频处理和图形渲染等。PyGame库可用于制作各种类型的游戏,从简单的休闲游戏到复杂的冒险游戏。 跨平台:pygame可以在Windows、Mac和Linux等操作系统上运行。 开源:pygame是一个开源库,开发者可以免费使

    2024年01月25日
    浏览(36)
  • python实战指西<1>pygame安装,以及vscode

    目录 1,安装pygame 1.1,(如果前一个没反应的化) 1.2如果飘红字 1,检查是否开了网络代理(不要开) 2,检查是否有pip模块更新需要 2.这里顺便记录一下vscode 蛇蛇的环境搭载 2.1首先在vscode里下载Python插件  2.2win系统: 2.3关于运行时飙红的字 一般十分简单,命令窗口执行如

    2023年04月08日
    浏览(23)
  • 【python大作业】pygame实战(python编写2048小游戏)

    本文介绍基于pygame编写的2048小游戏程序 包含四个文件 运行效果: 点击此处下载完整程序,下载即可运行 其中config.py用于设置游戏参数 包括游戏窗口大小,刷新率,方块颜色等 game.py中定义了游戏实现的函数,设置方块的产生,移动与计算,并判断游戏进行的程度,判断游

    2024年02月13日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包