pygame python3.6无法识别鼠标点击

2024-04-20 10:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在我开始之前,我要为这个糟糕的代码提前道歉。我刚刚开始,我正在尝试用pygame创建一个Tic-Tac-Toe游戏,因为我觉得这将是一个很好的实践。我遇到的问题是,我的很多鼠标点击没有被拿起,我不得不点击它的工作,而且在游戏结束时,程序也没有反应很多次。我在网上到处搜索,但没有找到解决办法。 我在windows10上使用python3.6。你知道吗

import pygame
import time
import random

pygame.init()

display_width = 600
display_height = 600

black = (0, 0, 0)
light_grey = (180, 180, 180)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
light_blue = (0, 137, 255)

game_background = pygame.image.load("Tic-tac-toe.png")

gameDisplay = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption('Tic Tac Toe')

clock = pygame.time.Clock()

my_big_font = pygame.font.Font(None, 64)
my_font = pygame.font.Font(None, 32)
my_small_font = pygame.font.Font('Calibri-Light.ttf', 20)

begin_text = my_font.render('Would you like to begin?', 1, black)
yes_text = my_font.render('YES', 1, black)
no_text = my_font.render('NO', 1, black)
instructions_title = my_font.render('INSTRUCTIONS', 1, black)
instructions_start = my_font.render('START', 1, black)
instructions_body1 = my_small_font.render('The object of Tic Tac Toe is to', 1, black)
instructions_body2 = my_small_font.render('get  three  in  a  row.  Players', 1, black)
instructions_body3 = my_small_font.render('alternate  placing  Xs  and  Os', 1, black)
instructions_body4 = my_small_font.render('on   the   game   board   until', 1, black)
instructions_body5 = my_small_font.render('either  opponent   has   three', 1, black)
instructions_body6 = my_small_font.render('in  a  row  or all  nine squares', 1, black)
instructions_body7 = my_small_font.render('are    filled.   In     the   event', 1, black)
instructions_body8 = my_small_font.render('that    no   one    has    three', 1, black)
instructions_body9 = my_small_font.render('in  a  row,  the  game  results', 1, black)
instructions_body0 = my_small_font.render('in  a  tie.', 1, black)

global ready
global instructions

done = False
ready = False
instructions = False
start = True

yes_position = pygame.Rect(175, 275, 100, 50)
no_position = pygame.Rect(325, 275, 100, 50)
start_position = pygame.Rect(202, 452, 198, 48)
position0 = pygame.Rect(25, 25, 150, 150)
position1 = pygame.Rect(226, 25, 150, 150)
position2 = pygame.Rect(420, 25, 150, 150)
position3 = pygame.Rect(25, 230, 150, 150)
position4 = pygame.Rect(226, 230, 150, 150)
position5 = pygame.Rect(420, 230, 150, 150)
position6 = pygame.Rect(25, 430, 150, 150)
position7 = pygame.Rect(226, 430, 150, 150)
position8 = pygame.Rect(420, 425, 150, 150)

print(my_big_font.size('YOU LOSE!!!'))


def quitting():
    if event.type == pygame.QUIT:
        pygame.quit()
        quit()

def draw_start():
    gameDisplay.fill(white)
    pygame.draw.rect(gameDisplay, black, [150, 200, 300, 150], 1)
    pygame.draw.rect(gameDisplay, light_grey, [151, 201, 298, 148])
    gameDisplay.blit(begin_text, (175, 225))
    pygame.draw.rect(gameDisplay, green, [175, 275, 100, 50])
    pygame.draw.rect(gameDisplay, black, [174, 274, 102, 52], 3)
    gameDisplay.blit(yes_text, (203, 289))
    pygame.draw.rect(gameDisplay, red, [325, 275, 100, 50])
    pygame.draw.rect(gameDisplay, black, [324, 274, 102, 52], 3)
    gameDisplay.blit(no_text, (359, 289))

def draw_instructions_body():
    gameDisplay.blit(instructions_body1, (180, 160))
    gameDisplay.blit(instructions_body2, (180, 185))
    gameDisplay.blit(instructions_body3, (180, 210))
    gameDisplay.blit(instructions_body4, (180, 235))
    gameDisplay.blit(instructions_body5, (180, 260))
    gameDisplay.blit(instructions_body6, (180, 285))
    gameDisplay.blit(instructions_body7, (180, 310))
    gameDisplay.blit(instructions_body8, (180, 335))
    gameDisplay.blit(instructions_body9, (180, 360))
    gameDisplay.blit(instructions_body0, (180, 385))

