我需要帮助来解决为什么我的按钮没有被正确检测到
I need help troubelshotting why my buttons arent being detected properly
我的游戏是一款简单的老虎机游戏,它基于 numpy 网格系统,左键单击旋转,r 重置棋盘。我正在尝试实现一个点系统以使其更令人兴奋,但我无法获取代码来检测我是否正确单击了按钮。
代码中似乎出现故障的部分是 posCheckRight 和 posCheckLeft 函数(靠近代码底部,第 160 - 184 行)。它们有时可以正常工作,但通常情况下无法正常工作,要重现我的问题,请尝试点击按钮附近的按钮并检查输入
import pygame as pg
import sys
import numpy as np
import random as rand
pg.init()
width = 800
height = 800
lineWidth = 15
winLineWidth = 15
boardRows = 4
boardCols = 4
squareSize = 200
space = 55
windowName = "Slots"
windowNameInt = 0
cost = 50
costtxt = "Cost: {}".format(cost)
fontClr = (255,99,71)
player = 1
game_over = False
posCheckLvlR = 0
posCheckLvlL = 0
bgColor = (200, 200, 0)
lineColor = (0, 0, 180)
triangleColor = (255, 0, 0)
winLineColor = (220, 220, 220)
circleColor = (239, 231, 200)
crossColor = (66, 66, 66)
screen = pg.display.set_mode((width, height))
pg.display.set_caption(windowName)
screen.fill(bgColor)
board = np.zeros((boardRows, boardCols))
def drawLines() :
#Line 1 vert
pg.draw.line(screen, lineColor, (0, squareSize), (width, squareSize), lineWidth)
#Line 2 vert
pg.draw.line(screen, lineColor, (0, 2 * squareSize), (width, 2 * squareSize), lineWidth)
#Line 3 vert
pg.draw.line(screen, lineColor, (0, 3 * squareSize), (width, 3 * squareSize), lineWidth)
#Line 1 hori
pg.draw.line(screen, lineColor, (squareSize, 0), (squareSize, height), lineWidth)
#Line 2 hori
pg.draw.line(screen, lineColor, (2 * squareSize, 0), (2 * squareSize, height), lineWidth)
#Line 3 hori
pg.draw.line(screen, lineColor, (3 * squareSize, 0), (3 * squareSize, height - 60), lineWidth)
def drawPointSyst() :
#(rightest point)(top point)(bottom point)
pg.draw.polygon(screen, (triangleColor), ((790, 760), (760, 730), (760, 790)))
#(leftest point)(top point)(bottom point)
pg.draw.polygon(screen, (triangleColor), ((720, 760), (750, 730), (750, 790)))
#temp square
pg.draw.rect(screen, (triangleColor), (720, 730, 30, 60))
pg.draw.rect(screen, (triangleColor), (760, 730, 30, 60))
myFont = pg.font.SysFont(None, 50)
textSurface = myFont.render(costtxt, True, (fontClr))
#(x,y)
screen.blit(textSurface, (560, 750))
snake1 = pg.image.load("snake.png")
snake2 = pg.image.load("blackSnake.png")
def drawShapes():
for row in range(boardRows):
for col in range(boardCols):
if board[row][col] == 1:
screen.blit(snake2, (int( col * squareSize + squareSize//2 - 32), int( row * squareSize + squareSize//2 - 32)))
elif board[row][col] == 2:
screen.blit(snake1, (int( col * squareSize + squareSize//2 - 32), int( row * squareSize + squareSize//2 - 32)))
def markSquare(row, col):
shape = rand.randint(1,2)
board[row][col] = shape
def freeSquare(row, col):
return board[row][col] == 0
def boardCheck():
for row in range(boardRows):
for col in range(boardCols):
if board[row][col] == 0:
return False
return True
def checkWin(player):
global windowNameInt
#All vertical
for col in range(boardCols):
if board[0][col] == player and board[1][col] == player and board[2][col] == player and board[3][col] == player:
vertWinLine(col)
windowNameInt += 1
#All horizontal
for row in range(boardRows):
if board[row][0] == player and board[row][1] == player and board[row][2] == player and board[row][3] == player:
horiWinLine(row)
windowNameInt += 1
#From bottom right to top left
if board[3][0] == player and board[2][1] == player and board[1][2] == player and board[0][3] == player:
drawAscDiagonal()
windowNameInt += 1
#From top left to bottom right
if board[0][0] == player and board[1][1] == player and board[2][2] == player and board[3][3] == player:
drawDescDiagonal()
windowNameInt += 1
def vertWinLine(col):
posX = col * squareSize + squareSize//2
color = winLineColor
pg.draw.line(screen, color, (posX, 15), (posX, height - 15), lineWidth)
# print("verti win")
def horiWinLine(row):
posY = row * squareSize + squareSize//2
color = winLineColor
pg.draw.line(screen, color, (15, posY), (width - 15, posY), winLineWidth)
# print("hori win")
def drawAscDiagonal():
color = winLineColor
pg.draw.line(screen, color, (15, height - 15), (width - 15, 15), winLineWidth)
# print("asc win")
def drawDescDiagonal():
color = winLineColor
pg.draw.line(screen, color, (15, 15), (width - 15, height - 15), winLineWidth)
# print("diag win")
def restart():
screen.fill(bgColor)
drawLines()
drawPointSyst()
windowName = (str(windowNameInt))
pg.display.set_caption(windowName)
for row in range(boardRows):
for col in range(boardCols):
board[row][col] = 0
drawLines()
drawPointSyst()
def posCheckLeft(pos) :
global posCheckLvlL
for x in pos :
if posCheckLvlL % 2 == 0 :
if x > 720 and x < 750 :
posCheckLvlL += 1
pass
elif posCheckLvlL % 2 == 1 :
if x > 730 and x < 790 :
posCheckLvlL += 1
return True
return False
def posCheckRight(pos) :
global posCheckLvlR
for x in pos :
if posCheckLvlR % 2 == 0 :
if x > 760 and x < 790 :
posCheckLvlR += 1
pass
elif posCheckLvlR % 2 == 1 :
if x > 730 and x < 790 :
posCheckLvlR += 1
return True
return False
def game() :
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
pos = pg.mouse.get_pos()
print(pos)
if posCheckLeft(pos) :
print("left")
if posCheckRight(pos) :
print("right")
while not boardCheck() :
randMouseX = rand.randint(0, width - 1)
randMouseY = rand.randint(0, height - 1)
clickedRow = int(randMouseY // squareSize)
clickedCol = int(randMouseX // squareSize)
# print("Click ", pos, "Grid coordinates: ", clickedRow, clickedCol)
if freeSquare(clickedRow, clickedCol) :
markSquare(clickedRow, clickedCol)
drawShapes()
checkWin(1)
checkWin(2)
if event.type == pg.KEYDOWN:
if event.key == pg.K_r:
restart()
pg.display.update()
while True: game()
如果你需要检查点击的(x,y)位置,那么你只需要检查x和y。我相信这可以满足您的需求:
def posCheckLeft(pos) :
x,y = pos
return 720 < x < 750 and 730 < y < 790
def posCheckRight(pos) :
x,y = pos
return 760 < x < 790 and 730 < y < 790
我的游戏是一款简单的老虎机游戏,它基于 numpy 网格系统,左键单击旋转,r 重置棋盘。我正在尝试实现一个点系统以使其更令人兴奋,但我无法获取代码来检测我是否正确单击了按钮。
代码中似乎出现故障的部分是 posCheckRight 和 posCheckLeft 函数(靠近代码底部,第 160 - 184 行)。它们有时可以正常工作,但通常情况下无法正常工作,要重现我的问题,请尝试点击按钮附近的按钮并检查输入
import pygame as pg
import sys
import numpy as np
import random as rand
pg.init()
width = 800
height = 800
lineWidth = 15
winLineWidth = 15
boardRows = 4
boardCols = 4
squareSize = 200
space = 55
windowName = "Slots"
windowNameInt = 0
cost = 50
costtxt = "Cost: {}".format(cost)
fontClr = (255,99,71)
player = 1
game_over = False
posCheckLvlR = 0
posCheckLvlL = 0
bgColor = (200, 200, 0)
lineColor = (0, 0, 180)
triangleColor = (255, 0, 0)
winLineColor = (220, 220, 220)
circleColor = (239, 231, 200)
crossColor = (66, 66, 66)
screen = pg.display.set_mode((width, height))
pg.display.set_caption(windowName)
screen.fill(bgColor)
board = np.zeros((boardRows, boardCols))
def drawLines() :
#Line 1 vert
pg.draw.line(screen, lineColor, (0, squareSize), (width, squareSize), lineWidth)
#Line 2 vert
pg.draw.line(screen, lineColor, (0, 2 * squareSize), (width, 2 * squareSize), lineWidth)
#Line 3 vert
pg.draw.line(screen, lineColor, (0, 3 * squareSize), (width, 3 * squareSize), lineWidth)
#Line 1 hori
pg.draw.line(screen, lineColor, (squareSize, 0), (squareSize, height), lineWidth)
#Line 2 hori
pg.draw.line(screen, lineColor, (2 * squareSize, 0), (2 * squareSize, height), lineWidth)
#Line 3 hori
pg.draw.line(screen, lineColor, (3 * squareSize, 0), (3 * squareSize, height - 60), lineWidth)
def drawPointSyst() :
#(rightest point)(top point)(bottom point)
pg.draw.polygon(screen, (triangleColor), ((790, 760), (760, 730), (760, 790)))
#(leftest point)(top point)(bottom point)
pg.draw.polygon(screen, (triangleColor), ((720, 760), (750, 730), (750, 790)))
#temp square
pg.draw.rect(screen, (triangleColor), (720, 730, 30, 60))
pg.draw.rect(screen, (triangleColor), (760, 730, 30, 60))
myFont = pg.font.SysFont(None, 50)
textSurface = myFont.render(costtxt, True, (fontClr))
#(x,y)
screen.blit(textSurface, (560, 750))
snake1 = pg.image.load("snake.png")
snake2 = pg.image.load("blackSnake.png")
def drawShapes():
for row in range(boardRows):
for col in range(boardCols):
if board[row][col] == 1:
screen.blit(snake2, (int( col * squareSize + squareSize//2 - 32), int( row * squareSize + squareSize//2 - 32)))
elif board[row][col] == 2:
screen.blit(snake1, (int( col * squareSize + squareSize//2 - 32), int( row * squareSize + squareSize//2 - 32)))
def markSquare(row, col):
shape = rand.randint(1,2)
board[row][col] = shape
def freeSquare(row, col):
return board[row][col] == 0
def boardCheck():
for row in range(boardRows):
for col in range(boardCols):
if board[row][col] == 0:
return False
return True
def checkWin(player):
global windowNameInt
#All vertical
for col in range(boardCols):
if board[0][col] == player and board[1][col] == player and board[2][col] == player and board[3][col] == player:
vertWinLine(col)
windowNameInt += 1
#All horizontal
for row in range(boardRows):
if board[row][0] == player and board[row][1] == player and board[row][2] == player and board[row][3] == player:
horiWinLine(row)
windowNameInt += 1
#From bottom right to top left
if board[3][0] == player and board[2][1] == player and board[1][2] == player and board[0][3] == player:
drawAscDiagonal()
windowNameInt += 1
#From top left to bottom right
if board[0][0] == player and board[1][1] == player and board[2][2] == player and board[3][3] == player:
drawDescDiagonal()
windowNameInt += 1
def vertWinLine(col):
posX = col * squareSize + squareSize//2
color = winLineColor
pg.draw.line(screen, color, (posX, 15), (posX, height - 15), lineWidth)
# print("verti win")
def horiWinLine(row):
posY = row * squareSize + squareSize//2
color = winLineColor
pg.draw.line(screen, color, (15, posY), (width - 15, posY), winLineWidth)
# print("hori win")
def drawAscDiagonal():
color = winLineColor
pg.draw.line(screen, color, (15, height - 15), (width - 15, 15), winLineWidth)
# print("asc win")
def drawDescDiagonal():
color = winLineColor
pg.draw.line(screen, color, (15, 15), (width - 15, height - 15), winLineWidth)
# print("diag win")
def restart():
screen.fill(bgColor)
drawLines()
drawPointSyst()
windowName = (str(windowNameInt))
pg.display.set_caption(windowName)
for row in range(boardRows):
for col in range(boardCols):
board[row][col] = 0
drawLines()
drawPointSyst()
def posCheckLeft(pos) :
global posCheckLvlL
for x in pos :
if posCheckLvlL % 2 == 0 :
if x > 720 and x < 750 :
posCheckLvlL += 1
pass
elif posCheckLvlL % 2 == 1 :
if x > 730 and x < 790 :
posCheckLvlL += 1
return True
return False
def posCheckRight(pos) :
global posCheckLvlR
for x in pos :
if posCheckLvlR % 2 == 0 :
if x > 760 and x < 790 :
posCheckLvlR += 1
pass
elif posCheckLvlR % 2 == 1 :
if x > 730 and x < 790 :
posCheckLvlR += 1
return True
return False
def game() :
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
pos = pg.mouse.get_pos()
print(pos)
if posCheckLeft(pos) :
print("left")
if posCheckRight(pos) :
print("right")
while not boardCheck() :
randMouseX = rand.randint(0, width - 1)
randMouseY = rand.randint(0, height - 1)
clickedRow = int(randMouseY // squareSize)
clickedCol = int(randMouseX // squareSize)
# print("Click ", pos, "Grid coordinates: ", clickedRow, clickedCol)
if freeSquare(clickedRow, clickedCol) :
markSquare(clickedRow, clickedCol)
drawShapes()
checkWin(1)
checkWin(2)
if event.type == pg.KEYDOWN:
if event.key == pg.K_r:
restart()
pg.display.update()
while True: game()
如果你需要检查点击的(x,y)位置,那么你只需要检查x和y。我相信这可以满足您的需求:
def posCheckLeft(pos) :
x,y = pos
return 720 < x < 750 and 730 < y < 790
def posCheckRight(pos) :
x,y = pos
return 760 < x < 790 and 730 < y < 790