最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+Pygame實現(xiàn)之走四棋兒游戲的實現(xiàn)

 更新時間:2022年07月20日 16:43:38   作者:木木子學(xué)python  
大家以前應(yīng)該都聽說過一個游戲:叫做走四棋兒。直接在家里的水泥地上用燒完的炭火灰畫出幾條線,擺上幾顆石頭子即可。當(dāng)時的火爆程度可謂是達到了一個新的高度。本文將利用Pygame實現(xiàn)這一游戲,需要的可以參考一下

導(dǎo)語

大家以前應(yīng)該都聽說過一個游戲:叫做走四棋兒

這款游戲出來到現(xiàn)在時間挺長了,小時候的家鄉(xiāng)農(nóng)村條件有限,附近也沒有正式的玩具店能買到玩具,因此小朋友們聚在一起玩耍時,其玩具大多都是就地取材的。

直接在家里的水泥地上用燒完的炭火灰畫出幾條線,擺上幾顆石頭子即可。當(dāng)時的火爆程度可謂是達到了一個新的高度。包括我當(dāng)時也比較喜歡這款游戲。因為時間推移,小時候很多游戲都已經(jīng)在現(xiàn)在這個時代看不到啦!

今天小編就帶大家追憶童年——一起敲一敲《走四棋兒》小游戲,你小時候還玩兒過那些游戲呢?(抓石頭、跳繩、丟手絹兒 .......捂臉.jpg)

一、游戲解說

“走四兒”大部分活躍在山東濟南、聊城、菏澤等地,是一種棋類游戲,特別適合兒童試玩。

在一個4×4的棋盤上,雙方各有4子,分別擺放在棋盤兩個最上面的兩端線的四個位置上。下圖

就是“走四兒”開局的樣子。

二、游戲規(guī)則

“走四兒”的游戲規(guī)則是:

1.雙方輪流走,每一步只能在上下左右中的一個無子的方向上走一個格,不能斜走。如果一方無法移動,則由另一方走。

2.當(dāng)甲方的一個子移動到一條線上之后,這條線上只有甲方的兩個子和乙方的一個子,且甲方的這兩子相連,乙方的子與甲方那兩子中的一個子相連,那么乙方的這個子就被吃掉。

下圖是可以吃子的樣式例舉:

3.少于2個子的一方為輸者。雙方都無法勝對方就可以認(rèn)為是和棋。

三、環(huán)境安裝

 1)素材(圖片) 

 2)運行環(huán)境 小編使用的環(huán)境:Python3、Pycharm社區(qū)版、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()

五、效果展示

?是不是效果跟上面的示例圖很像啦~棋子效果太死板啦!感覺會更加好看點兒撒!小編只是把雙方的棋子換成了紅心??跟黑桃♠啦!大家可以自己更換的哈~

走棋的效果我就不一一展示了哈,跟著游戲規(guī)則大家可以自己來探索哦!

以上就是Python+Pygame實現(xiàn)之走四棋兒游戲的實現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame走四棋兒游戲的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實現(xiàn)

    Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實現(xiàn)

    本文主要介紹了Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Python中的類型提示(Type Hints)總結(jié)

    Python中的類型提示(Type Hints)總結(jié)

    Python3.5 版本引入了類型提示(Type Hints),它允許開發(fā)者在代碼中顯式地聲明變量、函數(shù)、方法等的類型信息,下面小編就來帶大家一起看看Python類型提示的初步使用吧
    2023-05-05
  • 詳解Python中的文件操作

    詳解Python中的文件操作

    這篇文章主要介紹了Python中文件操作的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2021-01-01
  • Python如何使用PIL Image制作GIF圖片

    Python如何使用PIL Image制作GIF圖片

    這篇文章主要介紹了Python如何使用PIL Image制作GIF圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • python中if嵌套命令實例講解

    python中if嵌套命令實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python中if嵌套命令實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-02-02
  • Python 實現(xiàn)Excel XLS和XLSX格式相互轉(zhuǎn)換問題

    Python 實現(xiàn)Excel XLS和XLSX格式相互轉(zhuǎn)換問題

    本文介紹如何使用Python庫Spire.XLS for Python實現(xiàn)Excel文件的XLS和XLSX格式轉(zhuǎn)換,提供了詳細(xì)的安裝指南和轉(zhuǎn)換步驟,幫助用戶在不同版本的Excel文件格式之間靈活轉(zhuǎn)換,同時支持將Excel文件轉(zhuǎn)換為PDF、圖片、HTML等格式
    2024-10-10
  • pytorch VGG11識別cifar10數(shù)據(jù)集(訓(xùn)練+預(yù)測單張輸入圖片操作)

    pytorch VGG11識別cifar10數(shù)據(jù)集(訓(xùn)練+預(yù)測單張輸入圖片操作)

    這篇文章主要介紹了pytorch VGG11識別cifar10數(shù)據(jù)集(訓(xùn)練+預(yù)測單張輸入圖片操作),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python如何實現(xiàn)逐行讀取文本文件

    Python如何實現(xiàn)逐行讀取文本文件

    在Python中,有幾種方法可以讀取文本文件。在本文中,將介紹以下幾種方法:open()?函數(shù)、read()?方法、readline()?方法、readlines()?方法、close()?方法和?with?關(guān)鍵字。需要的同學(xué)可以參考一下
    2021-12-12
  • Python之字典對象的幾種創(chuàng)建方法

    Python之字典對象的幾種創(chuàng)建方法

    這篇文章主要介紹了Python之字典對象的幾種創(chuàng)建方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 在Apache服務(wù)器上同時運行多個Django程序的方法

    在Apache服務(wù)器上同時運行多個Django程序的方法

    這篇文章主要介紹了在Apache服務(wù)器上同時運行多個Django程序的方法,Django是Python各色高人氣web框架中最為著名的一個,需要的朋友可以參考下
    2015-07-07

最新評論

凌海市| 都昌县| 罗田县| 霍山县| 山丹县| 长顺县| 荥阳市| 天全县| 黄冈市| 东明县| 平遥县| 昂仁县| 安徽省| 雅安市| 航空| 乌恰县| 花莲县| 丁青县| 黎城县| 德保县| 清镇市| 尉犁县| 方城县| 新民市| 修水县| 高密市| 麦盖提县| 金川县| 合阳县| 砚山县| 中西区| 呼伦贝尔市| 惠安县| 湘潭市| 上思县| 嘉兴市| 克什克腾旗| 海晏县| 通化县| 富源县| 石城县|