def draw_instructions():
    if instructions:
        gameDisplay.fill(white)
        pygame.draw.rect(gameDisplay, black, [150, 100, 300, 425], 1)
        pygame.draw.rect(gameDisplay, light_grey, [151, 101, 298, 423])
        gameDisplay.blit(instructions_title, (214, 115))
        pygame.draw.rect(gameDisplay, green, [202, 452, 198, 48])
        pygame.draw.rect(gameDisplay, black, [200, 450, 200, 50], 2)
        gameDisplay.blit(instructions_start, (264, 464))
        draw_instructions_body()
        pygame.draw.rect(gameDisplay, black, [165, 150, 270, 265], 1)

def draw_x(x, y):
    pygame.draw.line(gameDisplay, black, [x, y], [(x + 100), (y + 100)], 20)
    pygame.draw.line(gameDisplay, black, [(x + 100), y], [x, (y + 100)], 20)

def draw_o(x, y):
    pygame.draw.circle(gameDisplay, black, [x + 60, y + 60], 60)
    pygame.draw.circle(gameDisplay, white, [x + 60, y + 60], 45)



def start():
    global ready
    global instructions
    if event.type == pygame.MOUSEBUTTONUP:
        if event.button == 1:
            if yes_position.collidepoint(event.pos):
                pygame.time.delay(100)
                instructions = True

            if start_position.collidepoint(event.pos) and instructions:
                instructions = False
                gameDisplay.fill(white)
                gameDisplay.blit(game_background, (0, 0))
                pygame.display.update()
                ready = True

            if no_position.collidepoint(event.pos):
                pygame.time.delay(500)
                pygame.quit()
                quit()



def drawplayerpiece(a, b):
    if player_piece == 'x':
        draw_x(a, b)
    if player_piece == 'o':
        draw_o(a, b)

global game_start
global order
global game_board
game_start = False
order = 0

game_board = [' '] * 9

def draw_new_board():
    global game_board
    if game_board[0] == 'o':
        draw_o(25, 25)
    if game_board[1] == 'o':
        draw_o(226, 25)
    if game_board[2] == 'o':
        draw_o(420, 25)
    if game_board[3] == 'o':
        draw_o(25, 230)
    if game_board[4] == 'o':
        draw_o(226, 230)
    if game_board[5] == 'o':
        draw_o(420, 230)
    if game_board[6] == 'o':
        draw_o(25, 430)
    if game_board[7] == 'o':
        draw_o(226, 430)
    if game_board[8] == 'o':
        draw_o(420, 425)
    if game_board[0] == 'x':
        draw_x(25, 25)
    if game_board[1] == 'x':
        draw_x(226, 25)
    if game_board[2] == 'x':
        draw_x(420, 25)
    if game_board[3] == 'x':
        draw_x(25, 230)
    if game_board[4] == 'x':
        draw_x(226, 230)
    if game_board[5] == 'x':
        draw_x(420, 230)
    if game_board[6] == 'x':
        draw_x(25, 430)
    if game_board[7] == 'x':
        draw_x(226, 430)
    if game_board[8] == 'x':
        draw_x(420, 425)

# 0  1  2
# 3  4  5
# 6  7  8

def draw_win(x, y, a, b, p):
    if p == player_piece:
        pygame.draw.line(gameDisplay, green, [x, y], [a, b], 20)
        pygame.time.delay(200)
        pygame.draw.rect(gameDisplay, green, [150, 200, 300, 150],)
        pygame.draw.rect(gameDisplay, black, [150, 200, 300, 150], 2)
        gameDisplay.blit((my_big_font.render('YOU WIN!!!', 1, black)), (180, 250))
        pygame.time.delay(2000)

    if p == computer_piece:
        pygame.draw.line(gameDisplay, red, [x, y], [a, b], 20)
        pygame.time.delay(200)
        pygame.draw.rect(gameDisplay, red, [150, 200, 300, 150],)
        pygame.draw.rect(gameDisplay, black, [150, 200, 300, 150], 2)
        gameDisplay.blit((my_big_font.render('YOU LOSE!!!', 1, black)), (160, 250))
        pygame.time.delay(2000)

def checkwin(x):
    if (game_board[0] == x and game_board[1] == x and game_board[2] == x):
        draw_win(50, 100, 550, 100, x)
    if (game_board[3] == x and game_board[4] == x and game_board[5] == x):
        draw_win(50, 300, 550, 300, x)
    if (game_board[6] == x and game_board[7] == x and game_board[8] == x):
        draw_win(50, 500, 550, 500, x)
    if (game_board[0] == x and game_board[3] == x and game_board[6] == x):
        draw_win(100, 50, 100, 550, x)
    if (game_board[1] == x and game_board[4] == x and game_board[7] == x):
        draw_win(300, 50, 300, 550, x)
    if (game_board[2] == x and game_board[5] == x and game_board[8] == x):
        draw_win(500, 50, 500, 550, x)
    if (game_board[0] == x and game_board[4] == x and game_board[8] == x):
        draw_win(75, 75, 525, 525, x)
    if (game_board[6] == x and game_board[4] == x and game_board[2] == x):
        draw_win(75, 525, 525, 75, x)

