[英]Shutting down pygame without using the mouse
我做了一個基本的井字游戲,我有 2 個特工隨機玩。 我希望游戲運行 10000 次。 我現在可以這樣做,但只有在我按下 pygame window 上的關閉按鈕后,實例才會重置。 相反,我希望 window 不斷關閉並再次打開,或者讓 window 通過重置游戲板等保持打開狀態 - 兩者都很好。 這是代碼:
import pygame
import numpy as np
import random
import time
import sys
def get_position(mouseX, mouseY):
if mouseX <= width/3:
col = 0
elif mouseX <= 2*width/3:
col = 1
else:
col = 2
if mouseY <= height/3:
row = 0
elif mouseY <= 2*height/3:
row = 1
else:
row = 2
return (row, col)
def show_board(input, board):
input.blit(board, (0,0))
pygame.display.flip()
def initialise_board(input):
background = pygame.Surface(input.get_size())
background = background.convert()
background.fill((250, 250, 250))
pygame.draw.line(background, (0, 0, 0), (width / 3, 0), (width / 3, height), 2)
pygame.draw.line(background, (0, 0, 0), (2 * width / 3, 0), (2 * width / 3, height), 2)
pygame.draw.line(background, (0, 0, 0), (0, height / 3), (width, height / 3), 2)
pygame.draw.line(background, (0, 0, 0), (0, 2 * height / 3), (width, 2 * height / 3), 2)
input.blit(background, (0, 0))
pygame.display.flip()
return background
def check_board(row, col, array_board):
if array_board[row][col] != 0:
return False
else:
return True
#def get_mouse():
# (mouseX, mouseY) = pygame.mouse.get_pos()
# (row, col) = get_position(mouseX, mouseY)
# return (row, col)
def check_win(array_board):
global winner
for row in range(3):
if array_board[row][0] == array_board[row][1] == array_board[row][2] != 0:
winner = array_board[row][0]
pygame.draw.line(board, (0, 0, 0), (75, (row*round(height/3) + 150)), (825, (row*round(height/3) + 150)), 3)
break
for col in range(3):
if array_board[0][col] == array_board[1][col] == array_board[2][col] != 0:
winner = array_board[col][0]
pygame.draw.line(board, (0, 0, 0), (col*round(width/3) + 150, 75), (col*round(width/3)+ 150, 825), 3)
break
if array_board[0][0] == array_board[1][1] == array_board[2][2] != 0:
winner = array_board[0][0]
pygame.draw.line(board, (0, 0, 0,), (75, 75), (825, 825), 3)
if array_board[0][2] == array_board[1][1] == array_board[2][0] != 0:
winner = array_board[0][2]
pygame.draw.line(board, (0,0,0), (825, 75), (75, 825), 3)
return winner
def click_board(array_board):
global team
(row, col) = get_position(random.randint(1,900), random.randint(1,900))
centerX = (col * round(width/3)) + 150
centerY = (row * round(height/3)) + 150
if team == 2 and check_board(row, col, array_board):
pygame.draw.circle(board, (0, 0, 0), np.round((centerX, centerY)), round(width/12), 2)
team = 1
array_board[row][col] = 2
elif team == 1 and check_board(row, col, array_board):
pygame.draw.line(board, (0,0,0), (centerX - width/15, centerY - height/15), (centerX + width/15, centerY + height/15), 2)
pygame.draw.line(board, (0,0,0), (centerX + width/15, centerY - height/15), (centerX - width/15, centerY + height/15), 2)
team = 2
array_board[row][col] = 1
def game(array_board, board):
global winner, xwins, owins
winner = check_win(array_board)
if winner == 0 and team == 1:
message = "X's turn"
elif winner == 0 and team == 2:
message = "O's turn"
elif winner != 0 and team == 2:
message = "X wins!"
xwins += 1
elif winner != 0 and team == 1:
message = "O wins!"
owins += 1
font = pygame.font.Font(None, 24)
text = font.render(message, 1, (10,10,10))
board.fill((250,250,250), (width/2 - 20, 50, width/2 + 20, 50))
board.blit(text, (width/2,50))
xwins = 0
owins = 0
for i in range(10):
pygame.init()
width = 900
height = 900
input = pygame.display.set_mode((width,height))
pygame.display.set_caption('Tic-Tac-Toe')
array_board = np.zeros([3,3])
winner = 0
team = 1 #team 1 is X, team 2 is O
board = initialise_board(input)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#elif event.type == pygame.MOUSEBUTTONDOWN and winner == 0:
if winner == 0:
print(winner)
game(array_board, board)
show_board(input, board)
check_win(array_board)
click_board(array_board)
time.sleep(0.1)
pygame.display.quit()
pygame.quit()
有兩個條件結束游戲:
winner != 0
)np.array(np.where(array_board == 0)).size == 0
) 您所要做的就是在您的應用程序中實現else案例並重置所有游戲狀態並重新繪制空的游樂場。
此外,您可以計算游戲數並在 10000 場游戲后break
循環。 for
循環( for i in range(10):
)不再需要。
no_of_games = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
end_of_game = winner != 0 or np.array(np.where(array_board == 0)).size == 0
if not end_of_game:
print(winner)
game(array_board, board)
show_board(input, board)
check_win(array_board)
click_board(array_board)
time.sleep(0.1)
else:
array_board = np.zeros([3,3])
team = 1
winner = 0
time.sleep(1)
board = initialise_board(input)
no_of_games += 1
if no_of_games >= 10000:
break
pygame.display.quit()
pygame.quit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.