Merhabalar,Pygame’de basit bir projem var.Proje basitçe şöyle:Oyun açıldığında bir top oluşuyor,Space tuşuna basıldığında yeni toplar oluşuyor.Sorunumu projemin kodunun altına yazacağım:
import pygame
import random
import math
pygame.init()
WIDTH = 450
HEIGHT = 450
COLOR = (255,255,255)
BALL_SIZE = 30
FINISHED = True
class Ball:
def __init__(self):
self.x = 0
self.y = 0
self.x_change = 0
self.y_change = 0
def make_ball():
ball = Ball()
ball.x = random.randint((BALL_SIZE),WIDTH-(BALL_SIZE))
ball.y = random.randint((BALL_SIZE),HEIGHT-(BALL_SIZE))
ball.x_change = 0
ball.y_change = 0
return ball
def main():
a = 1
CLOCK = pygame.time.Clock()
surface = pygame.display.set_mode((WIDTH,HEIGHT))
lst = []
lst_2 = []
make_bouncing_ball = make_ball()
lst.append(make_bouncing_ball)
while FINISHED:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
return 1
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_SPACE:
spawn = make_ball()
lst.append(spawn)
a = a + 1
surface.fill(COLOR)
lst_2 = lst[:-1]
while (lst[-1].x - BALL_SIZE) < 0 or (lst[-1].x + BALL_SIZE)>WIDTH:
lst[-1].x = random.randint((BALL_SIZE),WIDTH-(BALL_SIZE))
while (lst[-1].y - BALL_SIZE) < 0 or (lst[-1].y + BALL_SIZE)>HEIGHT:
lst[-1].y = random.randint((BALL_SIZE),HEIGHT-(BALL_SIZE))
for bouncing_list in lst:
for bouncing_list_2 in lst_2:
if abs(bouncing_list.x - bouncing_list_2.x)<30:
print(bouncing_list.x,bouncing_list_2.x)
pygame.draw.circle(surface,(255,0,0),(bouncing_list.x,bouncing_list.y),BALL_SIZE,BALL_SIZE)
CLOCK.tick(30)
pygame.display.flip()
if __name__ == "__main__":
main()
Sorunum şu:Oluşan yeni topların birbirlerine değmemesini,iç içe geçmemesini istiyorum.Fakat bunu yapamadım.Yardımcı olursanız çok sevinirim.İyi günler.