gpt4 book ai didi

python - 当我按下一个键时如何让图片出现?

转载 作者:行者123 更新时间:2023-12-04 08:05:37 24 4
gpt4 key购买 nike

我正在尝试用 Python 制作井字游戏,但我遇到了一个问题。我不知道如何让玩家在棋盘上做出选择。我一直试图在棋盘的左上角方块中得到一个“X”。 “X”和“O”都是图像,而不是文本。背景也是图像。每次我运行我的代码时,窗口打开,板和标题都是他们的,但是当我按“1”键时,没有任何 react 。有谁知道该怎么办?我不确定我的代码的哪些部分是错误的,所以这里是全部:

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
main() 函数内部,Python 不知道 Player_1 ,因为它是函数 redraw_window() 的“本地” - 这意味着只有 redraw_window() 函数知道 Player_1
那么你如何解决这个问题?
最快和最简单的方法是将 Player_1 的定义移动到主程序中,给它一个“全局”作用域。
这修复了错误,但“X”仍然没有出现......为什么?下一个问题是您仅在收到 1 事件时才绘制“X”。然后(几乎立即)再次重新绘制背景,将其覆盖。它只是在屏幕上一瞬间。
解决此问题的一种方法是将电路板的状态与电路板的绘图分开。
让我们引入一个新的 list-of-lists board_state ,它有 3 行,每行 3 个单元格,每个单元格对应一个零和交叉板:
board_state = [ [ None, None, None ], 
[ None, None, None ],
[ None, None, None ] ]
使用它,我们可以设置和检查棋盘上的移动布局,例如: 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
现在我们需要调整窗口绘制函数来查看board-state,并绘制需要的东西:
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_state 的行和列。在循环中使用 (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_state 还允许您检查单元格中是否已经进行了移动。假设玩家按下 5(中心单元格),您只需检查 board_state[1][1] != None 即可测试是否已在该单元格中放置了移动。

关于python - 当我按下一个键时如何让图片出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66234827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com