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

python實(shí)現(xiàn)五子棋小游戲

 更新時(shí)間:2020年03月25日 09:08:34   作者:在校學(xué)渣一枚  
這篇文章主要介紹了python實(shí)現(xiàn)五子棋小游戲,使用pygame模塊編寫一個(gè)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)五子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下

暑假學(xué)了十幾天python,然后用pygame模塊寫了一個(gè)五子棋的小游戲,代碼跟有緣人分享一下。

import numpy as np
import pygame
import sys
import traceback
import copy
from pygame.locals import *


pygame.init()
pygame.mixer.init()

#顏色
background=(201,202,187)
checkerboard=(80,80,80)
button=(52,53,44)



#音樂
play_chess_sound = pygame.mixer.Sound("music/play_chess.wav")
play_chess_sound.set_volume(0.2)
button_sound = pygame.mixer.Sound("music/button.wav")
button_sound.set_volume(0.2)
victor_sound = pygame.mixer.Sound("music/victory.wav")
victor_sound.set_volume(0.2)

#繪制棋盤
def Draw_a_chessboard(screen): 
 #填充背景色
 screen.fill(background)
 Background=pygame.image.load("background.jpg").convert_alpha()
 screen.blit(Background,(0,0))
 #畫棋盤
 for i in range(21):
 pygame.draw.line(screen, checkerboard, (40*i+3, 3), (40*i+3, 803)) 
 pygame.draw.line(screen, checkerboard, (3, 40*i+3), (803, 40*i+3))
 #畫邊線
 pygame.draw.line(screen, checkerboard, (3, 3), (803, 3),5) 
 pygame.draw.line(screen, checkerboard, (3, 3), (3, 803),5) 
 pygame.draw.line(screen, checkerboard, (803, 3), (803, 803),5) 
 pygame.draw.line(screen, checkerboard, (3, 803), (803, 803),5) 
 
 #畫定位點(diǎn)
 pygame.draw.circle(screen, checkerboard, (163, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (163, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (403, 403), 6) 
 
 #畫‘悔棋'‘重新開始'跟‘退出'按鈕
 pygame.draw.rect(screen,button,[900,350,120,100],5)
 pygame.draw.rect(screen,button,[900,500,200,100],5)
 pygame.draw.rect(screen,button,[900,650,200,100],5)
 s_font=pygame.font.Font('font.ttf',40)
 text1=s_font.render("悔棋",True,button)
 text2=s_font.render("重新開始",True,button)
 text3=s_font.render("退出游戲",True,button)
 screen.blit(text1,(920,370))
 screen.blit(text2,(920,520))
 screen.blit(text3,(920,670))

#繪制棋子(橫坐標(biāo),縱坐標(biāo),屏幕,棋子顏色(1代表黑,2代表白))
def Draw_a_chessman(x,y,screen,color): 
 if color==1: 
 Black_chess=pygame.image.load("Black_chess.png").convert_alpha()
 screen.blit(Black_chess,(40*x+3-15,40*y+3-15))
 if color==2:
 White_chess=pygame.image.load("White_chess.png").convert_alpha()
 screen.blit(White_chess,(40*x+3-15,40*y+3-15))

#繪制帶有棋子的棋盤
def Draw_a_chessboard_with_chessman(map,screen): 
 screen.fill(background)
 Draw_a_chessboard(screen)
 for i in range(24):
 for j in range(24):
 Draw_a_chessman(i+1,j+1,screen,map[i][j])



#定義存儲(chǔ)棋盤的列表,
#列表為24列24行是因?yàn)榕袛嗍欠駝倮瘮?shù)里的索引會(huì)超出19
#列表大一點(diǎn)不會(huì)對(duì)游戲有什么影響
map=[]
for i in range(24):
 map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#清零map列表
def clear():
 global map
 for i in range(24):
 for j in range(24):
 map[i][j]=0

#判斷是否勝利
def win(i, j):
 k = map[i][j]
 p=[]
 for a in range(20):
 p.append(0)
 for i3 in range(i-4,i+5):
 for j3 in range(j-4,j+5):
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j):
 p[0]+=1
 if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j):
 p[1]+=1
 if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j):
 p[2]+=1
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j):
 p[3]+=1
 if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j):
 p[4]+=1
 if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j):
 p[5]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j):
 p[6]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j):
 p[7]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[8]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[9]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[10]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[11]+=1
 if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[12]+=1
 if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[13]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3):
 p[14]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3):
 p[15]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[16]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[17]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[18]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[19]+=1
 for b in range(20):
 if p[b]==5:
 return True
 return False

#繪制提示器(類容,屏幕,字大?。?
def text(s,screen,x):
 #先把上一次的類容用一個(gè)矩形覆蓋
 pygame.draw.rect(screen,background,[850,100,1200,100])
 #定義字體跟大小
 s_font=pygame.font.Font('font.ttf',x)
 #定義類容,是否抗鋸齒,顏色
 s_text=s_font.render(s,True,button)
 #將字放在窗口指定位置
 screen.blit(s_text,(880,100))
 pygame.display.flip()

#用于控制順序
t=True

#用于結(jié)束游戲后阻止落子
running=True

#主函數(shù)
def main():
 #將 t,map,running設(shè)置為可改的
 global t,map,running,maps,r,h
 #將map置零
 clear()
 #定義儲(chǔ)存所有棋盤狀態(tài)的列表(用于悔棋)
 map2=copy.deepcopy(map)
 maps=[map2]

 
 #定義窗口
 screen = pygame.display.set_mode([1200,806])
 
 #定義窗口名字
 pygame.display.set_caption("五子棋")
 
 #在窗口畫出棋盤,提示器以及按鈕
 Draw_a_chessboard(screen)
 pygame.display.flip()
 clock=pygame.time.Clock()
 while True:
 #只有running為真才能落子,主要用于游戲結(jié)束后防止再次落子
 if running:
 if t:
 color=1
 text('黑棋落子',screen,54)
 else:
 color=2
 text('白棋落子',screen,54)
 
 for event in pygame.event.get():
 #點(diǎn)擊x則關(guān)閉窗口
 if event.type ==pygame.QUIT:
 pygame.quit()
 sys.exit()
 
 #點(diǎn)擊窗口里面類容則完成相應(yīng)指令
 elif event.type == MOUSEBUTTONDOWN:
 if event.button == 1:
 x,y=event.pos[0],event.pos[1]
 for i in range(19):
 for j in range(19):
 #點(diǎn)擊棋盤相應(yīng)位置
 if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running:
 #在棋盤相應(yīng)位置落相應(yīng)顏色棋子
 Draw_a_chessman(i+1,j+1,screen,color)
 #播放音效
 play_chess_sound.play(0)
 #在map里面記錄落子位置
 map[i][j]=color

 #將map存入maps
 map3=copy.deepcopy(map)
 maps.append(map3)

 #判斷落子后是否有五子一線
 if win(i,j):
  if t:
  text('黑棋勝利,請(qǐng)重新游戲',screen,30)
  else:
  text('白棋勝利,請(qǐng)重新游戲',screen,30)
  #播放音效
  victor_sound.play(0)
  #阻止再往棋盤落子
  running=False
 pygame.display.flip()
 t=not t
 #如果點(diǎn)擊‘重新開始'
 if 900<x<1100 and 500<y<600:
 #取消阻止
 running=True
 #播放音效
 button_sound.play(0)
 #重新開始
 main()
 
 #點(diǎn)擊‘退出游戲',退出游戲
 elif 900<x<1100 and 650<y<750:
 #播放音效
 button_sound.play(0)
 pygame.quit()
 sys.exit()
 
 #點(diǎn)擊‘悔棋'
 elif 900<x<1020 and 350<y<450 and len(maps)!=1:
 #播放音效
 button_sound.play(0)
 #刪除maps里最后一個(gè)元素
 del maps[len(maps)-1] 
 #再將最后一個(gè)元素copy給map
 map=copy.deepcopy(maps[len(maps)-1])
 #切換順序
 t=not t
 #將map顯示出來
 Draw_a_chessboard_with_chessman(map,screen)
 #悔棋完成,阻止再次悔棋
 x,y=0,0
 clock.tick(60)
