颜色不打印 - 连接 4
Colours not printing - connect 4
我正在尝试使用 GUI 编写一个 connect 4 游戏,但是我无法在我点击的地方打印颜色,我已经打印了 space = 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
单元格的顶部是 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()
or pygame.event.get()
. 处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过
pygame.display.update()
or pygame.display.flip()
更新显示
- 限制每秒帧数以限制 CPU 使用
使用pygame.time.Clock
控制每秒帧数,从而控制游戏速度。
方法tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick()
:
This method should be called once per frame.
这意味着循环:
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
每秒运行 60 次。
This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.
我正在尝试使用 GUI 编写一个 connect 4 游戏,但是我无法在我点击的地方打印颜色,我已经打印了 space = 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
单元格的顶部是 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()
orpygame.event.get()
. 处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
orpygame.display.flip()
更新显示
- 限制每秒帧数以限制 CPU 使用
使用pygame.time.Clock
控制每秒帧数,从而控制游戏速度。
方法tick()
of a pygame.time.Clock
object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick()
:
This method should be called once per frame.
这意味着循环:
clock = pygame.time.Clock() run = True while run: clock.tick(60)
每秒运行 60 次。
This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.