From cbb160b075e4c50ef3e226ce3c6e28c9ea83ed58 Mon Sep 17 00:00:00 2001 From: LiangLe <952141371@qq.com> Date: Tue, 28 May 2024 21:09:36 +0800 Subject: [PATCH] commit0528 --- 代码/frame_board.py | 22 +++++++ 代码/frame_new.py | 55 ++++++++++++++++ 代码/frame_prep.py | 45 ++++++++++++++ 代码/gui_prep.py | 38 ++++++++++++ 代码/main3.py | 141 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 301 insertions(+) create mode 100644 代码/frame_board.py create mode 100644 代码/frame_new.py create mode 100644 代码/frame_prep.py create mode 100644 代码/gui_prep.py create mode 100644 代码/main3.py diff --git a/代码/frame_board.py b/代码/frame_board.py new file mode 100644 index 0000000..d86e97f --- /dev/null +++ b/代码/frame_board.py @@ -0,0 +1,22 @@ +board = [[] for i in range(15)] + +for i in range(15): + for j in range(15): + board[i].append((i,j)) + + +board_b = [list(l) for l in zip(*board)] + +board_c = [[] for line in range(29)] +for x in range(15): + for y in range(15): + board_c[x-y].append(board[x][y]) +for line in board_c: + print(line) + +board_d = [[] for line in range(29)] +for x in range(15): + for y in range(15): + board_d[x+y].append(board[x][y]) +for line in board_d: + print(line) \ No newline at end of file diff --git a/代码/frame_new.py b/代码/frame_new.py new file mode 100644 index 0000000..059d68f --- /dev/null +++ b/代码/frame_new.py @@ -0,0 +1,55 @@ +def set_chess(x,y,color): + if board[x][y] != ' ': + print('该位置已有棋子') + return False + else: + board[x][y] = color + for i in board: + print(i) + return True + +def check_win(board): + for line in board: + if ''.join(line).find('O'*5) != -1: + print('白棋获胜') + return 0 + elif ''.join(line).find('X'*5) != -1: + print('黑棋获胜') + return 1 + else: + return -1 + +def check_win_all(board): + board_c = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_c[x-y].append(board[x][y]) + board_d = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_d[x+y].append(board[x][y]) + return [check_win(board), check_win([list(l) for l in zip(*board)]), check_win(board_c), check_win(board_d)] + +if __name__ == '__main__': + board = [[' ']*15 for line in range(15)] + for i in board: + print(i) + flag = 0 + going = True + while going: + if flag == 0: + print('黑棋回合') + x = int(input('请输入棋子横坐标0-14:')) + y = int(input('请输入棋子纵坐标0-14:')) + set_chess(x,y,'X') + flag = 1 + if 0 or 1 in check_win_all(board): + going = False + else: + print('白棋回合') + x = int(input('请输入棋子横坐标0-14:')) + y = int(input('请输入棋子纵坐标0-14:')) + set_chess(x,y,'O') + flag = 0 + if 0 or 1 in check_win_all(board): + going = False \ No newline at end of file diff --git a/代码/frame_prep.py b/代码/frame_prep.py new file mode 100644 index 0000000..42dc3ac --- /dev/null +++ b/代码/frame_prep.py @@ -0,0 +1,45 @@ + +list = ['','','','','','','','','','','','','','',''] +list_2 = ['']*15 +list_3 = ['' for i in range(15)] +print(list) +print(list_2) +print(list_3) + + +str = 'XXXXX' +print(str) + + +for i in range(10): + print(i) + + +def foo(x,y): + print('两数之和为{}'.format(x+y)) + +foo(10,11) + + +board = [[' ']*15 for line in range(15)] + + +list_4 = ['X','X','O','O'] +list_str = ''.join(list_4) +print(list_str) + + +line = ['O','O','O','O','O'] +line_2 = ['O','O','O','O','X'] +print('line转化为字符串:',''.join(line)) +print('line_2转化为字符串:',''.join(line_2)) +print('查找line是否有5个O:',''.join(line).find('O'*5)) +print('查找line_2是否有5个O:',''.join(line_2).find('O'*5)) + +def set_chess(x,y,color): + if board[x][y] != ' ': + print('该位置已有棋子') + return False + else: + board[x][y] = color + return True \ No newline at end of file diff --git a/代码/gui_prep.py b/代码/gui_prep.py new file mode 100644 index 0000000..4e79e27 --- /dev/null +++ b/代码/gui_prep.py @@ -0,0 +1,38 @@ +import pygame as pg + + +pg.init() + +WIDTH = 615 +HEIGHT = 615 +screen = pg.display.set_mode((WIDTH, HEIGHT)) #游戏窗口 +clock = pg.time.Clock() +objects = [] # 新创建的元素,全都放到这个列表里 + +background = pg.image.load("data/bg.png").convert_alpha() +black = pg.image.load("data/storn_black.png").convert_alpha() +white = pg.image.load("data/storn_white.png").convert_alpha() + +pg.display.set_caption("五子棋")#标题 + +going = True +while going: + screen.blit(background, (0,0)) + for event in pg.event.get(): + + if event.type == pg.QUIT: + going = False + + elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: + going = False + + elif event.type == pg.MOUSEBUTTONDOWN: + pos = pg.mouse.get_pos() + print(pos) + objects.append([black,(pos[0]-18,pos[1]-18)]) + for o in objects: + screen.blit(o[0], o[1]) + clock.tick(60) + pg.display.update() + +pg.quit() \ No newline at end of file diff --git a/代码/main3.py b/代码/main3.py new file mode 100644 index 0000000..0e56b9a --- /dev/null +++ b/代码/main3.py @@ -0,0 +1,141 @@ + +import pygame as pg +import os + +class GameObject: + def __init__(self, image, pos): + self.image = image + self.pos = image.get_rect(center= pos) + +def load_image(name): + path = os.path.join(main_dir, "data", name) + return pg.image.load(path).convert_alpha() + +def main(objects,board): + pg.init() + clock = pg.time.Clock() + screen = pg.display.set_mode((WIDTH, HEIGHT)) + black = load_image("storn_black.png") + white = load_image("storn_white.png") + background = load_image("bg.png") + + # 窗体的标题 + pg.display.set_caption("五子棋") + font = pg.font.Font('font/12345.TTF', 20) + flag = 1 + going = True + while going: + if flag % 2 == 0: + if pg.font: + text = font.render("白棋回合", True, (255, 255, 255)) + else: + if pg.font: + text = font.render("黑棋回合", True, (0, 0, 0)) + textpos = text.get_rect(centerx=background.get_width() / 2, y=2) + screen.blit(background, (0, 0)) + screen.blit(text, textpos) + for event in pg.event.get(): + if event.type == pg.QUIT: + going = False + elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: + going = False + elif event.type == pg.MOUSEBUTTONDOWN: + pos = pg.mouse.get_pos() + if pos[0] > 587: + x = 14 + elif pos[0] < 27: + x = 0 + else: + x = round((pos[0] - 27)/40) + if pos[1] > 587: + y = 14 + elif pos[1] < 27: + y = 0 + else: + y = round((pos[1] - 27)/40) + if flag % 2 == 0: + if set_chess(board,x,y,'O'): + objects.append(GameObject(white,(27+x*40, 27+y*40))) + flag = 1 + else: + hint_text = font.render("该位置已有棋子", True, (255, 255, 255)) + hint_textpos = hint_text.get_rect(centerx=background.get_width() / 2, y=200) + + screen.blit(hint_text, hint_textpos) + pg.display.update() + + else: + if set_chess(board,x,y,'X'): + objects.append(GameObject(black,(27+x*40, 27+y*40))) + flag = 0 + else: + hint_text = font.render("该位置已有棋子", True, (255, 255, 255)) + hint_textpos = hint_text.get_rect(centerx=background.get_width() / 2, y=200) + for o in objects: + screen.blit(o.image, o.pos) + screen.blit(hint_text, hint_textpos) + pg.display.update() + pg.time.delay(300) + for o in objects: + screen.blit(o.image, o.pos) + if 0 in check_win_all(board): + win_text = font.render("白棋获胜,游戏5秒后重新开始", True, (255, 255, 255)) + win_textpos = win_text.get_rect(centerx=background.get_width() / 2, y=200) + screen.blit(win_text, win_textpos) + pg.display.update() + pg.time.delay(5000) + board = [[' ']*15 for line in range(15)] + objects = [] + flag = 1 + elif 1 in check_win_all(board): + win_text = font.render("黑棋获胜,游戏5秒后重新开始", True, (0, 0, 0)) + win_textpos = win_text.get_rect(centerx=background.get_width() / 2, y=200) + screen.blit(win_text, win_textpos) + pg.display.update() + pg.time.delay(5000) + board = [[' ']*15 for line in range(15)] + objects = [] + flag = 1 + clock.tick(60) + pg.display.update() + +def set_chess(board,x,y,color): + if board[x][y] != ' ': + print('该位置已有棋子') + return False + else: + board[x][y] = color + return True + +def check_win(board): + for list_str in board: + if ''.join(list_str).find('O'*5) != -1: + print('白棋获胜') + return 0 + elif ''.join(list_str).find('X'*5) != -1: + print('黑棋获胜') + return 1 + else: + return -1 + +def check_win_all(board): + board_c = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_c[x+y].append(board[x][y]) + board_d = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_d[x-y].append(board[x][y]) + return [check_win(board) , check_win([list(l) for l in zip(*board)]) , check_win(board_c) , check_win(board_d)] + +if __name__ == "__main__": + main_dir = os.path.split(os.path.abspath(__file__))[0] + WIDTH = 615 + HEIGHT = 615 + SPRITE_WIDTH = 36 + SPRITE_HEIGHT = 36 + board = [[' ']*15 for line in range(15)] + objects = [] + main(objects,board) + pg.quit() \ No newline at end of file