[英]Game stops reacting after 15th click when 15 balls generated
This game will generate a ball in a random location on the grid after each click.此游戏将在每次点击后在网格上的随机位置生成一个球。
But it stops reacting on further clicks after reaching 15 balls (see screenshot above).但它在达到 15 个球后停止对进一步点击做出反应(见上面的截图)。
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()
Since the program does not react on clicks anymore I interrupted it in console with CTRL + C.由于程序不再对点击做出反应,我在控制台中使用 CTRL + C 中断了它。 After interrupting execution the console shows following:
中断执行后,控制台显示如下:
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
How can I fix this?我怎样才能解决这个问题?
The problem is the condition:问题是条件:
if not (ball_grid_x in balls_x and ball_grid_y in balls_y):
This condition checks whether there is a ball in line ball_grid_x
and whether there is a ball in column ball_grid_y
.此条件检查
ball_grid_x
行中是否有球以及ball_grid_y
列中是否有球。 However, these can be different balls.然而,这些可以是不同的球。 Therefore, if there is a ball in every row and a ball in every column, this condition creates an infinite loop:
因此,如果每一行都有一个球,每一列都有一个球,那么这个条件会创建一个无限循环:
You need to test if there is a ball at positionv (ball_grid_x, ball_grid_y)
:您需要测试 positionv
(ball_grid_x, ball_grid_y)
是否有球:
if not (ball_grid_x, ball_grid_y) in zip(balls_x, balls_y):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.