Loading...
Logo
Processing Forum

Game Project

in Programming Questions  •  1 year ago  
Hello, all! I'm very new to processing so bare with me. I'm trying to create a game where the player moves a rectangle around with their mouse and dodges triangles that move randomly across the screen. Of course i'm having trouble with generating the triangles at random and animating them properly. Any help you can give me would be most appreciated! Thanks a lot!

Replies(8)

Re: Game Project

1 year ago
What do you have so far? (That means show your code.)

Re: Game Project

1 year ago
woops I forgot that. Here it is

float x; 
float y;
float targetX, targetY;
float easing =.5;
void setup ()
{noCursor();
  size (600,600);
  smooth();
  }
void draw()
{
  background(51);
  
  targetX=mouseX;
  float dx=targetX -x;
  if (abs(dx) > 1) {
    x+=dx * easing;
  }
  targetY=mouseY;
  float dy=targetY -y;
  if(abs(dy) > 1) {
    y += dy * easing;
  }
  
  rect(x,y, 33,33);
}

Re: Game Project

1 year ago
Okay- what you want with triangles flying around seems a bit complicated...

There are two ways to approach this:

The preferred, advanced way, using an array of classes (see OOP); which is likely beyond what you will be able to do right now, or:

The alternative way, using several Arrays to store information; for example, and array of x positions, an array of y positions, and an array of rotations; these are essentially the same, except the latter does not broach the OOP boundary.

Either way, you'll probably have to do some reading...

Then, you'll need to find a way to reference that information and display it; referencing the array and drawing triangles.
Then you have to implement collision detection...

I suggest that you get started with creating the data and loading it...

Re: Game Project

1 year ago
If it is not absolutely necessary, maybe instead of triangles you could use circles for a first time making something like this. Circles are very easy to do collisions with, you just use the Pythagorean theorem. For example, pretend your object you are controlling is a circle instead of a square and you define it as:
Copy code
  1. float myX = 50;
  2. float myY = 25;

Also pretend for a moment you only have one thing to avoid (which is also a circle). Define it like this:
Copy code
  1. float badThingX = 50;
  2. float badThingY = 50;

Let's also assume that they both have a radius of 10. To determine if they crashed into one another you would do this:
Copy code
  1. float distanceToEachOther = sqrt(sq(myX-badThingX)+sq(myY-badThingY));
  2. if (distanceToEachOther <= 20) {
  3.   println("Crash");
  4. }
  5. else {
  6.   println("No Collision");
  7. }
Here would be the whole code:
Copy code
  1. float myX = 50;
  2. float myY = 25;
  3. float badThingX = 50;
  4. float badThingY = 50;

  5. void setup() {
  6.   size(100, 100);
  7.   smooth();
  8. }

  9. void draw() {
  10.   float distanceToEachOther = sqrt(sq(myX-badThingX)+sq(myY-badThingY));
  11.   if (distanceToEachOther <= 20) {
  12.     println("Crash");
  13.   }
  14.   else {
  15.     println("No Collision");
  16.   }
  17.   ellipse(myX, myY, 20, 20);
  18.   ellipse(badThingX, badThingY, 20, 20);
  19. }
The next step would be looking into arrays as calsign mentioned. If you use arrays then you would have many bad things and you would test the distance of each one from your controlled object. If any of them have a distance from your controlled object that is less than or equal to 20 then there is a collision (I'm assuming Game Over).

Collision tests for rectangles are not that difficult unless you are rotating the rectangle, if you do then things become a bit tricky. Triangles are a complete pain in the butt to do collision tests for, I would not recommend them for a first time game like this.

Re: Game Project

1 year ago
thank you both for your help. I think i'm understanding the concept but one error keeps popping up. The code looks like this the error is.. java.lang.NullPointerException. Thanks for all your help!

Copy code
  1. float x;
  2. float y;
  3. float myX, myY;
  4. float easing =.1;
  5. void setup ()
  6. {noCursor();
  7.  size (600,600);
  8.  smooth();
  9.  }
  10. void draw()
  11. {
  12.  background(51);

  13.  myX=mouseX;
  14.  float dx=myX -x;
  15.  if (abs(dx) > 1) {
  16.    x+=dx * easing;
  17.  }
  18.  myY=mouseY;
  19.  float dy=myY -y;
  20.  if(abs(dy) > 1) {
  21.    y += dy * easing;
  22.  }

  23.  ellipse(x,y, 33,33);
  24. }

  25. float badThingX=50;
  26. float badThingY=50;

  27. {float distanceToEachOther= sqrt(sq(myX-badThingX)+sq(myY-badThingY));
  28. if (distanceToEachOther <33) {
  29.   println("Crash");
  30. }
  31. else {
  32.   println("No Collision");
  33. }
  34. ellipse (badThingX, badThingY, 20, 20);
  35. }

Re: Game Project

1 year ago
You have an entire block of code and two variable declarations stored outside a function - move that close curly brace around and it should work. Hint: use Edit > Auto Format...

Re: Game Project

1 year ago
could you be more specific please? I'm having trouble locating what you are talking about. Thanks!

Re: Game Project

1 year ago
never mind. I figured it out. Thank you very much!