|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qwe1231069 于 2018-4-2 14:21 编辑
第一次更新
基本上能玩了,判断什么的都写好了(写的很烂,以后再改)
输赢会在终端上显示出来
文件已改
===================================================================
近来想写个象棋,发现有点难。于是决定先写个五子棋
我还没写完,花了大概两晚写了个界面。发出来让新手看看
肯定还有好多地方做的不好,希望大家帮我指出来
希望一个月之内能写个简单的带AI的五子棋
一共三个文件,图片是50像素的棋子
settings
class Settings():
def __init__(self):
#屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
#棋盘设置
#行数
self.row_num = 10
#列数
self.columns_num = 10
self.row_width = 50
self.qipan_color = (200,0,0)
chess.pyimport pygame
from settings import Settings
class Chess():
def __init__(self,screen):
#flag 0 为黑棋
self.flag = 0
self.screen = screen
self.set = Settings()
#加载棋子图片
self.white = pygame.image.load('baiqi1.png')
self.black = pygame.image.load('heiqi1.png')
self.white_rect = self.white.get_rect()
self.black_rect = self.black.get_rect()
#棋盘坐标列表
self.chesslist = [[0 for i in range(self.set.columns_num + 1)]for i in range(self.set.row_num + 1)]
def chessboard(self,screen):
self.zerox = self.set.screen_width/2 - self.set.columns_num/2*self.set.row_width
self.zeroy = self.set.screen_height/2 - self.set.row_num/2*self.set.row_width
#画棋盘
for i in range(0,self.set.row_num + 1):
a = i * self.set.row_width
pygame.draw.line(screen,self.set.qipan_color,(self.zerox,self.zeroy + a),(self.zerox + self.set.row_width * self.set.columns_num,self.zeroy + a),5)
for i in range(0,self.set.columns_num + 1):
a = i * self.set.row_width
pygame.draw.line(screen,self.set.qipan_color,(self.zerox + a,self.zeroy),(self.zerox + a,self.zeroy + self.set.row_num * self.set.row_width),5)
def blitme(self,flag):
pass
def save_chess(self):
if self.newx < 0 or self.newy < 0 or self.newy > self.set.row_num or self.newx > self.set.columns_num or self.chesslist[self.newy][self.newx] != 0:
return 0
if self.flag:
#bai qi -1
self.chesslist[self.newy][self.newx] = -1
else:
self.chesslist[self.newy][self.newx] = 1
#print(self.chesslist)
def coordinate(self):
#棋子坐标由鼠标决定
x,y = pygame.mouse.get_pos()
#鼠标点击地方的棋子坐标
self.newx = int((x + 25 - self.zerox)//50)
self.newy = int((y + 25 - self.zeroy)//50)
#print(self.newx,self.newy)
if self.newx < 0 or self.newy < 0 or self.newy > self.set.row_num or self.newx > self.set.columns_num or self.chesslist[self.newy][self.newx] != 0:
return 0
self.white_rect.centerx = self.newx * 50 + self.zerox
self.white_rect.bottom = self.newy * 50 + self.zeroy + 25
self.black_rect.centerx = self.newx * 50 + self.zerox
self.black_rect.bottom = self.newy * 50 + self.zeroy + 25
if self.flag:
#flag 1 为白色
self.screen.blit(self.white,self.white_rect)
else:
self.screen.blit(self.black,self.black_rect)
self.flag = ~self.flag
return 1
def win(self):
#x,y是坐标 i,j是移动方向 num是此方向个数
def a(x,y,i,j,num):
while(x >= 0 and y >= 0 and x <= self.set.columns_num and y <= self.set.row_num):
a = self.chesslist[y][x]
y = y + j
x = x + i
if x < 0 or y < 0 or x > self.set.columns_num or y > self.set.row_num:
break
b = self.chesslist[y][x]
if a == b:
num = num + 1
else:
break
return num
#白棋赢了-1 没人获胜 0
winflag = 0
i = self.chesslist[self.newy][self.newx]
x = self.newx
y = self.newy
numi = a(x,y,0,-1,0) + a(x,y,0,1,0) + 1
nums = a(x,y,-1,0,0) + a(x,y,1,0,0) + 1
num1 = a(x,y,-1,-1,0) + a(x,y,1,1,0) + 1
num2 = a(x,y,-1,1,0) + a(x,y,1,-1,0) + 1
if numi > 4 or nums > 4 or num1 > 4 or num2 > 4:
winflag = i
return winflag
return 0
wuziqi.py#五子棋
import pygame
import sys
from pygame.locals import *
from settings import Settings
from chess import Chess
def run_game():
global flag
#创建屏幕对象
pygame.init()
qiset = Settings()
screen = pygame.display.set_mode((qiset.screen_width,qiset.screen_height))
pygame.display.set_caption("五子棋")
chess = Chess(screen)
a = 0
#bg_color
screen.fill(qiset.bg_color)
#绘制棋盘
chess.chessboard(screen)
#main()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for i in range(len(pressed_array)):
if pressed_array[i]:
if i == 0:
#left button
#chess.blitme(flag)
a=chess.coordinate()
if a:
chess.save_chess()
b = chess.win()
if b == 1:
print('白棋赢了')
elif b == -1:
print('黑棋赢了')
elif i == 1:
#mouse wheel
pass
elif i == 2:
pass
#right button
#让最近绘制的屏幕可见
pygame.display.flip()
run_game()
|
|