亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python+Pygame實(shí)現(xiàn)彩色五子棋游戲

 更新時(shí)間:2023年02月10日 10:02:32   作者:武子康  
這篇文章主要為大家詳細(xì)介紹了如何溧陽Python和Pygame實(shí)現(xiàn)彩色五子棋游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

項(xiàng)目簡介

之前學(xué)python的時(shí)候 寫了個(gè)游戲來練手 用的是 pygame 沒有別的依賴

只用了一兩百行的代碼就實(shí)現(xiàn)了 整體來說功能并不算完整

項(xiàng)目背后的故事

這個(gè)項(xiàng)目是在大學(xué)的時(shí)候

偶然一個(gè)機(jī)遇交一個(gè)小朋友Python時(shí) 小朋友大概10多歲 正在打算上初一

小朋友分非常非常非常聰明?。?!

當(dāng)時(shí)給他講東西 他很快就可以接受 立馬就可以模仿出來

小朋友會(huì)的東西很多 其中一項(xiàng)我非常感興趣哈哈 — 圍棋 好像還是業(yè)余挺高的那種(不好意思 我不太懂段位)

好像是什么定段之后就可以打職業(yè)那種?對(duì)我來說是非常非常厲害的存在了

當(dāng)時(shí)我還讓他簡單的交了交我如何下圍棋 以及圍棋的一些概念

除了五子棋之外 當(dāng)時(shí)還寫了 貪吃蛇、掃雷等等這些游戲

還給他講了爬蟲相關(guān)的東西 還有HTML啊CSS之類的

當(dāng)時(shí)有一個(gè)游戲叫 “人類資源機(jī)器(HumanResource)” 游戲是一個(gè)通過簡單編程 控制小人來實(shí)現(xiàn)目標(biāo)的游戲

就是這個(gè)游戲!當(dāng)時(shí)我很驚訝 他過關(guān)速度非???!搞得我壓力都上來了哈哈

當(dāng)時(shí)還準(zhǔn)備了幾頁的 “課本” 方便小朋友以后能夠回看

項(xiàng)目擴(kuò)展思路

當(dāng)然圍棋其實(shí)也是一個(gè)道理 只是計(jì)算勝負(fù)、計(jì)算氣的邏輯會(huì)不一樣

可以改進(jìn)一下 使用鼠標(biāo)來落子會(huì)更有意思

大家可以參考一下 主項(xiàng)目在GitHub上 除了單機(jī)版以外還有一個(gè)局域網(wǎng)版

運(yùn)行截圖

安裝依賴

pip install pygame

或者

pip3 install pygame

運(yùn)行游戲

將游戲代碼保存后 直接運(yùn)行即可

上下左右移動(dòng)光標(biāo) 空格落子

import pygame

# 初始化
pygame.init()
# 設(shè)置窗口標(biāo)題
screencaption=pygame.display.set_caption('Gobang')
# 設(shè)置大小
screen=pygame.display.set_mode([350,285])

# 初始化字體
myfont=pygame.font.Font(None,30)
textImage=myfont.render("Hello Pygame",True,[255,255,255])
screen.blit(textImage,(100,100))

# 棋子狀態(tài)0為空 1為白色 2為黑色
status_list = {}
for i in range(0, 15*18):
    status_list[i] = 0  
#print(status_list)

clock = pygame.time.Clock()

