Делаю игру и понадобилось создавать объекты с разными случайными координатами, и иногда при компиляции объекты генерируются друг на друге, что плохо. Прошу помощи, спасибо
Python:
import pygame
from random import randint
import random
pygame.init()
sc = pygame.display.set_mode((600,400))
clock = pygame.time.Clock()
font = pygame.font.SysFont('arial', 32)
block1 = pygame.Rect(0,0,600,40)
block2 = pygame.Rect(0,360,600,40)
x = 300
y = 320
nup = 1
count = 0
control = 5
Ymove1 = False
Ymove2 = False
apples = []
apple_count = 4
enemyes1 = []
enemy_count1 = 4
it1 = 0
it2 = 0
def move():
global x,nup
x += 3 * nup
if pers.right >= 600:
nup = -1
elif pers.left <= 0:
nup = 1
run = True
while run:
sc.fill((255,255,255))
pers = pygame.Rect(x,y,40,40)
events = pygame.event.get()
for i in events:
if i.type == pygame.QUIT:
run = False
pygame.quit()
if i.type == pygame.KEYDOWN:
if (i.key == pygame.K_SPACE) and (pers.top <= 40 or pers.bottom >= 360):
if y <= 40:
Ymove1 = True
elif y >= 320:
Ymove2 = True
if i.type == pygame.KEYUP:
if i.key == pygame.K_SPACE:
Ymove = False
it1 = 0
while len(apples) != apple_count:
deap1 = randint(40,160)
deap2 = randint(40,73)
xcor1 = random.sample(range(30,570, deap1), apple_count)
ycor1 = random.sample(range(60,340, deap2), apple_count)
apple = pygame.Rect(xcor1[it1], ycor1[it1], 20, 20)
apples.append(apple)
it1 += 1
it2 = 0
while len(enemyes1) != enemy_count1:
deap3 = randint(40,113)
deap4 = randint(40,73)
xcor2 = random.sample(range(700,1100, deap3), enemy_count1)
ycor2 = random.sample(range(60,340, deap4), enemy_count1)
enemy1 = pygame.Rect(xcor2[it2], ycor2[it2], 20, 20)
enemyes1.append(enemy1)
it2 += 1
if count == 15:
enemy_count1 = 3
for i in apples:
pygame.draw.rect(sc, (255,0,0), i)
if pers.colliderect(i):
apples.remove(i)
count += 1
if apple_count < 4:
if count == control:
control += 5
apple_count += 1
for i in enemyes1:
pygame.draw.rect(sc, (0,255,0), i)
i.left -= 5
if i.right <= 0:
enemyes1.remove(i)
if pers.colliderect(i):
run = False
pygame.quit()
move()
if Ymove1:
y += 4
if y >= 320:
Ymove1 = False
if Ymove2:
y -= 4
if y <= 40:
Ymove2 = False
pygame.draw.rect(sc, (0,0,0), block1)
pygame.draw.rect(sc, (0,0,0), block2)
pygame.draw.rect(sc, (0,0,255), pers)
text = font.render('Счет ' + str(count), 0, (255,255,255))
sc.blit(text,(20,1))
stage = count // 5 + 1
if stage > 5:
stage = 5
text1 = font.render('Стадия ' + str(stage), 0, (255,255,255))
sc.blit(text1,(430,1))
pygame.display.flip()
clock.tick(60)