【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 ...
随机推荐
- [转]File uploads in ASP.NET Core
本文转自:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads By Steve Smith ASP.NET MVC ...
- val();html();.text()区别
对于innerHTML 属性,几乎所有的元素都有innerHTML属性,它是一个字符串,用来设置或获取位于对象起始和结束标签内的HTML.(获取HTML当前标签的起始和结束里面的内容) 对于inner ...
- mysql与mysqli的一些区别和方法
一.mysql与mysqli的概念相关: 1.mysql与mysqli都是php方面的函数集,与mysql数据库关联不大. 2.在php5版本之前,一般是用php的mysql函数去驱动mysql数据库 ...
- iOS字体打印
//打印所以字体 NSArray *familyNames = [UIFont familyNames]; for(NSString *familyName in familyNames) ...
- Hive 基础你需要掌握这些
HDFS 中一个简单的 Join查询,是否需要撸一大串代码?我只会SQL语句 能不能入坑大数据?这里我们就来聊一聊 Hive. Hive 是什么? Hive 是一种数据仓库工具,不提供数据存储(数据还 ...
- 中小型研发团队架构实践九:任务调度Job
一.Job 简介 Job 类似于数据库中的作业,多用于实现定时执行任务.适用场景主要包括定时轮询数据库同步.定时处理数据.定时邮件通知等. 我们的 Job 分为操作系统级别定时任务 WinJob 和 ...
- 适配器(GOF23)
---恢复内容开始--- 摘要:由于应用环境的变化,需要将现存的对象放到新的环境中去,但新环境的接口是现存对象不满足的. 意图:将原本接口不兼容的类通过转换,使得它们能够一起工作,复用现有的类 ada ...
- CSS3 @font-face实现颜色大小可控的三角效果——张鑫旭
一.我之前介绍过的三角实现效果回顾 这里所说的三角效果之等腰直角三角形效果(等边三角形有现成字符实现,没什么好说的:还有图片实现三角众人皆知,不予以说明): 1. 字符实现三角效果关于字符实现三角我早 ...
- 用pymysql操作数据库
import pymysql # 打开数据库连接 connection = pymysql.connect(host='127.0.0.1', user='root', passwd=', db='s ...
- Vue.js 的一些小技巧
给 props 属性设置多个类型 这个技巧在开发组件的时候用的较多,为了更大的容错性考虑,同时代码也更加人性化: export default { props: { width: { type: [S ...