How to make a shooting game in processing




















Tagged: collisions. Best Answer Answer by Chrisir from January January edited January Sorry, it formatted weirdly. You can edit your post click the small gear and the 'Edit'. How to format Code: empty line before and after the code section select highlight entire code with the mouse hit ctrl-o OR click the small C in the command bar.

January Thanks for that help. The next else if statement is used to check if the ball has made a collision with the paddle. Of course there are easier ways to detect collisions in processing by using libraries but this is just to know the basics of how these libraries work. So to check for collisions, all we need to do is to check if the ball is in the area of the paddle.

So if there is collision between the ball and the paddle, the ball should go back up, that is why I am subtracting from dirY. Then the else statement simply adds dirY to ballY at any point that the above statements is false. The next set of if statements are similar to the above except they are for the X axis. The next if statement checks if the ball has touched the left of the screen. If it has, then the ball should go back in the opposite direction which is right. The next else if statement checks if the ball has touched the right of the screen.

If it has, then the ball should go back in the opposite direction which is left. Similarly, the else statement simply adds dirX to ballX at any point that the above statements are false. And now you have successfully created a nice bouncing ball game. Please feel free to ask your questions in the comment box below:. I did my game in a bit different method. But how can I send you the code? Your email address will not be published. Notify me of follow-up comments by email. Notify me of new posts by email.

Skip to content. Related posts:. Here is the code:. We defined the color, width and height of the racket as a global variable, we might want them to change during gameplay. We implemented a method drawRacket which does what its name suggests.

We set the rectMode to center, so our racket is aligned to the center of our cursor. So what watchRacketBounce does is it makes sure that the racket and the ball collides. There are two things to check here, which is if the ball and racket lined up both vertically and horizontally. The first if statement checks if the X coordinate of the right side of the ball is greater than the X coordinate of the left side of the racket and the other way around. If it is, second statement checks if the distance between the ball and the racket is smaller than or equal to the radius of the ball which means they are colliding.

So if these conditions meet, makeBounceBottom method gets called and the ball bounces on our racket at mouseY , where the racket is. Have you noticed the variable overhead which is calculated by mouseY - pmouseY?

As the mouse can move very fast, there is a good chance we might not detect the distance between the ball and the racket correctly in between frames if the mouse is moving towards the ball fast enough. So, we take the difference of the mouse coordinates in between frames and take that into account while detecting distance. The the faster mouse is moving, the greater distance is acceptable.

We also use overhead for another reason. We detect which way the mouse is moving by checking the sign of overhead. If overhead is negative, the mouse was somewhere below in the previous frame so our mouse racket is moving up. In that case, we want to add an extra speed to the ball and move it a little further than regular bounce to simulate the effect of hitting the ball with the racket. If overhead is less than 0 , we add it to ballY and ballSpeedVert to make the ball go higher and faster.

So the faster the racket hits the ball, the higher and faster it will move up. In this section, we will add horizontal movement to the ball. Then, we will make it possible to control the ball horizontally with our racket. Here we go:. The idea here is the same as what we did for vertical movement. We created a horizontal speed variable, ballSpeedHorizon. We created a method to apply horizontal speed to ballX and take away the air friction.

We added two more if statements to the keepInScreen method which will watch the ball for hitting the left and right edges of the screen. Finally we created makeBounceLeft and makeBounceRight methods to handle the bounces from left and right. Now that we added horizontal speed to the game, we want to control the ball with the racket. As in the famous Atari game Breakout and in all other brick breaking games, the ball should go left or right according to the point on the racket it hits.

Code first:. Adding that simple line to the watchRacketBounce did the job. What we did is we determined the distance of the point that the ball hits from the center of the racket with ballX - mouseX.

Then, we make it the horizontal speed. The actual difference was too much, so I gave it a few tries and figured that one-tenth of the value feels the most natural. Our sketch is starting to look more like a game with each step. In this step, we will add walls moving towards the left, just like in Flappy Bird:.

Even though the code looks long and intimidating, I promise there is nothing hard to understand. First thing to notice is ArrayList. It is resizeable, it has useful methods like list. We keep the wall data as integer arrays within the arraylist. The data we keep in the arrays are for the gap between two walls. The arrays contain the following values:. The actual walls are drawn based on the gap wall values.

We have two base methods to manage the walls, wallAdder and wallHandler. We have a global variable lastAddTime which stores the time when the last wall was added in milliseconds. If the current millisecond millis minus the last added millisecond lastAddTime is larger than our interval value wallInterval , it means it is now time to add a new wall.

Random gap variables are then generated based on the global variables defined at the very top. Then a new wall integer array that stores the gap wall data is added into the arraylist and the lastAddTime is set to the current millisecond millis.

And for each item at each loop, it calls wallRemover i , wallMover i and wallDrawer i by the index value of the arraylist. These methods do what their name suggests.

It grabs the wall data array from the arraylist, and calls rect method to draw the walls to where they should actually be. Finally, wallRemover removes the walls from the arraylist which are out of the screen. It would be easier for us to understand your code by running and experimenting with it if it didn't require images; however, it is possible to make some observations as it is: Most people would create a class to hold the bullets; that way, you don't have to manage them from within your player class.

But, then, if you want to make it closer to Portal, you could automatically change the wall that you are aiming at and skip bullets altogether.

Re: Re: Need help adding shooting 1 year ago. I could supply a zip with everything if that would help. I've tried making a class for bullets but im clearly doing something wrong because it doesn't work when i do it. Edit: here is a. Leave a comment on calsign's reply. A zip file would definitely be helpful Can you show us what you tried with the Bullet class? I tried to have an image be drawn at the point of the gun when space was clicked but it wouldn't work.

Leave a comment on thez3r's reply. Leave a comment on Chrisir's reply. But again thank you! Shoot both ways, not only to the right Have a decent wall detection. Count the ammo you are using and when it's 0 don't shoot anymore till he finds an ammo pack. Also maybe have a timer millis to make sure there's a gap between bullets like with a automatic gun. I just wanted to give you a general impression of how to shoot.

I also can't figure out a way to make the bullet change the color of the wall when it hits. Anyone have any suggestions on how to do this?

Regarding direction: See line in my player tab.



0コメント

  • 1000 / 1000