Python Forum
My Pygame Project's Problem **huh**
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Pygame Project's Problem **huh**
#1
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.
Reply
#2
(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()
Recommended Tutorials:
Reply
#3
thank you so much :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Pygame display problem video game Paul_Maillet 1 610 Feb-20-2024, 07:50 PM
Last Post: Bronjer
  Problem with pygame.event.clear qq12346 1 2,057 Oct-05-2023, 08:39 AM
Last Post: patriciainman
  pygame double jump problem Yegor123 3 2,505 May-02-2023, 09:34 PM
Last Post: sudoku6
  (HELP GREATLY APPRECIATED) New user- Huge Pygame Installation Problem! Jbomb 1 2,782 Jan-12-2021, 07:32 PM
Last Post: MK_CodingSpace
  problem with pygame Aladdin 3 4,145 Jun-25-2020, 01:41 PM
Last Post: Aladdin
  [PyGame] Beginners to share pygame project? michael1789 4 3,017 Dec-04-2019, 07:17 AM
Last Post: michael1789
  Problem with music - Pygame.error GaseBall 1 3,150 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  [PyGame] Rotation Problem in PyGame thunderbird028 1 2,659 Nov-14-2019, 06:49 AM
Last Post: Windspar
  [PyGame] Encountered a logic problem in my project osmanb06 3 2,717 Nov-16-2018, 05:06 PM
Last Post: osmanb06
  Coding problem in a Pygame Sghet 4 7,960 Aug-13-2018, 05:39 PM
Last Post: MTGReen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020