# 0 是白棋走 1是黑棋走
flag = 0
# 將要繪制的棋子的位置
movex = 1
movey = 1
while True:
    clock.tick(30)
    
    # 繪制棋盤
    screen.fill([255,255,255])
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[0,i*20],[280,i*20],2)
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[i*20,0],[i*20,280],2)
    
    # 繪制棋子
    for x in range(0, 15):
        for y in range(0, 15):
            if status_list[x*15 + y] == 1:
                pygame.draw.circle(screen,[255,0,0],[ 2 + y * 20,2 + x*20],10)
            elif status_list[x*15 + y] == 2:
                pygame.draw.circle(screen,[0,0,0],[ 2 + y * 20, 2 + x*20],10)
            # 判斷是否獲勝
            # X軸的判定
            if y < 11:
                # 白棋獲勝
                if status_list[x*15 + y] == 1 and status_list[x*15 + y + 1] == 1 and status_list[x*15 + y + 2] == 1 and status_list[x*15 + y + 3] == 1 and status_list[x*15 + y + 4] == 1:
                    print("白棋勝利")
                    # break
                    
                # 黑棋獲勝
                if status_list[x*15 + y] == 2 and status_list[x*15 + y + 1] == 2 and status_list[x*15 + y + 2] == 2 and status_list[x*15 + y + 3] == 2 and status_list[x*15 + y + 4] == 2:
                    print("黑棋勝利")
                    # break

            # 判斷是否獲勝
            # Y軸的判定
            if x < 11:
                if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + y] == 1 and status_list[(x+2)*15 + y] == 1 and status_list[(x+3)*15 + y] == 1 and status_list[(x+4)*15 + y] == 1:
                    print("白棋勝利")
                    # break
                    
                if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + y] == 2 and status_list[(x+2)*15 + y] == 2 and status_list[(x+3)*15 + y] == 2 and status_list[(x+4)*15 + y] == 2:
                    print("黑棋勝利")
                    # break

            # 判斷是否獲勝
            # 斜著判斷 正對(duì)角線
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y+1)] == 1 and status_list[(x+2)*15 + (y+2)] == 1 and status_list[(x+3)*15 + (y+3)] == 1 and status_list[(x+4)*15 + (y+4)] == 1:
                print("白棋勝利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y+1)] == 2 and status_list[(x+2)*15 + (y+2)] == 2 and status_list[(x+3)*15 + (y+3)] == 2 and status_list[(x+4)*15 + (y+4)] == 2:
                print("黑棋勝利")
                # break
            # 判斷是否獲勝
            # 斜著判斷 反對(duì)角線
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y-1)] == 1 and status_list[(x+2)*15 + (y-2)] == 1 and status_list[(x+3)*15 + (y-3)] == 1 and status_list[(x+4)*15 + (y-4)] == 1:
                print("白棋勝利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y-1)] == 2 and status_list[(x+2)*15 + (y-2)] == 2 and status_list[(x+3)*15 + (y-3)] == 2 and status_list[(x+4)*15 + (y-4)] == 2:
                print("黑棋勝利")
                # break
    # 繪制落棋位置
    pygame.draw.circle(screen,[0,0,0],[ 2 + movex*20, 2 + movey*20],10,3)
    
    # 繪制文字 顯示到誰落棋子
    if flag == 0: 
        textImage=myfont.render("White",True,[255,0,0])
    else:
        textImage=myfont.render("Black",True,[0,0,255])
    screen.blit(textImage,(290,10))
	
    # 判斷事件
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
        # 鍵盤事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if movex > 0:
                    movex = movex - 1
            if event.key == pygame.K_RIGHT:
                if movex < 14:
                    movex = movex + 1
            if event.key == pygame.K_UP:
                if movey > 0:
                    movey = movey - 1
            if event.key == pygame.K_DOWN:
                if movey < 14:
                    movey = movey + 1
            if event.key == pygame.K_SPACE:
                if flag == 0:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 1
                        flag = 1
                elif flag == 1:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 2
                        flag = 0

    # 刷新頁面
    pygame.display.flip()
print("Done!")

以上就是Python+Pygame實(shí)現(xiàn)彩色五子棋游戲的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame彩色五子棋游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python緩存方案優(yōu)化程序性能提高數(shù)據(jù)訪問速度

    Python緩存方案優(yōu)化程序性能提高數(shù)據(jù)訪問速度

    Python緩存方案是一種優(yōu)化程序性能,提高數(shù)據(jù)訪問速度的方案。通過緩存數(shù)據(jù),可以減少重復(fù)的計(jì)算和IO操作,從而提高程序的運(yùn)行效率。Python中常用的緩存方案包括內(nèi)存緩存、磁盤緩存和分布式緩存等,根據(jù)實(shí)際需求選擇不同的方案可以幫助我們更好地優(yōu)化程序性能
    2023-05-05
  • python下paramiko模塊實(shí)現(xiàn)ssh連接登錄Linux服務(wù)器

    python下paramiko模塊實(shí)現(xiàn)ssh連接登錄Linux服務(wù)器

    這篇文章主要介紹了python下paramiko模塊實(shí)現(xiàn)ssh連接登錄Linux服務(wù)器的方法,實(shí)例分析了paramiko模塊實(shí)現(xiàn)ssh連接的具體用法,需要的朋友可以參考下
    2015-06-06
  • python 圖像平移和旋轉(zhuǎn)的實(shí)例

    python 圖像平移和旋轉(zhuǎn)的實(shí)例

    今天小編就為大家分享一篇python 圖像平移和旋轉(zhuǎn)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python集合常見運(yùn)算案例解析

    python集合常見運(yùn)算案例解析

    這篇文章主要介紹了python集合常見運(yùn)算,結(jié)合具體實(shí)例形式分析了Python使用集合生成隨機(jī)數(shù)的幾種常用算法的效率比較,需要的朋友可以參考下
    2019-10-10
  • 一篇文章教你掌握python數(shù)據(jù)類型的底層實(shí)現(xiàn)

    一篇文章教你掌握python數(shù)據(jù)類型的底層實(shí)現(xiàn)

    這篇文章主要介紹了Python 數(shù)據(jù)類型的底層實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-09-09
  • Django 重寫用戶模型的實(shí)現(xiàn)

    Django 重寫用戶模型的實(shí)現(xiàn)

    這篇文章主要介紹了Django 重寫用戶模型的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Scrapy?之中間件(Middleware)的具體使用

    Scrapy?之中間件(Middleware)的具體使用

    本文主要介紹了Scrapy?之中間件(Middleware)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 最新評(píng)論