def checkdraw():
    if not checkwin() and ' ' not in game_board:
        return True        


def game_loop():
    global order
    global game_start
    global player_piece
    global computer_piece
    global game_board
    if not game_start:
        game_board = [' '] * 9
        pygame.draw.rect(gameDisplay, light_grey, [200, 250, 200, 100])
        pygame.draw.rect(gameDisplay, black, [200, 250, 200, 100], 1)
        pygame.draw.rect(gameDisplay, light_blue, [235, 300, 45, 35])
        pygame.draw.rect(gameDisplay, black, [235, 300, 45, 35], 1)
        pygame.draw.rect(gameDisplay, light_blue, [320, 300, 45, 35])
        pygame.draw.rect(gameDisplay, black, [320, 300, 45, 35], 1)
        gameDisplay.blit((my_font.render('X', 1, black)), (250, 307))
        gameDisplay.blit((my_font.render('O', 1, black)), (334, 307))
        gameDisplay.blit((my_small_font.render('What do you want', 1, black)), (225, 256))
        gameDisplay.blit((my_small_font.render('to play as?', 1, black)), (255, 275))
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if (pygame.Rect(235, 300, 45, 35)).collidepoint(event.pos):
                        player_piece = 'x'
                        computer_piece = 'o'
                        order = 2
                        game_start = True
                    if (pygame.Rect(320, 300, 45, 35)).collidepoint(event.pos):
                        player_piece = 'o'
                        computer_piece = 'x'
                        order = 1
                        game_start = True
    if game_start:
        if order == 1:
            computer_move()
            checkwin((computer_piece))
            order = 2
        if order == 2:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:
                        if position0.collidepoint(event.pos) and game_board[0] == ' ':
                            game_board[0] = player_piece
                            drawplayerpiece(25, 25)
                            checkwin((player_piece))
                            order = 1
                        if position1.collidepoint(event.pos) and game_board[1] == ' ':
                            drawplayerpiece(226, 25)
                            game_board[1] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position2.collidepoint(event.pos) and game_board[2] == ' ':
                            drawplayerpiece(420, 25)
                            game_board[2] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position3.collidepoint(event.pos) and game_board[3] == ' ':
                            drawplayerpiece(25, 230)
                            game_board[3] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position4.collidepoint(event.pos) and game_board[4] == ' ':
                            drawplayerpiece(226, 230)
                            game_board[4] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position5.collidepoint(event.pos) and game_board[5] == ' ':
                            drawplayerpiece(420, 230)
                            game_board[5] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position6.collidepoint(event.pos) and game_board[6] == ' ':
                            drawplayerpiece(25, 430)
                            game_board[6] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position7.collidepoint(event.pos) and game_board[7] == ' ':
                            drawplayerpiece(226, 430)
                            game_board[7] = player_piece
                            checkwin((player_piece))
                            order = 1
                        if position8.collidepoint(event.pos) and game_board[8] == ' ':
                            drawplayerpiece(420, 425)
                            game_board[8] = player_piece
                            checkwin((player_piece))
                            order = 1                            
        draw_new_board()
        checkwin('x')
        checkwin('o')

global computer_test_location
computer_test_location = 100

def computer_move():
    global computer_test_location
    global game_board
    global computer_piece
    computer_test_location = random.randint(0, 8)
    pygame.time.delay(500)
    while True:
        if game_board[(computer_test_location)] == ' ':
            game_board[(computer_test_location)] = computer_piece
            break
        computer_test_location = random.randint(0, 8)


while True:
    for event in pygame.event.get():
        quitting()
        if not ready:
            start()

    if not ready:
        if start:
            draw_start()
        if instructions:
            draw_instructions()
    if ready:
        gameDisplay.fill(white)
        gameDisplay.blit(game_background, (0, 0))
        game_loop()

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

Tags: andboardeventgamepieceifmypygame
1条回答
网友
1楼 · 发布于 2024-04-20 10:29:00

问题是您有几个连续执行的事件循环。这意味着当您进入game_loop时,第一个for event in pygame.event.get():循环将清空队列,并且您只看到在第一个事件循环和第二个事件循环之间的短时间内添加的事件。你知道吗

一个快速解决方法是将事件分配给外部事件循环中的一个变量(pygame.event.get返回一个列表),在第一个事件循环中使用这个列表,然后将它传递给game_loop函数,在那里您可以对它进行第二次迭代。你知道吗

events = pygame.event.get()
for event in events:
    # ...

if ready:
    game_loop(events)

game_loop函数中:

def game_loop(events):
    # ...
    for event in events:

相关问题 更多 >