Python Forum

Full Version: My Pygame Project's Problem **huh**
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I have a problem on my project.Firstly,I am sharing my project code:

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()
I want to make other balls and that balls never spawn with previous balls' side by side.I guess you can understand.Because my english not well :) If you solve this problem,I will happy.Have a nice day.
(Nov-05-2018, 02:07 PM)osmanb06 Wrote: [ -> ]I want to make other balls and that balls never spawn with previous balls' side by side.
First you should fix up your code. A ball class should contain the majority of your code within it. Logic and data for the ball should be in the class. You want to split it out from the main game loop. That will make it a lot easier when you make numerous balls.

Here would be an example of a couple balls and a paddle (because it was already in our tutorials). You can add more balls by just adding another ball object and updating/rendering it. You can make it more presentable by inserting a ball image in its place. The set_ball method sets the velocity of the ball so that each ball feels independently.

import pygame as pg
import random
 
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
done = False
 
class Ball:
    def __init__(self, screen_rect, size):
        self.screen_rect = screen_rect
        self.height, self.width = size
        self.image = pg.Surface(size).convert()
        self.image.fill((255,0,0))
        self.rect = self.image.get_rect()
        self.speed = 5
        self.set_ball()
 
    def get_random_float(self):
        '''get float for velocity of ball on starting direction'''
        while True:
            num = random.uniform(-1.0, 1.0)
            if num > -.5 and num < .5: #restrict ball direction to avoid infinity bounce
                continue
            else:
                return num
                 
    def set_ball(self):
        '''get random starting direction and set ball to center screen'''
        x = self.get_random_float()
        y = self.get_random_float()
        self.vel = [x, y]
        self.rect.center = self.screen_rect.center
        self.true_pos = list(self.rect.center)
         
    def collide_walls(self):
        if self.rect.y < 0 or self.rect.y > self.screen_rect.bottom - self.height:
            self.vel[1] *= -1;
             
        if self.rect.x < 0 or self.rect.x > self.screen_rect.right- self.height:
            self.vel[0] *= -1;
            print('side wall hit, time to reset ball and give points')
             
    def collide_paddle(self, paddle_rect):
        if self.rect.colliderect(paddle_rect):
            self.vel[0] *= -1;
             
    def move(self):
        self.true_pos[0] += self.vel[0] * self.speed
        self.true_pos[1] += self.vel[1] * self.speed
        self.rect.center = self.true_pos
             
    def update(self, paddle_rect):
        self.collide_walls()
        self.collide_paddle(paddle_rect)
        self.move()
 
    def render(self, screen):
        screen.blit(self.image, self.rect)
         
class Paddle:
    def __init__(self, screen_rect, size):
        self.screen_rect = screen_rect
        self.image = pg.Surface(size).convert()
        self.image.fill((255,255,0))
        self.rect = self.image.get_rect()
        self.rect.x += 25 #spacer from wall
        self.speed = 5
         
    def move(self, x, y):
        self.rect[0] += x * self.speed
        self.rect[1] += y * self.speed
         
    def update(self, keys):
        self.rect.clamp_ip(self.screen_rect)
        if keys[pg.K_UP] or keys[pg.K_w]:
            self.move(0, -1)
        if keys[pg.K_DOWN] or keys[pg.K_s]:
            self.move(0, 1)
         
    def render(self, screen):
        screen.blit(self.image, self.rect)
 
paddle = Paddle(screen_rect, (25,100))
ball = Ball(screen_rect, (25,25))
ball2 = Ball(screen_rect, (25,25))
 
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    keys = pg.key.get_pressed()
    screen.fill((0,0,0))
    paddle.update(keys)
    ball.update(paddle.rect)
    ball2.update(paddle.rect)
    paddle.render(screen)
    ball.render(screen)
    ball2.render(screen)
    clock.tick(60)
    pg.display.update()
thank you so much :)