So I have been creating this game for a while, where rectangles (written in code as mice) move in random directions around a screen, and every loop I move another rectangle that isn't rendered on the screen (named 'mouse_vision') around each mouse, that tells the mouse if it has collided with another mouse(to tell it not to go there), as well as to tell the mouse to move onto the randomly generated blue blocks (named 'food' in the code), that it has collided. However, the mice occasionally ignore the food, and move to a different, random position even though that should be impossible.
I have spent a couple hours trying to debug it (as well as another bug where the code doesn't register that a mouse has collided with some food), however I have made little progress in solving the issue.
As this is quite a specific question, i have not been able to find anyone else with a similar problem.
This is the code where the game tells the mice to move to the food:
loop5 = 0
while loop5 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop5][0]):
direction = 1
loop5 += 1
given_mouse_vision.x -= 30
given_mouse_vision.x -= 30
loop6 = 0
while loop6 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop6][0]):
direction = 4
loop6 += 1
given_mouse_vision.x += 30
given_mouse_vision.y += 30
loop7 = 0
while loop7 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop7][0]):
direction = 3
loop7 += 1
given_mouse_vision.y -= 30
given_mouse_vision.y -= 30
loop8 = 0
while loop8 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop8][0]):
direction = 2
loop8 += 1
This is the entire code, although the problem could only occur in the 'mouse_animation()' function:
import pygame
import time
import random
import sys
import os
### GENERATION OF FOOD ###
def food_generation():
food_duplicate = False
food_rand_generation_loop = 0
food_rendered = random.randint(1, 12)
if food_rendered == 1:
food_random_x = random.randint(0, 36)
food_random_y = random.randint(0, 20)
food_mouse_x = 30 * food_random_x
food_mouse_y = 30 * food_random_y
food_taken_positions.append([food_mouse_x, food_mouse_y])
food_n = 0
while food_n < len(food_taken_positions) - 1:
if food_taken_positions[food_n][0] == food_taken_positions[-1][0] and food_taken_positions[food_n][1] == \
food_taken_positions[-1][1]:
del food_taken_positions[-1]
food_duplicate = True
food_rand_generation_loop -= 1
food_n += 1
if not food_duplicate:
food = pygame.Rect(food_mouse_x, food_mouse_y, 28, 28)
food_list.append([food, food_mouse_x, food_mouse_y])
food_rand_generation_loop += 1
### MOVEMENT OF MOUSE ###
def mouse_animation(given_mouse, given_mouse_vision):
global mouse_movement_counter, can_move_left, can_move_right, can_move_up, can_move_down, mouse_x, mouse_y
direction = random.randint(1, 5)
del mice[mouse_loop]
mice.append([given_mouse, given_mouse_vision, mouse_x, mouse_y])
given_mouse = mice[-1][0]
given_mouse_vision = mice[-1][1]
loop1 = 0
given_mouse_vision.x += 30
while loop1 < len(mice) - 1:
if given_mouse_vision.colliderect(mice[loop1][0]):
can_move_right = False
loop1 += 1
given_mouse_vision.x -= 30
loop2 = 0
given_mouse_vision.x -= 30
while loop2 < len(mice) - 1:
if given_mouse_vision.colliderect(mice[loop2][0]):
can_move_left = False
loop2 += 1
given_mouse_vision.x += 30
loop3 = 0
given_mouse_vision.y += 30
while loop3 < len(mice) - 1:
if given_mouse_vision.colliderect(mice[loop3][0]):
can_move_down = False
loop3 += 1
given_mouse_vision.y -= 30
loop4 = 0
given_mouse_vision.y -= 30
while loop4 < len(mice) - 1:
if given_mouse_vision.colliderect(mice[loop4][0]):
can_move_up = False
loop4 += 1
given_mouse_vision.y += 30
given_mouse_vision.x += 30
loop5 = 0
while loop5 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop5][0]):
direction = 1
loop5 += 1
given_mouse_vision.x -= 30
given_mouse_vision.x -= 30
loop6 = 0
while loop6 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop6][0]):
direction = 4
loop6 += 1
given_mouse_vision.x += 30
given_mouse_vision.y += 30
loop7 = 0
while loop7 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop7][0]):
direction = 3
loop7 += 1
given_mouse_vision.y -= 30
given_mouse_vision.y -= 30
loop8 = 0
while loop8 < len(food_list) - 1:
if given_mouse_vision.colliderect(food_list[loop8][0]):
direction = 2
loop8 += 1
given_mouse_vision.y += 30
ie = len(food_list) - 1
while ie > 0:
ie -= 1
if given_mouse.colliderect(food_list[ie][0]): # FOOD
del food_list[ie]
break
ie -= 1
if given_mouse.x <= 10:
can_move_left = False
if given_mouse.x >= 1100:
can_move_right = False
if given_mouse.y <= 10:
can_move_up = False
if given_mouse.y >= 600:
can_move_down = False
if direction == 1 and mouse_movement_counter <= 0 and can_move_right: # RIGHT
given_mouse.x += 30
given_mouse_vision.x += 30
mouse_movement_counter += 15
mouse_x += 30
elif direction == 2 and mouse_movement_counter <= 0 and can_move_up: # UP
given_mouse.y -= 30
given_mouse_vision.y -= 30
mouse_movement_counter += 15
mouse_y -= 30
elif direction == 3 and mouse_movement_counter <= 0 and can_move_down: # DOWN
given_mouse.y += 30
given_mouse_vision.y += 30
mouse_movement_counter += 15
mouse_y += 30
elif direction == 4 and mouse_movement_counter <= 0 and can_move_left: # LEFT
given_mouse.x -= 30
given_mouse_vision.x -= 30
mouse_movement_counter += 15
mouse_x -= 30
elif can_move_right == False and can_move_up == False and can_move_down == False and can_move_left == False:
mouse_movement_counter += 15
else:
mouse_movement_counter -= 1
pygame.display.update()
### VARIABLES ###
pygame.init()
clock = pygame.time.Clock()
FPS = 10
screen_width, screen_height = 1140, 630
screen = pygame.display.set_mode((screen_width, screen_height))
food_taken_positions = []
food_list = []
mice = []
taken_positions = []
rand_generation_loop = 0
### GENERATES HOW MANY MICE WILL BE SPAWNED ###
while rand_generation_loop < 100:
duplicate = False
random_x = random.randint(0, 36)
random_y = random.randint(0, 20)
mouse_x = 30 * random_x
mouse_y = 30 * random_y
taken_positions.append([mouse_x, mouse_y])
n = 0
while n < len(taken_positions) - 1:
if taken_positions[n][0] == taken_positions[-1][0] and taken_positions[n][1] == taken_positions[-1][1]:
del taken_positions[-1]
duplicate = True
rand_generation_loop -= 1
n += 1
if duplicate == False:
mouse = pygame.Rect(mouse_x, mouse_y, 30, 30)
mouse_position = pygame.Rect(mouse_x, mouse_y, 30, 30)
mice.append([mouse, mouse_position, mouse_x, mouse_y])
rand_generation_loop += 1
x_location = 0
y_location = 0
mouse_movement_counter = 30
### LOOP THAT EXECUTES THE CODE ###
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((20, 100, 20))
for food_ in range(len(food_list)):
pygame.draw.rect(screen, (20, 40, 255), food_list[food_][0])
for certain_mouse in range(len(mice)):
pygame.draw.rect(screen, (200, 200, 200), mice[certain_mouse][0])
mouse_loop = 0
while mouse_loop < len(mice):
can_move_right = True
can_move_left = True
can_move_up = True
can_move_down = True
given_mouse_vision = pygame.Rect(mouse_x, mouse_y, 30, 30)
mouse_animation(mice[mouse_loop][0], mice[mouse_loop][1])
mouse_loop += 1
food_generation()
pygame.display.flip()
I have attempted to cut down the code, but apologies if it is still too long.