I am trying to make a pygame clone of the Ballz game where you aim and shoot a bunch of balls to knock out blocks of different strengths. I can aim and shoot balls, but I have one problem: only one ball can move at a time. One shoots, and when it's finished the next one starts. This would get real boring real quick. They are all supposed to shoot in sequence in a stream. But they also must move independently, because when a ball destroys a block the following balls that would have hit it will have a different path.

But first things first, this is my list:

Code:
`[ [12,15,1,3], [5,73,2,2], [23,9,3,1] ]`

Each 4-element list represents a ball. The first two elements in each ball are its x and y coordinates, and the next two are its speed on the x and y axes.

So here is the goal: For now, I'm not worried about them moving in sequence. I want to change the first and second elements (position) of every ball based on the third and fourth elements (its direction). In the above example, the end product (after 1 iteration) would be:

Code:
`[ [13,18,1,3], [7,75,2,2], [26,10,3,1] ]`

OR: If you happen to know how to achieve the behavior of the balls in Ballz a different way, please explain it.
Haha, I'm addicted to this game, and I seriously thought about making this in ICE, but I don't have the time for that . Anyway, my idea was to make 4 lists, one with all the X coordinates, one with the Y coordinates, one with the DeltaX, and one with the DeltaY. In that case (in BASIC), you don't need separate element parsing, but just add the lists, like L1+L3 for the X coordinates, and L2+L4 for the Y coordinates. Pretty simple
Uhh, Python. Right.

You need to make a Ball class (inheriting pygame.sprite.Sprite) and then make a list of Balls. In the Ball class, make an update method to change the position based on the velocity and be sure to clamp based on map bounds and perform collision detection. You would also declare the x, y, vx, vy fields in the __init__ method of the Ball class to define the position and velocity of each ball.
Sorry, I forgot to mention that this is in python. I think I'll try PT_'s way first because I'm still fairly new to OOP (especially in OOP in Python) and if I can't get that to work I'll give oldmud0's way a shot.
You cant simultaneously change all balls at once. You need to use a For loop to update them all every frame. I would do it like this:

1)Define a 'ball' as something that has x, y, vx and vy fields.
2)each time you shoot define your list of balls. Set vx = cos(launchAngle) and vx = cos(launchAngle) (90 degree launch angle should be straight up)
3) Every game loop do:

Code:
``` for ball in balls:     x += vx     y += vy     #check collision here     #render ball ```

4)flip frame buffers

ps: python isnt good for games _._
Hmm. That's basically what I was doing, and the issue is that the balls only went one at a time. Should I put that for loop inside of another loop of some sort to get it to work?

I think I get your idea though.
You want to only do one "timestep" for each ball at a time.

Something like:

Code:
``` while not(gameOver){ #clear screen if ballsAreMoving{ for ball in balls{    ball.x +=  ball.vx      ball.y +=  ball.vy     #check collision here     #render ball } }elif { #get user input for next launch } #flip screen buffer } ```

ps i used {} instead of indentation becouse indentation is to much work.
Ohhhhhhh ok. I see what you mean now. Thanks!
So I got the balls shooting in the desired fashion using something similar to PT_'s idea with 4 lists (x positions, y positions, x speeds, and y speeds) and everything is working (thanks, everyone): The balls launch toward the mouse and shoot one after another, and then proceed to bounce off the sides and top until they all reach the bottom again, at which point another ball is added to the list.

But I don't really know how to do the next step: detecting collisions with blocks AND knowing which block was hit to subtract from its strength/remove it.

I have never tried to do anything like this before, but it seems like it would be fairly simple with a general understanding of the mechanics. Any ideas?

UPDATE: Here is the link to the Github repository for this project.

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum