Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不使用鼠标关闭pygame_Python_Pygame - Fatal编程技术网

Python 不使用鼠标关闭pygame

Python 不使用鼠标关闭pygame,python,pygame,Python,Pygame,我已经做了一个基本的井字游戏,我有两个代理人随机玩。我想让游戏运行10000次。我现在可以这样做,但只有在我按下pygame窗口上的close按钮后,实例才会重置。取而代之的是,我想让窗口不断地关闭和再次打开,或者让窗口一直保持打开状态,并重置游戏板等等——这两种情况都很好。代码如下: import pygame import numpy as np import random import time import sys def get_position(mouseX, mouseY):

我已经做了一个基本的井字游戏,我有两个代理人随机玩。我想让游戏运行10000次。我现在可以这样做,但只有在我按下pygame窗口上的close按钮后,实例才会重置。取而代之的是,我想让窗口不断地关闭和再次打开,或者让窗口一直保持打开状态,并重置游戏板等等——这两种情况都很好。代码如下:

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()
导入pygame
将numpy作为np导入
随机输入
导入时间
导入系统
def get_位置(鼠标、鼠标):

如果mouseX有两种情况结束游戏:

  • 已设置赢家(
    winner!=0
  • 或者操场上的所有场地都被占用了
    np.array(np.where(array\u board==0)).size==0
您所要做的就是在应用程序中实现else用例,重置所有游戏状态并重新绘制空的游乐场。
此外,您还可以计算游戏数,并在10000场游戏后打破循环。
循环(
用于范围(10)中的i):
)不再需要

没有游戏=0
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
游戏结束=赢家!=0或np.array(np.where(array_board==0)).size==0
如果不是游戏的结束:
印刷品(优胜者)
游戏(阵列,棋盘,棋盘)
显示板(输入,板)
检查赢(阵列板)
单击电路板(阵列电路板)
睡眠时间(0.1)
其他:
阵列板=np.0([3,3])
团队=1
获胜者=0
时间。睡眠(1)
电路板=初始化电路板(输入)
无游戏数+=1
如果没有游戏数量>=10000:
打破
pygame.display.quit()
pygame.quit()

是否有理由需要将其作为GUI/PyGame应用程序编写?如果只是两个代理相互玩,他们不需要GUI,它可以通过管道进行位交换。我最初只是想做这个游戏,然后决定做随机代理。