颜色不打印 - 连接 4
Posted
技术标签:
【中文标题】颜色不打印 - 连接 4【英文标题】:Colours not printing - connect 4 【发布时间】:2021-06-30 16:32:25 【问题描述】:我正在尝试使用 GUI 编写一个 connect 4 游戏,但我无法在我单击的位置打印颜色,我让它打印空间 = 0 的颜色,我也让它在我单击的位置工作1 和 2 被印在棋盘的文本版本上,但在图形上,我试图让颜色与输入的玩家数量相对应。 任何帮助表示感谢。
import numpy as np
import pygame
import sys
import math
BLUE = (0,0,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
YELLOW = (255,255,0)
ROW_COUNT = 6
COLUMN_COUNT = 7
def create_board(): # board creation
board = np.zeros((ROW_COUNT,COLUMN_COUNT))
return board
def drop_piece(board,row,column, piece): #the dropping of player counters
board[row][column] = piece
def is_valid_location(board, column): # checks if the location entered is valid to be used
return board[ROW_COUNT - 1][column] == 0 # if not true column is full
def get_next_open_row(board,column): # checks which row the piece will fall on
for r in range(ROW_COUNT):
if board[r][column] == 0:
return r
def print_board(board): # flips the board so numbers stack from bottom to top
print(np.flip(board, 0))
def winning_move(board, piece):
# check horizontal locations for win
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
return True
# check for vertical locations for win
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT - 3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
return True
#check for positively sloped diagonals
for c in range(COLUMN_COUNT - 3):
for r in range(ROW_COUNT - 3):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
return True
#check for negatively sloped diagonals
for c in range(COLUMN_COUNT - 3):
for r in range(3,ROW_COUNT):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
return True
def draw_board(board):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, BLACK, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if board[r][c] == 1:
pygame.draw.circle(screen, GREEN, (int(c * SQUARESIZE + SQUARESIZE / 2), HEIGHT - int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)), RADIUS)
elif board[r][c] == 2:
pygame.draw.circle(screen, YELLOW,(int(c * SQUARESIZE + SQUARESIZE / 2), HEIGHT - int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)),RADIUS)
pygame.display.update()
board = create_board()
print_board(board)
game_over = False
turn = 0
# https://www.pygame.org/docs/ <----- pygame commands
pygame.init()
SQUARESIZE = 100
WIDTH = COLUMN_COUNT * SQUARESIZE
HEIGHT = (ROW_COUNT+1) * SQUARESIZE
size = (WIDTH,HEIGHT)
RADIUS = int(SQUARESIZE/2 - 5 )
screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.update()
while not game_over:
#asks for player 1 input
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# print(event.pos)
try:
if turn == 0:
posx = event.pos[0]
column = int(math.floor(posx/SQUARESIZE))
print(column)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 1)
if winning_move(board,1):
print("PLAYER 1 WINS")
game_over = True
#asks for player 2 input
else:
posx = event.pos[0]
column = int(math.floor(posx / SQUARESIZE))
print(column)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 2)
if winning_move(board, 2):
print("PLAYER 2 WINS")
game_over = True
break
print_board(board)
turn = turn + 1
turn = turn % 2 #alternates between the two players
except:
if turn == 0:
column = int(input("Player 1 Make your selection (0-6):"))
print(column)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 1)
if winning_move(board,1):
print("PLAYER 1 WINS")
game_over = True
# asks for player 2 input
else:
column = int(input("Player 2 Make your selection (0-6):"))
print(column)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 2)
if winning_move(board, 2):
print("PLAYER 2 WINS")
game_over = True
break
print_board(board)
draw_board(board)
turn = turn + 1
turn = turn % 2 # alternates between the two players
【问题讨论】:
【参考方案1】:单元格的顶部是HEIGHT - r * SQUARESIZE - SQUARESIZE
,单元格的中心是HEIGHT - r * SQUARESIZE - SQUARESIZE // 2
:
def draw_board(board):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c * SQUARESIZE, HEIGHT - r * SQUARESIZE - SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, BLACK, (c* SQUARESIZE + SQUARESIZE // 2, HEIGHT - r * SQUARESIZE - SQUARESIZE // 2), RADIUS)
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if board[r][c] == 1:
pygame.draw.circle(screen, GREEN, (c * SQUARESIZE + SQUARESIZE // 2, HEIGHT - r * SQUARESIZE - SQUARESIZE // 2), RADIUS)
elif board[r][c] == 2:
pygame.draw.circle(screen, YELLOW,(c * SQUARESIZE + SQUARESIZE // 2, HEIGHT - r * SQUARESIZE - SQUARESIZE // 2),RADIUS)
我建议在应用循环中连续绘制网格:
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
while not game_over:
clock.tick(60)
screen.fill(0)
draw_board(board)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# [...]
典型的 PyGame 应用程序循环必须:
通过pygame.event.pump()
或pygame.event.get()
处理事件。
根据输入事件和时间(分别为帧)更新游戏状态和对象位置
清空整个显示或绘制背景
绘制整个场景(blit
所有对象)
通过pygame.display.update()
或pygame.display.flip()
更新显示
限制每秒帧数以限制 CPU 使用率
使用pygame.time.Clock
控制每秒帧数,从而控制游戏速度。
pygame.time.Clock
对象的方法tick()
以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。见pygame.time.Clock.tick()
:
这个方法应该每帧调用一次。
这意味着循环:
clock = pygame.time.Clock() run = True while run: clock.tick(60)
每秒运行 60 次。
见pygame.time.Clock.tick()
:
这个方法应该每帧调用一次。它将计算自上次调用以来经过了多少毫秒。
【讨论】:
以上是关于颜色不打印 - 连接 4的主要内容,如果未能解决你的问题,请参考以下文章