【Pygame】 简易五子棋
import pygame
import sys
import time
import random pygame.init()
screen_size = (800,560)
WIDTH = 720
HEIGHT = 720
GRID_WIDTH = WIDTH // 20 WHITE = (255, 250, 255)
BLACK = (0, 0, 0)
GREEN = (0, 0xff, 0)
RED = (0xff, 0, 0)
color = [BLACK,WHITE]
class checker:
def __init__(self,x,y):
self.ox = x
self.oy = y
self.color = (0,0,0)
self.state = 0 # 0 1 2 checks = []
colorful = []
aiful = []
for i in range(1,16):
for j in range(1,16):
achecker = checker(i,j)
checks.append(achecker)
colorful.append(0)
aiful.append(0)
for i in range(1,16):
colorful.append(0)
aiful.append(0)
movements=[]
movements1 = []
movements2 = [] screen = pygame.display.set_mode(screen_size,0, 32)
pygame.display.set_caption('欢乐五子棋')
clock = pygame.time.Clock()
bg = pygame.image.load("image\\checker.jpg").convert()
weixiao = pygame.image.load("image\\weixiao.jpg").convert()
huaji = pygame.image.load("image\\huaji.jpg").convert()
arrow = pygame.image.load("image\\arrow.jpg").convert()
bgcolor = (255,255,255) turn = 0
def draw_check(surf):
screen.fill(bgcolor)
surf.blit(bg, (0, 0))
def draw_column(surf,where):
surf.blit(huaji, (650, 100))
surf.blit(weixiao, (650, 300))
if where == 1:
surf.blit(arrow, (580, 100))
else:
surf.blit(arrow, (580, 300))
def draw_checker(surf):
for c in checks:
if c.state == 1:
pygame.draw.circle(surf, c.color, (c.ox * 35, c.oy * 35), 10, 0)
if c.state == 2:
pygame.draw.circle(surf, c.color, (c.ox * 35, c.oy * 35), 10, 0)
def tmax(a):
max = a[0]
for i in a:
if max < i:
max = i
return max
def Ai_choose():
for i in aiful:
i = 0
if len(movements) == 0:
return (7,7)
for i in movements1:
aiful[i[0]+i[1]*15] = 0
if i[0] > 0:
aiful[i[0]-1+i[1]*15] +=1
if i[0] < 15:
aiful[i[0]+1 + i[1] * 15] += 1
if i[1] > 0:
aiful[i[0]+ (i[1]-1) * 15] += 1
if i[1] < 15:
aiful[i[0] + (i[1] + 1) * 15] += 1
c = tmax(aiful)
print(c) def check_win(x,y):
i = x
j = y
count = 0
ck = colorful[x+y*15]
if ck == 0:
return False
#横着
while i>=0 and colorful[i+y*15] == ck:
i -= 1
count += 1
i = x+1
while i< 15 and colorful[i+y*15] == ck:
i += 1
count += 1
if count >= 5:
print("win")
return True
#竖着
i = x
count = 0
while j>=0 and colorful[i+15*j] == ck:
j-=1
count +=1
j = y+1
while j < 15 and colorful[i+15*j] == ck:
j += 1
count +=1
if count >= 5:
print("win")
return True
#斜着左上
j = y
count = 0
while i >= 0 and j >=0 and colorful[i +15* j] == ck:
j -= 1
i -= 1
count += 1
i = x+1
j = y+1
while i< 15 and j <15 and colorful[i+15*j] == ck:
i += 1
j +=1
count += 1
if count >= 5:
print("win")
return True
#斜着右上
j = y
i = x
count = 0
while i >= 0 and j <15 and colorful[i + 15 * j] == ck:
i -= 1
j += 1
count += 1
i = x + 1
j = y - 1
while i< 15 and j >0 and colorful[i+15*j] == ck:
i += 1
j -=1
count += 1
if count >= 5:
print("win")
return True
return False
runing = 1
Ai_wait = 0
while runing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if turn == 1:
pos = event.pos
grid = (int(round(pos[0] / 35)), int(round(pos[1] / 35)))
for c in checks:
if c.state == 0:
if c.ox == grid[0] and c.oy == grid[1]:
movements.append(grid)
movements2.append(grid)
colorful[(c.ox) + (c.oy) * 15] = 2
c.color = WHITE
c.state = 2
turn = 0
if True == check_win(c.ox,c.oy):
runing = 0
print("bingo")
if turn == 0:
if Ai_wait == 30:
Ai_wait= 0
#pos = Ai_choose()
grid = pos
for i in movements:
while i == pos:
pos = (random.randint(1, 19), random.randint(1, 19))
grid = pos
for c in checks:
if c.state == 0:
if c.ox == grid[0] and c.oy == grid[1]:
movements.append(grid)
movements1.append(grid)
colorful[(c.ox) + (c.oy) * 15] = 1
c.color = BLACK
c.state = 1
turn = 1
if True == check_win(c.ox, c.oy):
runing = 0
print("bingo")
else:
Ai_wait+=1
draw_check(screen)
draw_column(screen,turn)
draw_checker(screen)
pygame.display.update()
clock.tick(50) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
【Pygame】 简易五子棋的更多相关文章
- 简易五子棋 V1.1.0
main.cpp #include "fivechess.cpp" int main() { fivechess a; a.RunGame(); getchar(); return ...
- Web版简易五子棋
前些时候把大三写的C++版五子棋改成Web板挂到了网上,具有一定傻瓜式智能,欢迎体验使用拍砖:http://www.zhentiyuan.com/Games/QuickFiveChess.aspx 现 ...
- c语言进阶4-有返回值函数
一. 从函数返回 从函数返回就是返回语句的第一个主要用途.在程序中,有两种方法可以终止函数的执行,并返回到调用函数的位置.第一种方法是在函数体中,从第一句一直执行到最后一句,当所有语句 ...
- c语言进阶3-有参函数
一. 有参函数的定义 有参函数的定义格式如下: 类型标识符 函数名(形式参数表列) { 语句: } 如 void fun(int a,int b) { printf(“a+b=%d”,a ...
- html+js+node实现五子棋线上对战,五子棋最简易算法
首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...
- [收藏]C++简单五子棋
#include<iostream> #include<iomanip> using namespace std; ; //棋盘行数 ; //棋盘列数 char p[X][Y] ...
- [深度学习]实现一个博弈型的AI,从五子棋开始(2)
嗯,今天接着来搞五子棋,从五子棋开始给小伙伴们聊AI. 昨天晚上我们已经实现了一个五子棋的逻辑部分,其实讲道理,有个规则在,可以开始搞AI了,但是考虑到不够直观,我们还是顺带先把五子棋的UI也先搞出来 ...
- Python:游戏:五子棋之人机对战
本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知 ...
- 贪吃蛇(简易版)Leslie5205912著
# include <stdio.h># include <string.h># include <windows.h># include <stdlib.h ...
随机推荐
- The type org.springframework.jms.JmsException cannot be resolved报错解决
在调用JmsTemplate的send方法时,一直报编译时异常.如下: 异常提示是无法解析org.SpringFrawork.jms.JmsException类型.如下: The type org.s ...
- CLR 中 线程的 ThreadState 解释
ThreadState Aborted 线程已停止 AbortedRequested 线程的 Thread.Abort() 方法已被调用,但线程还未停止. Background 线程在后台执行,与 ...
- 美图吴欣鸿:请不要叫我CEO
关于采访提纲上“对互联网+如何理解?”的问题能否不做回答? 他的说法听上去谦虚而实在,“我一般对于这种大的.有点政策性的问题其实是Hold不住的,我的谈话风格就是比较随性.感性,也很难说去推出一个很强 ...
- [C语言] 数据结构-逻辑结构和物理结构
数据结构:相互之间存在一种或多种特定关系的数据元素的集合 1.数据结构分为逻辑结构和物理结构 集合结构:集合结构中的数据元素除了同属于一个集合外,他们之间没有其他关系 线性结构:线性结构中的数据元素之 ...
- fzu 2154 YesOrNo
Problem 2154 YesOrNo Accept: 14 Submit: 29Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- gulpjs的使用介绍及技巧
gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常简单,学习起来很容易,而且gulpjs使用的是nodejs中stream来读取和操作数据,其速 ...
- thinkphp3.2 create()
* create作用 * 1.将表单元素中的值和数据库字段意义匹配 * 2.将数据库中没有的字段在数组中去除 if(IS_ ...
- HTML5 Boilerplate
time: 2016-10-20 20:00 HTML5 Boilerplate(H5BP)是一个由 Paul Irish(Google Chrome 开发人员.jQuery 项目成员.Moderni ...
- css3画半圆 , 加上一点动画
border-radius制作半圆与制作圆形的方法是一样的,只是元素的宽度与圆角方位要配合一致,不同的宽度和高度比例,以及圆角方位,可以制作上半圆.下半圆.左半圆和右半圆效果.例如: .semicir ...
- maven 依赖和坐标
1.maven 坐标由groupId.artifactId.packaging.version.classifier定义.2.classifier 用来帮助定义构建输出的一些附属构件.如,*javad ...