简体   繁体   English

当产生 15 个球时,第 15 次点击后游戏停止反应

[英]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.此游戏将在每次点击后在网格上的随机位置生成一个球。

用 15 个球点击 15 次后的网格

Issue问题

But it stops reacting on further clicks after reaching 15 balls (see screenshot above).但它在达到 15 个球后停止对进一步点击做出反应(见上面的截图)。

Pygame Code 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()

Stacktrace after interrupting中断后的堆栈跟踪

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.

相关问题 pandas 每月重新抽样第 15 天 - pandas monthly resample 15th day 需要计算从下个月15号到15号的操作总和 - Need to count the total sum of operations from 15th day to 15th day of next month 确定在第15天之后会转到下个月的当前月份变量-python 2.7 - Determine current month variable that rolls over to next month after the 15th day- python 2.7 Select 每月的第一天/第十五天加上 python 的前后一天 - Select first/15th day of month plus the day before and after in python 需要计算 pi 到 30 位,但是在第 15 位之后它们都失败了 - Needing to calculate pi to 30 digits, but after the 15th digit they all fail 计算python中每月的第1个或第15个月的时间 - Calculating time until 1st or 15th of the month in python Discord.py 每月15号发一个embed - Discord.py Sending an embed on the 15th every month Django ORM过滤上个月15日到本月15日之间的记录 - Django ORM filter records between last month 15th date to current month 15th date 如何线性插补从下个月15日开始的每月15号的每月数据 - how to linearly interpolate monthly data from the 15th of each month that rolls over the 15th of the next month 使用正则表达式匹配长 integer 中的第 10 位到第 15 位数字并替换为 * - Using regex to match 10th to 15th digits in a long integer and replace with *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM