Python 当我按下一个键时,如何使图片显示?

Python 当我按下一个键时,如何使图片显示?,python,pygame,Python,Pygame,我试图在Python中使用Tic-Tac-Toe,但遇到了一个问题。我不知道如何让球员在棋盘上做出选择。我一直在试着在黑板左上角的方块上画一个“X”。“X”和“O”都是图像,而不是文本。背景也是图像。每次我运行代码时,窗口都会打开,图板和标题都是它们的,但当我按“1”键时,什么也没有发生。有人知道该怎么办吗?我不确定我的代码的哪些部分是错误的,所以这里是全部: import pygame import os import sys from pygame.locals import * pygam

我试图在Python中使用Tic-Tac-Toe,但遇到了一个问题。我不知道如何让球员在棋盘上做出选择。我一直在试着在黑板左上角的方块上画一个“X”。“X”和“O”都是图像,而不是文本。背景也是图像。每次我运行代码时,窗口都会打开,图板和标题都是它们的,但当我按“1”键时,什么也没有发生。有人知道该怎么办吗?我不确定我的代码的哪些部分是错误的,所以这里是全部:

import pygame
import os
import sys
from pygame.locals import *
pygame.init()

WIDTH, HEIGHT = 500, 500
P_WIDTH, P_HEIGHT = 100, 130
WIN = pygame.display.set_mode((WIDTH, HEIGHT))

main_font = pygame.font.SysFont("Comic sans", 40)
#color = (255, 255, 255)
#light_color = (170, 170, 170)
#dark_color = (100, 100, 100)


def redraw_window():
    BG = pygame.transform.scale(pygame.image.load(os.path.join("Board.jpg")).convert_alpha(), (WIDTH, HEIGHT))

    title_label1 = main_font.render("Tic", True, (0, 0, 0))
    title_label2 = main_font.render("Tac", True, (0, 0, 0))
    title_label3 = main_font.render("Toe", True, (0, 0, 0))

    Player_1 = pygame.transform.scale(pygame.image.load(os.path.join("X.jpg")).convert_alpha(), (P_WIDTH, P_HEIGHT))

    WIN.blit(BG, (0, 0))
    WIN.blit(title_label1, (10, 10))
    WIN.blit(title_label2, (10, 40))
    WIN.blit(title_label3, (10, 70))

    pygame.display.update()

def main():

    while True:
        redraw_window()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
            elif event.type == pygame.locals.KEYDOWN:
                if event.key == K_1:
                    WIN.blit(Player_1, (72, 35))


main()

另外,如果有人看到一些可以改进的地方,你能指出吗?

所以你得到了一个错误,最好在你的问题中包含这个细节

