这个游戏将产生一个球在一个随机的位置上的网格后,每次点击。

问题
但在达到15个球后,它不再对进一步的点击做出反应(见上面的截图)。
Pygame码
import pygame
from random import randint
pygame.init()
gameDisplay = pygame.display.set_mode((915,915))
colors = [(102,51,0),(0,255,255),(0,255,0),(0,51,204),(102,0,204),(255,255,0),(255,0,0)]
slots = []
x = 0
y = 0
click_pos = (0,0)
play = True
#Size of squares
size = 83
white = (255,255,255)
gray = (200,200,200)
deep_gray = (20,20,20)
green = (0,255,0)
black = (0,0,0)
boardLength = 9
z = 0
clicked = False
selecting = False
ball_spawn = False
balls_x = []
balls_y = []
ball_amount = 0
ball_color = []
ball_drew = 0
get_ball_pos = False
click_once = 0
#bg color
gameDisplay.fill(deep_gray)
# GRID
while boardLength > z:
#border
pygame.draw.rect(gameDisplay, black, [size-5,size-5,boardLength*size+5 ,boardLength*size+5])
#grid squares
for i in range(1,boardLength+1):
for z in range(1,boardLength+1):
pygame.draw.rect(gameDisplay, gray, [size*z,size*i,size-5,size-5])
# GAME
while play == True:
for events in pygame.event.get():
if events.type == pygame.MOUSEBUTTONUP:
click_pos = list(pygame.mouse.get_pos())
clicked = True
if events.type == pygame.QUIT:
play = False
#get click pos on grid
if clicked == True:
click_once += 1
if click_once == 1:
x = round((click_pos[0]-size/2) / size)
y = round((click_pos[1]-size/2) / size)
if x > 0 and x < 10 and y > 0 and y < 10:
grid_x = x*size
grid_y = y*size
clicked = False
selecting = True
get_ball_pos = True
ball_spawn = True
else:
click_once = 0
#selector
if selecting:
pygame.draw.rect(gameDisplay, green, (grid_x,grid_y,size-5,size-5), 5)
# BALLS
while ball_spawn:
while get_ball_pos:
ball_grid_x = randint(1,9)
ball_grid_y = randint(1,9)
if not (ball_grid_x in balls_x and ball_grid_y in balls_y):
get_ball_pos = False
balls_x.append(ball_grid_x)
balls_y.append(ball_grid_y)
ball_color.append(colors[randint(0,6)])
ball_amount += 1
while ball_drew < ball_amount:
pygame.draw.circle(gameDisplay, ball_color[ball_drew], (balls_x[ball_drew]*size + size*0.5 - 2, balls_y[ball_drew]*size + size*0.5 - 2), 25)
pygame.draw.circle(gameDisplay, black, (balls_x[ball_drew]*size + size*0.5 - 2, balls_y[ball_drew]*size + size*0.5 - 2), 25, 5)
ball_drew += 1
ball_drew = 0
ball_spawn = False
#final result
pygame.display.update()
pygame.quit()
quit()中断后的堆栈跟踪
由于程序不再对单击作出反应,所以我在控制台中用CTRL + C中断了它。在中断执行之后,控制台显示如下:
pygame 2.0.1 (SDL 2.0.14, Python 3.6.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
^CTraceback (most recent call last):
File "SO_pygame.py", line 73, in <module>
ball_grid_x = randint(1,9)
File "/usr/lib/python3.6/random.py", line 221, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.6/random.py", line 192, in randrange
istop = _int(stop)
KeyboardInterrupt我怎么才能解决这个问题?
发布于 2021-12-11 09:05:19
问题在于条件:
如果不是
(ball_grid_x in balls_x,ball_grid_y in balls_y):
此条件检查行ball_grid_x中是否有一个球,ball_grid_y列中是否有一个球。然而,这些可能是不同的球。因此,如果每一行中都有一个球,而每个列中都有一个球,则此条件将创建一个无限循环:
您需要测试在(ball_grid_x, ball_grid_y)位置是否有一个球。
if not (ball_grid_x, ball_grid_y) in zip(balls_x, balls_y):https://stackoverflow.com/questions/70312733
复制相似问题