if __name__ == "__main__":
 try:
 main()
 except SystemExit:
 pass
 except:
 traceback.print_exc()
 pygame.quit()
 input()

實(shí)現(xiàn)效果圖如下:

更多關(guān)于python游戲的精彩文章請(qǐng)點(diǎn)擊查看以下專題:

python俄羅斯方塊游戲集合

python經(jīng)典小游戲匯總

python微信跳一跳游戲集合

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python使用datetime.utcnow()問題解析

    python使用datetime.utcnow()問題解析

    這篇文章主要介紹了python使用datetime.utcnow()問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法

    python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法

    我相信很多人在學(xué)習(xí)Python的時(shí)候,特別是在open文件的時(shí)候總還碰到,還報(bào)錯(cuò)IOError:[Errno?2]沒有這樣的文件或目錄:'E://aaa.txt',這篇文章主要給大家介紹了關(guān)于python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法,需要的朋友可以參考下
    2022-02-02
  • python版本的仿windows計(jì)劃任務(wù)工具

    python版本的仿windows計(jì)劃任務(wù)工具

    這篇文章主要介紹了python版本的仿windows計(jì)劃任務(wù)工具,計(jì)劃任務(wù)工具根據(jù)自己設(shè)定的具體時(shí)間,頻率,命令等屬性來規(guī)定所要執(zhí)行的計(jì)劃,當(dāng)然功能不是很全大家可以補(bǔ)充
    2018-04-04
  • 如何利用Pyecharts可視化微信好友

    如何利用Pyecharts可視化微信好友

    這篇文章主要給大家介紹了關(guān)于如何利用Pyecharts可視化微信好友的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Pyecharts具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python實(shí)現(xiàn)UDP協(xié)議下的文件傳輸

    python實(shí)現(xiàn)UDP協(xié)議下的文件傳輸

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)UDP協(xié)議下的文件傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Win7下Python與Tensorflow-CPU版開發(fā)環(huán)境的安裝與配置過程

    Win7下Python與Tensorflow-CPU版開發(fā)環(huán)境的安裝與配置過程

    這篇文章主要介紹了Win7下Python與Tensorflow-CPU版安裝與配置心得,需要的朋友可以參考下
    2018-01-01
  • Django配置Redis使用的方法步驟

    Django配置Redis使用的方法步驟

    本文主要介紹了Django配置Redis使用的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景

    關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景

    這篇文章主要介紹了關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景,argparse 模塊使編寫用戶友好的命令行界面變得容易,程序定義了所需的參數(shù),而 argparse 將找出如何從 sys.argv 中解析這些參數(shù),需要的朋友可以參考下
    2023-08-08
  • Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過程

    Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過程

    這篇文章主要介紹了Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過程,通過本文的指導(dǎo),讀者可以學(xué)習(xí)如何使用Python構(gòu)建機(jī)器學(xué)習(xí)模型的API服務(wù),并了解到在實(shí)際應(yīng)用中需要考慮的一些關(guān)鍵問題和解決方案,從而為自己的項(xiàng)目提供更好的支持和服務(wù),需要的朋友可以參考下
    2024-04-04
  • 解決pytorch trainloader遇到的多進(jìn)程問題

    解決pytorch trainloader遇到的多進(jìn)程問題

    這篇文章主要介紹了解決pytorch trainloader遇到的多進(jìn)程問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論

黑龙江省| 桂阳县| 双辽市| 阿荣旗| 综艺| 延安市| 厦门市| 德兴市| 临汾市| 定安县| 靖安县| 黔东| 普兰县| 榆林市| 珲春市| 顺义区| 封开县| 呼图壁县| 永福县| 象山县| 宕昌县| 彭泽县| 阳高县| 同德县| 东兰县| 西贡区| 哈尔滨市| 类乌齐县| 西青区| 北宁市| 宣恩县| 曲麻莱县| 女性| 海原县| 微博| 汉阴县| 佛学| 商南县| 雷州市| 彭山县| 蓝田县|