pygame 2.0.1 (SDL 2.0.14, Python 3.6.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "./bad_noughts.py", line 47, in <module>
    main()
  File "./bad_noughts.py", line 44, in main
    WIN.blit(Player_1, (72, 35))
NameError: name 'Player_1' is not defined
使用此选项,我们可以设置并检查棋盘上的移动布局,方法如下:
board[0][0]
(左上)和
board[2][2]
(右下)。您刚刚意识到的一件重要事情是,列表是“零索引”的,这意味着第一个元素位于数字0(而不是1)

现在当玩家移动时,我们可以设置棋盘状态:

        elif event.type == pygame.locals.KEYDOWN:
            if event.key == K_1:
                board_state[0][0] = 'x'  # top-left
现在我们需要调整window drawing功能以查看电路板状态,并绘制所需内容:

def redraw_window():
    global board_state

    WIN.blit(BG, (0, 0))
    WIN.blit(title_label1, (10, 10))
    WIN.blit(title_label2, (10, 40))
    WIN.blit(title_label3, (10, 70))

    x_cursor = 0  # position on the board
    y_cursor = 0
    for row in ( 0, 1, 2 ):
        x_cursor = 0
        for col in ( 0, 1, 2 ):
            # if there's a move on the board, draw it
            if ( board_state[row][col] == 'x' ):
                WIN.blit( Player_1, ( x_cursor, y_cursor ) )
            elif ( board_state[row][col] == 'o' ):
                WIN.blit( Player_2, ( x_cursor, y_cursor ) )
            x_cursor += WIDTH//3                             # move the cursor across
        y_cursor += HEIGHT//3                                # move the cursor down

    pygame.display.update()
因此循环在
board\u状态的行和列中迭代。在循环中使用
(0,1,2)
不是最佳实践,但我会尽量保持简单。当循环在棋盘上移动,然后向下移动时,我们也会保持一个光标位置,在该位置可以绘制任何玩家的移动

如果电路板包含“x”或“o”,则绘制正确的位图。除了这个代码有点粗糙,所以坐标可能会有点偏离,但是我没有你的位图,所以这是我们能做的最好的了

参考代码:

import pygame
import os
import sys
from pygame.locals import *
pygame.init()

WIDTH, HEIGHT = 500, 500
P_WIDTH, P_HEIGHT = 100, 130
WIN = pygame.display.set_mode((WIDTH, HEIGHT))

main_font = pygame.font.SysFont("Comic sans", 40)
#color = (255, 255, 255)
#light_color = (170, 170, 170)
#dark_color = (100, 100, 100)

BG = pygame.transform.scale(pygame.image.load(os.path.join("ox_board.png")).convert_alpha(), (WIDTH, HEIGHT))
Player_1 = pygame.transform.scale(pygame.image.load(os.path.join("ox_x.png")).convert_alpha(), (P_WIDTH, P_HEIGHT))
Player_2 = pygame.transform.scale(pygame.image.load(os.path.join("ox_o.png")).convert_alpha(), (P_WIDTH, P_HEIGHT))
title_label1 = main_font.render("Tic", True, (0, 0, 0))
title_label2 = main_font.render("Tac", True, (0, 0, 0))
title_label3 = main_font.render("Toe", True, (0, 0, 0))

# moves (or not) placed on the board
# None -> no move yet
# Or 'x', 'o'
board_state = [ [ None, None, None ],
            [ None, None, None ],
            [ None, None, None ] ]


def redraw_window():
    global board_state

    WIN.blit(BG, (0, 0))
    WIN.blit(title_label1, (10, 10))
    WIN.blit(title_label2, (10, 40))
    WIN.blit(title_label3, (10, 70))

    x_cursor = 0  # position on the board
    y_cursor = 0
    for row in ( 0, 1, 2 ):
    x_cursor = 0
    for col in ( 0, 1, 2 ):
        # if there's a move on the board, draw it
        if ( board_state[row][col] == 'x' ):
            WIN.blit( Player_1, ( x_cursor, y_cursor ) )
        elif ( board_state[row][col] == 'o' ):
            WIN.blit( Player_2, ( x_cursor, y_cursor ) )
        x_cursor += WIDTH//3                             # move the cursor across
    y_cursor += HEIGHT//3                                # move the cursor down

    pygame.display.update()


def main():

    while True:
    redraw_window()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        elif event.type == pygame.locals.KEYDOWN:
            if event.key == K_1:
                board_state[0][0] = 'x'


main()
board_状态
还允许您检查单元格中是否已经进行了移动。假设玩家按下5键(中间单元格),您只需检查
board_state[1][1]!=None
用于测试移动是否已放置在该单元格中

import pygame
import os
import sys
from pygame.locals import *
pygame.init()

WIDTH, HEIGHT = 500, 500
P_WIDTH, P_HEIGHT = 100, 130
WIN = pygame.display.set_mode((WIDTH, HEIGHT))

main_font = pygame.font.SysFont("Comic sans", 40)
#color = (255, 255, 255)
#light_color = (170, 170, 170)
#dark_color = (100, 100, 100)

BG = pygame.transform.scale(pygame.image.load(os.path.join("ox_board.png")).convert_alpha(), (WIDTH, HEIGHT))
Player_1 = pygame.transform.scale(pygame.image.load(os.path.join("ox_x.png")).convert_alpha(), (P_WIDTH, P_HEIGHT))
Player_2 = pygame.transform.scale(pygame.image.load(os.path.join("ox_o.png")).convert_alpha(), (P_WIDTH, P_HEIGHT))
title_label1 = main_font.render("Tic", True, (0, 0, 0))
title_label2 = main_font.render("Tac", True, (0, 0, 0))
title_label3 = main_font.render("Toe", True, (0, 0, 0))

# moves (or not) placed on the board
# None -> no move yet
# Or 'x', 'o'
board_state = [ [ None, None, None ],
            [ None, None, None ],
            [ None, None, None ] ]


def redraw_window():
    global board_state

    WIN.blit(BG, (0, 0))
    WIN.blit(title_label1, (10, 10))
    WIN.blit(title_label2, (10, 40))
    WIN.blit(title_label3, (10, 70))

    x_cursor = 0  # position on the board
    y_cursor = 0
    for row in ( 0, 1, 2 ):
    x_cursor = 0
    for col in ( 0, 1, 2 ):
        # if there's a move on the board, draw it
        if ( board_state[row][col] == 'x' ):
            WIN.blit( Player_1, ( x_cursor, y_cursor ) )
        elif ( board_state[row][col] == 'o' ):
            WIN.blit( Player_2, ( x_cursor, y_cursor ) )
        x_cursor += WIDTH//3                             # move the cursor across
    y_cursor += HEIGHT//3                                # move the cursor down

    pygame.display.update()


def main():

    while True:
    redraw_window()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        elif event.type == pygame.locals.KEYDOWN:
            if event.key == K_1:
                board_state[0][0] = 'x'


main()