|
3 months ago | |
---|---|---|
README.md | 3 months ago | |
五子棋.py | 3 months ago |
README.md
1a1
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption('我的游戏')
space = 15
cell_size = 30
cell_num = 15
chess_arr = []
def get_one_dire_num(lx, ly, dx, dy, m):
tx = lx
ty = ly
s = 0
while True:
tx += dx
ty += dy
if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
s+=1
def check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)]
for x, y, c in chess_arr:
if c == flag:
m[y][x] = 1
lx = chess_arr[-1][0]
ly = chess_arr[-1][1]
dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
[(-1, 1), (1, -1)]]
for dire1, dire2 in dire_arr:
dx, dy = dire1
num1 = get_one_dire_num(lx, ly, dx, dy, m)
dx, dy = dire2
num2 = get_one_dire_num(lx, ly, dx, dy, m)
if num1 + num2 + 1 >= 5: return True
return False
print(list(range(cell_size, cell_size * cell_num, cell_size*3)))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((255,255,0))
for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (205, 205, 205), (x + space, 0 + space),
(x + space, cell_size * (cell_num - 1) + space), 1)
for y in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (205, 205, 205), (0 + space, y + space),
(cell_size * (cell_num - 1) + space, y + space), 1)
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1)
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1)
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1)
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1)
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1)
pygame.display.update()
chess_arr=[]
while True:
if event.type==pygame.MOUSEBUTTONUP:
x,y=pygame.mouse.gat_pos()
chess_arr.append((x,y))
for x,y in chess_arr:
pygame.draw.circle(screen,(205,205,205), [x*cell_size+space, y*cell_size+space], 16,16)
xi = int(round((x - space) * 1.0 / cell_size))
yi = int(round((y - space) * 1.0 / cell_size))
if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num:
chess_arr.append((xi,yi))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi,yi) not in chess_arr and (xi,yi) not in chess_arr:
chess_arr.append((xi, yi))
flag=1
game_state = 1
if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi,1) not in chess_arr and (xi, yi,2) not in chess_arr:
chess_arr.append((xi, yi,flag))
if flag == 1:
flag=2
else:
flag=1
for x, y,c in chess_arr:
chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
if (4, 4,1) not in chess_arr and (4, 4,2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1)
if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1)
if (10, 4, 1) not in chess_arr and (10, 4, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1)
if (4, 10, 1) not in chess_arr and (4 ,10, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1)
if (7, 7, 1) not in chess_arr and (7, 7, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1)
if game_state != 1:
myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
win_text = "%s win" % ('black' if game_state == 2 else 'white')
textImage = myfont.render(win_text, True, white)
screen.blit(textImage, (260, 320))
pygame.display.update()