五子棋2pygame代码
Posted 小夜喜欢敲代码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了五子棋2pygame代码相关的知识,希望对你有一定的参考价值。
看看呐,点一点也好啊🤣
#!python3
# -*- coding: utf-8 -*-
import pygame
import game
ROWS = 17
SIDE = 30
SCREEN_WIDTH = ROWS * SIDE
SCREEN_HEIGHT = ROWS * SIDE
EMPTY = -1
BLACK = (0,0,0)
WHITE = (255, 255, 255)
DIRE = [(1, 0), (0, 1), (1, 1), (1, -1)]
class Gobang(game.Game):
def __init__(self, title, size, fps=15):
super(Gobang, self).__init__(title, size, fps)
self.board = [[EMPTY for i in range(ROWS)] for j in range(ROWS)]
self.select = (-1, -1)
self.black = True
self.draw_board()
self.bind_click(1, self.click)
def click(self, x, y):
if self.end:
return
i, j = y // SIDE, x // SIDE
if self.board[i][j] != EMPTY:
return
self.board[i][j] = BLACK if self.black else WHITE
self.draw_chess(self.board[i][j], i, j)
self.black = not self.black
chess = self.check_win()
if chess:
self.end = True
i, j = chess[0]
winer = "Black"
if self.board[i][j] == WHITE:
winer = "White"
pygame.display.set_caption("五子棋 ---- %s win!" % (winer))
for c in chess:
i, j = c
self.draw_chess((100, 255, 255), i, j)
self.timer.tick(5)
def check_win(self):
for i in range(ROWS):
for j in range(ROWS):
win = self.check_chess(i, j)
if win:
return win
return None
def check_chess(self, i, j):
if self.board[i][j] == EMPTY:
return None
color = self.board[i][j]
for dire in DIRE:
x, y = i, j
chess = []
while self.board[x][y] == color:
chess.append((x, y))
x, y = x+dire[0], y+dire[1]
if x < 0 or y < 0 or x >= ROWS or y >= ROWS:
break
if len(chess) >= 5:
return chess
return None
def draw_chess(self, color, i, j):
center = (j*SIDE+SIDE//2, i*SIDE+SIDE//2)
pygame.draw.circle(self.screen, color, center, SIDE//2-2)
pygame.display.update(pygame.Rect(j*SIDE, i*SIDE, SIDE, SIDE))
def draw_board(self):
self.screen.fill((139, 87,66))
for i in range(ROWS):
start = (i*SIDE + SIDE//2, SIDE//2)
end = (i*SIDE + SIDE//2, ROWS*SIDE - SIDE//2)
pygame.draw.line(self.screen, 0x000000, start, end)
start = (SIDE//2, i*SIDE + SIDE//2)
end = (ROWS*SIDE - SIDE//2, i*SIDE + SIDE//2)
pygame.draw.line(self.screen, 0x000000, start, end)
center = ((ROWS//2)*SIDE+SIDE//2, (ROWS//2)*SIDE+SIDE//2)
pygame.draw.circle(self.screen, (0,0,0), center, 4)
pygame.display.update()
if __name__ == '__main__':
print('''
Welcome to 五子棋!
click LEFT MOUSE BUTTON to play game.
''')
gobang = Gobang("五子棋", (SCREEN_WIDTH, SCREEN_HEIGHT))
gobang.run()
上面是源代码,下面就自己看,注意下面的代码要和源代码分开且在同一个文件夹里
#!python3
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from sys import exit
FOUR_NEIGH = {"left": (0, -1), "right": (0, 1), "up": (-1, 0), "down": (1, 0)}
EIGHT_NEIGH = list(FOUR_NEIGH.values()) + [(1, 1), (1, -1), (-1, 1), (-1, -1)]
DIRECTION = {pygame.K_UP: "up", pygame.K_LEFT: "left", pygame.K_RIGHT: "right", pygame.K_DOWN: "down"}
def hex2rgb(color):
b = color % 256
color = color >> 8
g = color % 256
color = color >> 8
r = color % 256
return (r, g, b)
class Game(object):
def __init__(self, title, size, fps=30):
self.size = size
pygame.init()
self.screen = pygame.display.set_mode(size, 0, 32)
pygame.display.set_caption(title)
self.keys = {}
self.keys_up = {}
self.clicks = {}
self.timer = pygame.time.Clock()
self.fps = fps
self.score = 0
self.end = False
self.fullscreen = False
self.last_time = pygame.time.get_ticks()
self.is_pause = False
self.is_draw = True
self.score_font = pygame.font.SysFont("Calibri", 130, True)
def bind_key(self, key, action):
if isinstance(key, list):
for k in key:
self.keys[k] = action
elif isinstance(key, int):
self.keys[key] = action
def bind_key_up(self, key, action):
if isinstance(key, list):
for k in key:
self.keys_up[k] = action
elif isinstance(key, int):
self.keys_up[key] = action
def bind_click(self, button, action):
self.clicks[button] = action
def pause(self, key):
self.is_pause = not self.is_pause
def set_fps(self, fps):
self.fps = fps
def handle_input(self, event):
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key in self.keys.keys():
self.keys[event.key](event.key)
if event.key == pygame.K_F11: # F11全屏
self.fullscreen = not self.fullscreen
if self.fullscreen:
self.screen = pygame.display.set_mode(self.size, pygame.FULLSCREEN, 32)
else:
self.screen = pygame.display.set_mode(self.size, 0, 32)
if event.type == pygame.KEYUP:
if event.key in self.keys_up.keys():
self.keys_up[event.key](event.key)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button in self.clicks.keys():
self.clicks[event.button](*event.pos)
def run(self):
while True:
for event in pygame.event.get():
self.handle_input(event)
self.timer.tick(self.fps)
self.update(pygame.time.get_ticks())
self.draw(pygame.time.get_ticks())
def draw_score(self, color, rect=None):
score = self.score_font.render(str(self.score), True, color)
if rect is None:
r = self.screen.get_rect()
rect = score.get_rect(center=r.center)
self.screen.blit(score, rect)
def is_end(self):
return self.end
def update(self, current_time):
pass
def draw(self, current_time):
pass
class Test(Game):
def __init__(self, title, size, fps=30):
super(Test, self).__init__(title, size, fps)
self.bind_key(pygame.K_RETURN, self.press_enter)
def press_enter(self):
print("press enter")
def draw(self, current_time):
pass
def press_space(key):
print("press space.")
def click(x, y):
print(x, y)
def main():
print(hex2rgb(0xfcf040))
game = Test("game", (640, 480))
game.bind_key(pygame.K_SPACE, press_space)
game.bind_click(1, click)
game.run()
if __name__ == '__main__':
main()
点个赞吧各位
以上是关于五子棋2pygame代码的主要内容,如果未能解决你的问题,请参考以下文章
C/C++学院0907-象棋五子棋代码分析/寻找算法以及排序算法
[教你做小游戏] 用86行代码写一个联机五子棋WebSocket后端