Intersect function stops working after the first intersect - java

im making a game, but i ran into a problem while working with the function "intersect".
the senario looks like this; i've made a game where the player is a rectangle were the objective is to kill the enemy rectangle. The enemy "boss" rectangle has two "simulations" that simulate a movement and an attack these simulations are driven by vectors. The movement is horizantally back and forth and the attack is in a vertical maner, a charge type of deal. The boss also has a rectangular target area where if itersected the "boss" will charge across the screen.
now the problem comes when i tried to make it so that if the player intersects with the rectangular target area the "boss" will attack/charge. The boss attack/charge the first time the player intersects but not after that. I want the boss to follow the same pantern, is that he should only be able to go from side to side horizantaly and if Target area intersected; attack/charge verticaly.
( i will include some code bellow. sorry if my english is bad. )
first comes the main:
boss b;
Character C;
void setup(){
C = new Character();
b = new boss();
}
void draw(){
if (play) {
b.simulate(); //horizantal movement
}
if (b.start) {
b.sim(); //boss vertical attack
}
if (b.intersects(C)){
play = false;
b.start = true;
}
C.character(); //player
b.bounce(); //makes it bounce if horizantal. and stop if vertical
b.Display(); //boss
b.display(); //boss target area
}
Next comes the boss:
class boss {
int x = 10 ;
int y = 10 ;
boolean start = false;
int RW = 50;
int RH = 700;
boolean up = false;
boolean down = true;
boss() {
Location = new PVector( x+25, y ); //vector for the location of the boss
Velocity = new PVector( 5, 0 ); // vector for horizaltal movement
speed = new PVector( 0, 10 ); // vector for vertical down movement
speed2 = new PVector(0, -10); // vector for vertical up movement
}
void bounce() {
if ((Location.x == width) ||(Location.x == 0)) { //horizantal movement bounce on screen edge
Velocity.x = Velocity.x * -1;
}
if ((Location.y == 650) || (Location.y == 0)) {
start = false; //makes boss stop if reaches botton or top of the screen
play = true;
if (Location.y == 650) {
RH = -700;
up = true;
down = false; //specificly if it reaches top of screen
}
if (Location.y == 0) {
RH = 700;
down = true; //specificly if it reaches bottom of screen
up = false;
}
}
}
void simulate() {
Location.add(Velocity); //simulates horizantal movement
}
void sim() {
if (down) {
Location.add(speed); //simulates up and down attacking movemnet
}
if (up) {
Location.add(speed2);
}
}
boolean intersects(Character C) {
return Location.x < C.x + C.w && C.x < Location.x + RW &&
Location.y < C.y + C.h && C.y < Location.y + RH; //intersect between player and boss targeting area
}
void Display() {
rect( Location.x, Location.y, 50, 50 ); //boss
}
void display() {
rect( Location.x, Location.y+50, RW, RH ); //boss targeting area
}
}
if anything is unclear i will gladly clear up any confusion. :)

Thanks for sharing the code. It made it much easier.
When I comment this piece (not the if-condition the reset of start after the if)
if ((Location.y == 650) || (Location.y == 0)) {
// start = false;
then the boss starts going back up top and still calls intersects the Character when the co-ordinates match. However, post this the boss keeps bouncing up and down.
There is surely more work to be done on this to take account of the bullets fired and the hits to boss.
Hope this helps. :) It was fun debugging this code. I had never used PDE before this.
ok, here is the "edit"..
Now,
Good News: I can make it go in both directions only when it intersects
Bad News: It keeps going to the other side as long as it is intersecting. So if the Character is stationary then boss keeps intersecting and passing to the other side at least 8-10 times in the current speed.
Anyways, here is the summary. I added and isAttacking flag that tells the program to not stop the boss from crossing over if it has reached the bottom of the frame. The other thing I changed was the intersection condition. Now it just checks for intersection on the X-Axis. If you must compare intersection on the Y-Axis too then intersects is where you need to change & test.
After the long explanation :P Here is the code. Hope this is better.
Main
boss b;
Character C;
Inventory I;
Bullet B;
int previousKey = 0;
int lastKey;
int lastKeyCode;
void setup() {
C = new Character();
b = new boss();
I = new Inventory();
background(128, 128, 128);
size( 700, 700 );
strokeWeight( 10 );
frameRate( 30 );
}
void keyPressed() {
if (key == CODED) {
previousKey = keyCode;
if (keyCode == UP) {
C.MoveUP();
}
if (keyCode == LEFT) {
C.MoveLEFT();
}
if (keyCode == DOWN) {
C.MoveDOWN();
}
if (keyCode == RIGHT) {
C.MoveRIGHT();
}
}
if (key == 'w' || key == 'W') {
attack();
}
if ( key == 'q' || key == 'Q' ) {
if (I.Shoot == true) {
B = new Bullet(C.x, C.y);
this.Shoot();
}
} else if (key == 'e' || key == 'E') {
I.changePop();
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
//play = false;
//b.start = true;
}
}
}
void attack() {
if (I.Attack == true) {
if (previousKey == UP) {
C.AttackUP();
}
if (previousKey == LEFT) {
C.AttackLEFT();
}
if (previousKey == DOWN) {
C.AttackDOWN();
}
if (previousKey == RIGHT) {
C.AttackRIGHT();
}
}
}
void Shoot() {
if (I.Shoot == true) {
if (previousKey == UP) {
B.ShootUP();
}
if (previousKey == LEFT) {
B.ShootLEFT();
}
if (previousKey == DOWN) {
B.ShootDOWN();
}
if (previousKey == RIGHT) {
B.ShootRIGHT();
}
}
}
boolean play = true;
void keyReleased() {
lastKey = 0;
lastKeyCode = 0;
}
void draw() {
background(128, 128, 128);
if (play) {
b.simulate();//side to side
}
if (b.start) {
b.sim(); //boss rush
}
if (b.intersects(C)) {
b.isAttacking = true;
play = false;
b.start = true;
} else {
b.isAttacking= false;
}
C.character();
b.bounce();
b.Display();//boss
b.display();//rush area
C.HPbar();
I.popUp();
if ( key == 'q' || key == 'Q' ) {
if (I.Shoot == true) {
B.bullet();
B.Simulate();
}
}
}
Enemies or Boss
class boss {
PVector Location;
PVector Velocity;
PVector speed;
PVector speed2;
int x = 10 ;
int y = 10 ;
boolean start = false;
int RW = 50;
int RH = 700;
boolean up = false;
boolean down = true;
boolean isAttacking = false;
boss() {
Location = new PVector( x+25, y );
Velocity = new PVector( 5, 0 );
speed = new PVector( 0, 10 );
speed2 = new PVector(0, -10);
}
void bounce() {
if ((Location.x == width) ||(Location.x == 0)) {
Velocity.x = Velocity.x * -1;
}
if ((Location.y == 650) || (Location.y == 0)) {
if (!isAttacking) {
start = false;
}
play = true;
if (Location.y == 650) {
RH = -700;
up = true;
down = false;
}
if (Location.y == 0) {
RH = 700;
down = true;
up = false;
}
}
}
void simulate() {
Location.add(Velocity);
}
void sim() {
//print("\n In Sim UP: [" + up + "] Down: [" + down + "] Location [" + Location + "]");
if (down) {
Location.add(speed);
}
if (up) {
Location.add(speed2);
}
}
boolean intersects(Character C) {
//print ("\nUP: [" + up + "] Down: [" + down + "] X: [" + (Location.x < (C.x + C.w) && (Location.x + RW) > C.x)
//+ "] Y: [" + (Location.y < (C.y + C.h) && (Location.y + RH) > C.y) + "]");
return Location.x < (C.x + C.w) && (Location.x + RW) > C.x;
//&&
// Location.y < (C.y + C.h) && (Location.y + RH) > C.y ;
}
void Display() {
pushStyle();
stroke(0);
fill(255, 0, 0);
rect( Location.x, Location.y, 50, 50 );
popStyle();
}
void display() {
pushStyle();
stroke(0);
strokeWeight(0);
fill(255, 0, 0, 20);
rect( Location.x, Location.y+50, RW, RH );
popStyle();
}
}

Related

Processing color collision detection (java)

I have to make a tron type game with collision detection. I started by making p1 and p2 move and leave a trail behind them.
I tried to make a color collision detection but its not working. I have different variables such as "P1_edge" which is when p1 hits the edge of the screen. I mark that with a red border.
But the only one that seem to be working is when they crash into themselves when the game starts. It instantly finishes as they are already on themselves. if I take that bit away, nothing else detects.
int P1_XPos; //Player 1 position
int P1_YPos;
boolean Player1_Up = false; //Determins what way P1 is going
boolean Player1_Down = false;
boolean Player1_Left = false;
boolean Player1_Right = true;
int P2_XPos; //Player 2 position
int P2_YPos;
boolean Player2_Up = false; //Determins what way P2 is going
boolean Player2_Down = false;
boolean Player2_Left = true;
boolean Player2_Right = false;
boolean Game_State = false; //see if the game is over (false = over)
final int P1_Edge = 0; //P1 crashed into the edge
final int P1_P1 = 1; //P1 crashed into P1
final int P1_P2 = 2; //P1 crashed into P2
final int P2_Edge = 3; //P2 crashed into the edge
final int P2_P2 = 4; //P2 crashed into P2
final int P2_P1 = 5; //P2 crashed into P1
final int Crash = 6; //Other
void setup()
{
size(700,600); //Set screen dimensions
background(0, 0, 0);
P1_XPos = 100; //set P1 and P2 posision
P1_YPos = 100;
P2_XPos = 600;
P2_YPos = 500;
strokeWeight(3); //Draw the edge of the screen
stroke(255, 0, 0);
line(1, 1, 1, height - 1);
line(1, 1, width - 1, 1);
line(width - 2, 1, width - 2, height - 1);
line(1, height - 2, width - 1, height - 2);
stroke(0, 255, 0); //Draw the starting positions
point(P1_XPos, P1_YPos);
stroke(0, 0, 255);
point(P2_XPos, P2_YPos);
}
void draw()
{
strokeWeight(3);
if (Game_State == true) //if the game is not over
{
stroke(0, 255, 0); //Draw P1
point(P1_XPos, P1_YPos);
MovePlayer1(); //Move P1
stroke(0, 0, 255); //Draw P2
point(P2_XPos, P2_YPos);
MovePlayer2(); //Move P2
Collision_Detection(); //Detect any crashes
}
}
void keyPressed()
{
if(key == CODED) //Controls P1 movement
{
if (keyCode == UP)
{
Player1_Up = true;
Player1_Down = false;
Player1_Left = false;
Player1_Right = false;
}
else if(keyCode == DOWN)
{
Player1_Down = true;
Player1_Up = false;
Player1_Left = false;
Player1_Right = false;
}
else if (keyCode == LEFT)
{
Player1_Left = true;
Player1_Down = false;
Player1_Up = false;
Player1_Right = false;
}
else if (keyCode == RIGHT)
{
Player1_Right = true;
Player1_Down = false;
Player1_Left = false;
Player1_Up = false;
}
}
if ((key == 'W') || (key == 'w')) //Controls P2 movement
{
Player2_Up = true;
Player2_Down = false;
Player2_Left = false;
Player2_Right = false;
}
else if((key == 'S') || (key == 's'))
{
Player2_Down = true;
Player2_Up = false;
Player2_Left = false;
Player2_Right = false;
}
else if ((key == 'A') || (key == 'a'))
{
Player2_Left = true;
Player2_Down = false;
Player2_Up = false;
Player2_Right = false;
}
else if ((key == 'D') || (key == 'd'))
{
Player2_Right = true;
Player2_Down = false;
Player2_Left = false;
Player2_Up = false;
}
if (key == ' ')
{
Game_State = true;
}
}
void MovePlayer1() //Moves P1
{
if(Player1_Up == true)
{
P1_YPos -= 1;
}
if(Player1_Down == true)
{
P1_YPos += 1;
}
if(Player1_Left == true)
{
P1_XPos -= 1;
}
if(Player1_Right == true)
{
P1_XPos += 1;
}
}
void MovePlayer2() //Moves P2
{
if(Player2_Up == true)
{
P2_YPos -= 1;
}
if(Player2_Down == true)
{
P2_YPos += 1;
}
if(Player2_Left == true)
{
P2_XPos -= 1;
}
if(Player2_Right == true)
{
P2_XPos += 1;
}
}
int TestColorP1 (color P1Test) //Detect what color P1 is touching
{
if (P1Test == color (255,0,0))
return P1_Edge;
else if (P1Test == color(0,255,0))
return P1_P1;
else if (P1Test == color(0,0,255))
return P1_P2;
else return Crash;
}
int TestColorP2 (color P2Test) //Detect what color P2 is touching
{
if (P2Test == color (255,0,0))
return P2_Edge;
else if (P2Test == color(0,255,0))
return P2_P1;
else if (P2Test == color(0,0,255))
return P2_P2;
else return Crash;
}
void Collision_Detection()
{
color P1_Pixel; //This is the color P1 is touching
color P2_Pixel;
P1_Pixel = get(P1_XPos, P1_YPos); //Declare is as P1 position
P2_Pixel = get(P2_XPos, P2_YPos);
if (TestColorP1(P1_Pixel) == P1_Edge) //See if P1 has crashed
{
background(0);
Game_State = false;
}
else if (TestColorP1(P1_Pixel)== P1_P1)
{
// background(0);
// Game_State = false;
}
else if (TestColorP1(P1_Pixel) == P1_P2)
{
background(0);
Game_State = false;
}
if (TestColorP2(P2_Pixel) == P2_Edge) //See if P2 has crashed
{
background(0);
Game_State = false;
}
else if (TestColorP2(P2_Pixel)== P2_P1)
{
background(0);
Game_State = false;
}
else if (TestColorP2(P2_Pixel) == P2_P2)
{
// background(0);
// Game_State = false;
}
else if (TestColorP2(P2_Pixel) == Crash)
{
}
}
I know it's long, but you should be able to just copy and paste this in a processing sketch and it will work. I have also left comments so you know why I've added each bit of code.
This was a real head scratcher- good question.
The biggest thing throwing you off is the fact that Processing enables anti-aliasing by default. This causes your colors to be just a little different than what you expect. In most sketches, this is a good thing, since it makes things look better. But since you're using the exact color values, this is screwing you up. You can prove this by taking a screenshot of your sketch and then sampling your colors.
To disable this, simply call noSmooth() at the beginning of your sketch. More info can be found in the reference here.
The next thing screwing you up is that your stroke weight is set to 3, but you're only moving the players 1 pixel at a time. This causes the players to stay "inside" the last point that was drawn, which is why they're constantly running into themselves.
To fix that, you could simply call strokeWeight(1); at the beginning of the draw() function. Or if you need a stroke weight of 3, then make sure you move the player outside of the circle that was just drawn.
That'll fix your problem here, but in the long run, you'd probably be better off keeping track of previous player positions in a data structure like an ArrayList of PVectors. Then you'd redraw them each time draw() was called instead of only drawing them once. And instead of trying to check colors manually, it would be easier to do collision checks in only certain parts of the paths, to avoid the above case.

Ball going through paddle when increasing velocity

I am trying to create a Pong game and so far I have everything working for me other than the ball. At first I had the ball's x and y axis move up by one space each time. It was working fine until i decided to increase it to 2. I cant figure out what is wrong with my code and I need help.
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pong extends JPanel implements KeyListener{
int x = 90;
int y = 90;
int rectytop = 30;
int rectytop2 = 30;
int rectybottom = rectytop + 100;
int rectybottom2 = rectytop2 + 100;
int border = 30;
boolean balldown = true;
boolean ballright = true;
boolean playerborderup = false;
boolean playerborderdown = false;
boolean balltouch1 = false;
boolean balltouch2 = false;
private void moveball() {
if (balldown == true){
y = y + 2;
}
if (y == getHeight()-border){
balldown = false;
}
if (balldown == false){
y = y - 2;
}
if (ballright == true){
x = x + 2;
}
if (x == getWidth()-border){
ballright = false;
}
if (ballright == false){
x = x - 2;
}
if (y == 0){
balldown = true;
}
if (x == 0){
ballright = true;
}
if (balltouch1 == false){
if (x == 75){
if(y < rectybottom && y > rectytop){
ballright = true;
}
}
}
if (balltouch2 == false){
if (x == 390 && y < rectybottom2 && y > rectytop2){
ballright = false;
}
}
}
#Override
public void paint(Graphics g){
super.paint(g);
//drawing ball and paddles
g.fillOval(x, y, 30, 30);
g.fillRect(45 , rectytop, 30, 100);
g.fillRect(425, rectytop2, 30, 100);
}
public static void main(String[] args) throws InterruptedException {
//making the window
JFrame f = new JFrame("Pong Game");
Pong game = new Pong();
f.setVisible(true);
f.setSize(500, 500);//1024, 724
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addKeyListener(game);
//game code
f.add(game);
while (true){
game.repaint();
game.moveball();
Thread.sleep(10);
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
//player one
if (e.getKeyCode() == KeyEvent.VK_W){
if (rectytop == 0){
playerborderup = true;
}
if (rectytop != 0){
playerborderup = false;
}
if (playerborderup == true){
rectytop = rectytop + 0;
rectybottom = rectytop + 100;
repaint();
}
if (playerborderup == false){
rectytop = rectytop - 5;
rectybottom = rectytop + 100;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_S){
if (rectytop == 585){
playerborderdown = true;
}
if (rectytop != 585){
playerborderdown = false;
}
if (playerborderdown == true){
rectytop = rectytop - 0;
rectybottom = rectytop + 100;
repaint();
}
if (playerborderdown == false){
rectytop = rectytop + 5;
rectybottom = rectytop + 100;
repaint();
}
}
//player two
if (e.getKeyCode() == KeyEvent.VK_UP){
if (rectytop2 == 0){
playerborderup = true;
}
if (rectytop2 != 0){
playerborderup = false;
}
if (playerborderup == true){
rectytop2 = rectytop2 + 0;
rectybottom2 = rectytop2 + 100;
repaint();
}
if (playerborderup == false){
rectytop2 = rectytop2 - 5;
rectybottom2 = rectytop2 + 100;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
if (rectytop2 == 585){
playerborderdown = true;
}
if (rectytop2 != 585){
playerborderdown = false;
}
if (playerborderdown == true){
rectytop2 = rectytop2 - 0;
rectybottom2 = rectytop2 + 100;
repaint();
}
if (playerborderdown == false){
rectytop2 = rectytop2 + 5;
rectybottom2 = rectytop2 + 100;
repaint();
}
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
Since you are moving by 2 every time, it may "skip" over some coordinates. Thus when you say
if (y == getHeight()-border){
balldown = false;
}
y might go from getHeight()-border + 1 to getHeight()-border - 1 and never meet this condition. Thus, change it to a range or a less than. Your new code should be
if (y <= getHeight()-border +1){
balldown = false; //change the +1 and -1 to have difference at least speed number
}
Note that you must do the same for the other ifs with ==, change
if (x == getWidth()-border){
ballright = false;
}
if (y == 0){
balldown = true;
}
if (x == 0){
ballright = true;
}
to
if (x <= getWidth()-border +1){
ballright = false;
}
if (y <= 1){
balldown = true;
}
if (x <= 1){
ballright = true;
}
Note that you can resolve the problem also by saying if the position is eer exactly one away from the border, temporarily decrement position by 1 instead of 2.
You are using if (x == 75) to detect collision with the left paddle. But if speed is 2 than the ball's x is always even and never equals 75. For a quick fix, check x against a range: if (x > 60 && x < 75)

Java Applet Grid Game Flashing On Refresh/Keypress

I made a simple game on a grid in java that involves controlling a white square with the WASD keys. Whenever I press W,S,A, or D, the screen sometimes flickers before drawing the grid again. I am fairly new to coding, so the more you can dumb it down for me, the better.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* DotGame.applet
*
* A simple game where the player controls a white circle with the WASD keys.
* - If the user moves his circle over a green circle, his score will go up by 1, and another red circle will spawn.
* - If the user moves his circle over a red circle, his score will go down by 1, and one less red circle will be generated.
* - There is no win condition, it is just a test game.
*
* #author -----------
* #version 11-9-2014
*/
public class DotGame extends Applet
implements KeyListener, MouseListener
{
int width,height;
int powerup = 1;
int click = 0;
int x,y;
final int size = 40;
int ox = size,oy = size;
int rx = 0, ry = 0;
int reddots = 0;
int red[][] = new int[1000][2];
String s = "";
int score = 0;
int DrawGrid = 0;
String sc = "";
int powerupdots = 1;
int yellow[][] = new int[1][2];
int powerupcounter = 0;
int shield = 0;
int blue[][] = new int[1][2];
public void init()
{
this.setSize(740,500);
width = 640;
height = 480;
setBackground( Color.black );
x = width/2;
y = height/2;
s = "CLICK TO START";
addMouseListener( this );
addKeyListener( this );
}
public void keyPressed( KeyEvent e ) { }
public void keyReleased ( KeyEvent e ) { }
public void keyTyped( KeyEvent e )
{
char c = e.getKeyChar();
String t = c+"";
//This will change the coordinates of the user-controlled circle by the size of the circle based on what button is pressed
if(powerupcounter > 0)
{
powerup = 2;
}
else if(powerupcounter == 0)
{
powerup = 1;
}
if( t.equalsIgnoreCase("w" )&& oy > 0+((powerup-1)*size))
{
oy = oy-(size*powerup);
}
else if( t.equalsIgnoreCase("s") && oy < height-(size*powerup))
{
oy = oy+(size*powerup);
}
else if( t.equalsIgnoreCase("a")&& ox > 0+((powerup-1)*size))
{
ox = ox-(size*powerup);
}
else if( t.equalsIgnoreCase("d") && ox < width-(size*powerup))
{
ox = ox+(size*powerup);
}
else if(t.equalsIgnoreCase("w" ) && oy == 0)
{
oy = height-(size*powerup);
}
else if(t.equalsIgnoreCase("s") && oy == height-size)
{
oy = 0+((powerup-1)*size);
}
else if(t.equalsIgnoreCase("a") && ox == 0)
{
ox = width-(size*powerup);
}
else if(t.equalsIgnoreCase("d") && ox == width-size)
{
ox = 0+((powerup-1)*size);
}
else if(t.equalsIgnoreCase("w") && oy == size && powerup ==2)
{
oy = height-size;
}
else if(t.equalsIgnoreCase("s") && oy == height -(size*powerup) && powerup ==2)
{
oy = 0;
}
else if(t.equalsIgnoreCase("a") && ox == size && powerup ==2)
{
ox = width-size;
}
else if(t.equalsIgnoreCase("d") && ox == width -(size*powerup) && powerup ==2)
{
ox = 0;
}
if(powerupcounter > 0)
{
powerupcounter--;
}
repaint();
e.consume();
}
public void mouseEntered( MouseEvent e) {}
public void mouseExited( MouseEvent e) {}
public void mousePressed( MouseEvent e) {}
public void mouseReleased( MouseEvent e) {}
public void mouseClicked( MouseEvent e)
{
if(click == 0)
{
randomYellow();
randomBlue();
DrawRandom();
x = e.getX();
y = e.getY();
s = "";
repaint();
e.consume();
click = 1;
}
}
public void CheckScore()
{
if(ox == rx && oy == ry)
{
score++;
reddots+=10;
DrawRandom();
}
}
public void DrawRandom()
{
//The reason we divide by the size and then multiply after it is an int is to
//make sure that the random circle drawn is in the same base as the size of the circle,
//so that the player's circle can move directly over it, and not be of by a fraction
//of the predetermined size.
rx = (int)(Math.random()*width/size)*size;
ry = (int)(Math.random()*height/size)*size;
while(rx == ox && ry == oy)
{
rx = (int)(Math.random()*width/size)*size;
ry = (int)(Math.random()*height/size)*size;
}
for(int y = 0 ; y < reddots ; y++)
{
red[y][0] = (int)(Math.random()*width/size)*size;
red[y][1] = (int)(Math.random()*height/size)*size;
while(red[y][0] == rx && red[y][1] == ry || red[y][0] == yellow[0][0] && red[y][1] == yellow[0][1]
|| red[y][0] == ox && red[y][1] == oy || red[y][0] == blue[0][0] && red[y][1] == blue[0][1])
{
red[y][0] = (int)(Math.random()*width/size)*size;
red[y][1] = (int)(Math.random()*height/size)*size;
}
}
}
public void randomYellow()
{
yellow[0][0] = (int)(Math.random()*width/size)*size;
yellow[0][1] = (int)(Math.random()*height/size)*size;
while(yellow[0][0] == rx && yellow[0][1] == ry)
{
yellow[0][0] = (int)(Math.random()*width/size)*size;
yellow[0][1] = (int)(Math.random()*height/size)*size;
}
}
public void randomBlue()
{
blue[0][0] = (int)(Math.random()*width/size)*size;
blue[0][1] = (int)(Math.random()*height/size)*size;
while(blue[0][0] == rx && blue[0][1] == ry || blue[0][0] == yellow[0][0] && blue[0][1] == yellow[0][1])
{
blue[0][0] = (int)(Math.random()*width/size)*size;
blue[0][1] = (int)(Math.random()*height/size)*size;
}
}
public void CheckDeath()
{
for(int y = 0 ; y < reddots ; y++)
{
if(ox == red[y][0] && oy == red[y][1] && shield == 0)
{
score--;
reddots--;
DrawRandom();
}
else if(ox == red[y][0] && oy == red[y][1] && shield !=0)
{
shield--;
DrawRandom();
}
}
}
public void CheckPowerup()
{
for(int y = 0 ; y < powerupdots ; y++)
{
if(ox == yellow[y][0] && oy == yellow[y][1])
{
powerupcounter += 10;
randomYellow();
}
}
}
public void CheckShield()
{
if(ox == blue[0][0] && oy == blue[0][1] && shield < 5)
{
shield++;
randomBlue();
}
else if(ox == blue[0][0] && oy == blue[0][1] && shield >= 5)
{
randomBlue();
}
}
public void CheckWin( Graphics g )
{
g.setColor(Color.black);
g.fillRect(0,0,width,height);
while(1 == 1)
{
g.drawString( "YOU WIN" , width/2, height/2);
}
}
public void paint( Graphics g )
{
CheckScore();
if(score == 20)
{
CheckWin(g);
}
CheckDeath();
CheckPowerup();
CheckShield();
DrawGrid(g);
g.setColor(Color.yellow);
g.fillRect(yellow[0][0],yellow[0][1],size+1,size+1);
g.setColor(Color.red);
for(int y = 0; y < reddots ; y++)
{
g.fillRect(red[y][0],red[y][1],size+1,size+1);
}
sc = ""+score;
//Draw user
g.setColor(Color.white);
g.fillRect(ox,oy,size+1,size+1);
//Draw shield around user if they have shields (max 5)
if(shield >= 1)
{
g.setColor(Color.blue);
g.fillRect(ox,oy,size+1,5);
g.fillRect(ox,oy,5,size+1);
g.fillRect(ox,oy+size-4,size+1,5);
g.fillRect(ox+size-4,oy,5,size+1);
}
//Draw green tile
g.setColor(Color.green);
g.fillRect(rx,ry,size+1,size+1);
//Draw shield tile
g.setColor(Color.blue);
g.fillRect(blue[0][0],blue[0][1],size+1,size+1);
g.setColor( Color.green );
g.drawString( s, x, y );
g.drawString( "Score : "+sc, 650, 20);
g.drawString( "Powerups : "+powerupcounter, 650, 40);
g.drawString( "Red Dots : "+reddots, 650,60);
g.drawString( "Shield : "+shield,650,80);
}
public void DrawGrid( Graphics g )
{
g.setColor(Color.orange);
for(int x = 0 ; x <= width ; x += size)
{
g.drawLine(x,0,x,height);
}
for(int y = 0 ; y <= height ; y+= size)
{
g.drawLine(0,y,width,y);
}
}
}
My advice is to move as much of your calculation as possible into methods called from your keypress events, and keep your paint() method as short and direct as possible. Don't do math inside your paint() method if you can help it - use your keypress method to build the picture of everything you need to paint before you call repaint(), and make your paint() method's only job to be to draw the current state of the board. Also, don't redraw the whole board anytime you don't have to. For instance, when moving from the current square to an empty square, all you should draw is a black square over your previous position, and a new user square in the new position. When hitting a yellow or blue square, all you need to draw is the new yellow or blue square (and update the appropriate status messages). You only need to redraw the whole board when hitting a red or green square.

Processing game: Gif images not respawning as intended

I am a diploma student making a game based on processing, it's a tower defense game where a heroine would be shooting arrows at zombies charging towards the castle. I have multiple issues with this.
The charger zombies do not respawn after being shot dead.
All of the charger zombies die together when I shot only one charger zombie.
Supposedly I wanted the zombies to spawn in waves repeatedly after a group of them has been shot down, and then I want the charger zombies to die individually, depending on the one I click on.
Here's my source code:
import processing.opengl.*; // transparency
import gifAnimation.*; // after add gifAnimation library
import ddf.minim.*; // importing music
PImage backgroundImg;
PImage animation;
int counter;
//settings
int score = 50, totalScore = 0;
int titleX = 200, titleY = 200;
boolean shotsFired = false;
PFont scorePoints, castleHealthPoints, font; //font for startingScreen
int chargerKilled;
//Charger class
int maxChargerOnScreen;
Charger[] chargerArr; //chargerArray undefined
int chargerIndex;
final int numCharger = 300;
ArrayList<Charger> onScreen = new ArrayList<Charger>();
boolean chargerReachedTarget = false, chargerShot = false, chargerDive = true;
Gif chargerCharge, chargerAttack, chargerDied, shootingStance;
int chargerMoveX = 800, chargerMoveY = 450;
// CastleWallHealth
int castleHealth = 1000;
// audio
Minim minim;
AudioPlayer song;
AudioSnippet arrowShoot;
//-------------------------------Codes start here --------------------------------------------------//
void setup() {
//ipad mini size
// frameRate(10);
frameRate(60);
size(800, 600);
smooth();
minim = new Minim(this);
// song = minim.loadFile("InGameSoundtrack.mp3");
// song.play();
arrowShoot = minim.loadSnippet("arrowSound1.wav");
backgroundImg = loadImage("GameBackground.jpg");
backgroundImg.resize(800, 600);
chargerCharge = new Gif(this, "ChargerCharge.gif");
chargerAttack = new Gif(this, "ChargerAttack.gif");
chargerDied = new Gif(this, "ChargerDie.gif");
gargoyleFly = new Gif(this, "GargoyleFly.gif");
gargoyleAttack = new Gif(this, "GargoyleAttack.gif");
shootingStance = new Gif(this, "ShootingGif.gif");
scorePoints = createFont("Arial", 16, true);
castleHealthPoints = createFont("Arial", 16, true);
// loopingGif.play() plays the animation without loop
// must make charger
chargerCharge.play();
chargerAttack.play();
chargerDied.play();
resetGames(); // starts with respawning 5 charges
}
void draw() {
// backgroud
background(backgroundImg);
image(shootingStance, 120,200);
// scores and health
textFont(scorePoints, 15);
fill(0);
text("Score: " + totalScore, 100, 100);
displayCastleHealth();
//*********************************************charger settings code********************************************************************
// If there's no more zombies. prompt reset (screen no zombies) , index from 0 add till 300 then game won
if (onScreen.size() == 0 && chargerIndex == numCharger) {
println("Game won! :)");
// gameWon = true;
}
// while onScreen charger less than 5 only and chargerIndex havent reach 300 it will only respawn
while (onScreen.size () < maxChargerOnScreen && chargerIndex <= numCharger - 1) {
if (chargerIndex < chargerArr.length) { // <--- this is the problem chargerArrayLength
Charger chargerMobs = chargerArr[chargerIndex];
// starts from chargerArray[0] to chargerArray[300]
onScreen.add(chargerMobs);
chargerIndex++;
// chargerIndex keeps looping why?
// println("ADDED ZOMBIE TO ONSCREEN Zombs on screen: " + onScreen.size());
}
}
// Adjusts maxZombOnScreen based off the current index Difficulties?
if (chargerIndex == 10)
maxChargerOnScreen++;
else if (chargerIndex == 50)
maxChargerOnScreen++;
else if (chargerIndex == 100)
maxChargerOnScreen += 2;
else if (chargerIndex == 150)
maxChargerOnScreen += 2;
else if (chargerIndex == 190)
maxChargerOnScreen += 3;
else if (chargerIndex == 250)
maxChargerOnScreen += 3;
else if (chargerIndex == 260)
maxChargerOnScreen += 3;
else if (chargerIndex == 270)
maxChargerOnScreen += 5;
// removes dead zombies, living zombies act normally
for(int i = 0; i < onScreen.size(); i++) {
Charger c = onScreen.get(i);
// add money when a zombie is dead
// if (c.getHealth() <= 0)
if (chargerShot==true) {
c.chargerDying();
onScreen.remove(i);
chargerKilled++;
i--;
} else {
c.act(); ///it should just act
// c.displayHealth(); // if gt time only do
// }
}
}
// creating list of predetermined zombies and their characteristics
println("chargerIndex: " + chargerIndex); // debug
println("onScreenSize: " + onScreen.size());
// println("ChargerArray: " + chargerArray.length); chargerArray point null?
// cursor
if (mouseY > height - 500) {
noFill();
stroke(255, 0, 0);
ellipse(mouseX, mouseY, 35, 35);
line(mouseX - 25, mouseY, mouseX + 25, mouseY);
line(mouseX, mouseY - 25, mouseX, mouseY + 25);
}
}
void resetGames() {
// resetting variables
chargerIndex = 0;
maxChargerOnScreen = 5;
chargerKilled = 0;
createNewCharger();
}
void createNewCharger() {
Charger[] temp = new Charger[numCharger];
for (int i = 0; i < numCharger; i++)
temp[i] = new Charger((int) random(800, 1200), chargerMoveY);
chargerArr = temp;
}
//____________________________________________________________________________________________________________-
class Charger {
private int chargerMoveX, chargerMoveY;
Gif chargerGif; // by default
// chargerGif will change based on input (either it reaches the castle or its shot)
// no constructor
Charger() {
chargerMoveX = 0;
chargerMoveY = 450;
}
// overloaded constructor
Charger(int cMoveX,int cMoveY) {
chargerMoveX = cMoveX;
chargerMoveY = cMoveY;
}
void act() { // use
// if reached castle it wont move
if (chargerMoveX <= 180) {
// remove gif images
chargerDive = false;
chargerReachedTarget = true;
}
// if not shot or reached castle it will move
if (chargerDive == true) {
chargerGif = chargerCharge;
image(chargerGif, chargerMoveX, chargerMoveY);
chargerMoveX -= 2;
}
// if reached castle perform attack animation
if (chargerReachedTarget == true) {
chargerGif = chargerAttack;
image(chargerGif, chargerMoveX - 80, chargerMoveY - 120);
}
if (mousePressed &&
mouseX < chargerMoveX + 180
&& mouseX > chargerMoveX
&& mouseY < chargerMoveY + 120
&& mouseY > chargerMoveY) {
// score board
totalScore = totalScore + score ;
float r = random(50);
println(r);
chargerShot = true;
chargerDive = false;
}
}
void chargerDying() {
chargerReachedTarget = false;
chargerGif = chargerDied;
image(chargerGif, chargerMoveX, chargerMoveY);
}
}
void displayCastleHealth() {
if (castleHealth <= 0) {
fill(250, 70, 0);
textFont(castleHealthPoints, 20);
text("HP: 0", 100, 50);
} else {
fill(250, 70, 0);
textFont(castleHealthPoints, 20);
text("HP: " + castleHealth, 100, 50);
}
}
void mousePressed() {
strokeWeight(2);
arrowShoot.rewind();
arrowShoot.play();
for (int i = 0 ; i < 3; i++)
shootingStance.play();
shotsFired = true;
}
void mouseReleased() {
strokeWeight(1);
if (shotsFired == true)
shootingStance.pause();
}
public void stop() {
arrowShoot.close();
}
Majority of the problems from the charger came from here:
if (onScreen.size() == 0 && chargerIndex == numCharger) {
println("Game won! :)");
// gameWon = true;
}
// while onScreen charger less than 5 only and chargerIndex havent reach 300 it will only respawn
while (onScreen.size () < maxChargerOnScreen && chargerIndex <= numCharger - 1) {
if (chargerIndex < chargerArr.length) { // <--- this is the problem chargerArrayLength
Charger chargerMobs = chargerArr[chargerIndex];
// starts from chargerArray[0] to chargerArray[300]
onScreen.add(chargerMobs);
chargerIndex++;
// chargerIndex keeps looping why?
// println("ADDED ZOMBIE TO ONSCREEN Zombs on screen: " + onScreen.size());
}
}
// Adjusts maxZombOnScreen based off the current index Difficulties?
if (chargerIndex == 10)
maxChargerOnScreen++;
else if (chargerIndex == 50)
maxChargerOnScreen++;
else if (chargerIndex == 100)
maxChargerOnScreen += 2;
else if (chargerIndex == 150)
maxChargerOnScreen += 2;
else if (chargerIndex == 190)
maxChargerOnScreen += 3;
else if (chargerIndex == 250)
maxChargerOnScreen += 3;
else if (chargerIndex == 260)
maxChargerOnScreen += 3;
else if (chargerIndex == 270)
maxChargerOnScreen += 5;
// removes dead zombies, living zombies act normally
for(int i = 0; i < onScreen.size(); i++) {
Charger c = onScreen.get(i);
// add money when a zombie is dead
// if (c.getHealth() <= 0)
if (chargerShot==true) {
c.chargerDying();
onScreen.remove(i);
chargerKilled++;
i--;
} else {
c.act(); ///it should just act
// c.displayHealth(); // if gt time only do
// }
}
}
Evaluation and criticism is appreciated.
There are several issues with the code, but for now, I'll ignore the parts that are somewhat "not clean" or "not elegant", and focus on the main problem that was the reason for the question:
The state of all Chargers instances was the same. The fields
boolean chargerReachedTarget = false, chargerShot = false, chargerDive = true;
have been defined globally. Just by moving this line (that is, these 3 fields) into the Charger class, and changing the line
if (chargerShot==true) {
to
if (c.chargerShot==true) {
the game already seemed to be much closer to what you presumably have been looking for. After this modification, it was possible to shoot individual opponents, and new opponents kept coming after the old ones had disappeared.
Again: The code is not really nicely structured, and there are many possible minor improvements. For example, something like if (c.chargerShot==true) { ... } should better be if (charger.isShot()) { ... }. But I'm not so familiar with processing and its best practices, so I'm not sure how the "best" solution would look like in a global sense...

Collision bug in a 2D platform game

I'm new to java, and game programming and I'm starting my first big project which is a 2D platform puzzle game.
This is my player movement code
if (speedX > 0 && centerX <= 400){
centerX += speedX;
}
if (speedX < 0 && centerX >= 400){
centerX += speedX;
}
if (speedX > 0 && centerX >= 400){
bg1.setSpeedX(-MOVESPEED);
bg2.setSpeedX(-MOVESPEED);
}
if (speedX < 0 && centerX <= 400){
bg1.setSpeedX(MOVESPEED);
bg2.setSpeedX(MOVESPEED);
}
if (speedX == 0){
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if(movingRight == true && movingLeft == true ){
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
rect.setRect(centerX - 47, centerY - 65, 32, 87);
centerY += speedY;
}
public void moveRight() {
speedX = MOVESPEED;
}
public void moveLeft() {
speedX = -MOVESPEED;
}
public void stopRight() {
movingRight = false;
stop();
}
public void stopLeft() {
movingLeft = false;
stop();
}
private void stop() {
if (movingRight == false && movingLeft == false) {
speedX = 0;
}
if (movingRight == false && movingLeft == true) {
moveLeft();
}
if (movingRight == true && movingLeft == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
and this is the collision code
public void checkCollision(Rectangle rect){
if (rect.intersects(r)){
if(Player.movingRight){
Player.centerX = tileX + 11;
Player.speedX =0;
}
if(Player.movingLeft){
Player.centerX = tileX + 89;
Player.speedX = 0;
}
if(Player.speedY > 0){
Player.centerY = tileY - 25;
Player.speedY = 0;
Player.jumped = false;
}
}
}
There are two problems.The first one is that if I press one of the movement keys when landing the character "teleports" to the right or left.
I know this happens because I programmed it that if the character intersects with the ground while movingRight or movingLeft are true he moves right or left.(I made it this way so the horizonal collision will work) and I cant think of any other way to do it or how to fix it.
The second problem is that if the character moves of a platfrom he does not fall down.
I tryed to fix it by adding to the collision method
else{
speedY += 1;
}
But it made the character disappear for some reason.
Thanks a lot!
This code was originally written in C++ for a 3D platformer. I rewrote it, but there might be some bugs. I can draw a picture later if it's difficult to understand.
public void checkCollision(Rectangle rect){
if(player.intersects(rect)) {
//the rectangles intersect, time to move the player out of the block
if(rect.y+rect.height >= player.y && rect.y+rect.height-0.7f < player.y) { //if the player is at most 0.7 units (you should change this!) below top side
player.y = rect.y+rect.height; //set player to stand on top
speed.y = 0f; //stop the movement
onGround = true;
} else if(rect.y+rect.height > player.y && rect.y < player.y+player.height) { //if the playeer is on the side, but not below
float xEscMinus = (float)Math.abs((rect.x+rect.width)-player.x); //find the distance to the side
float xEscPlus = (float)Math.abs(rect.x-(player.x+player.width));
if(xEscMinus<xEscPlus) {
player.x = rect.x+rect.width;
} else {
player.x = rect.x-player.width;
}
}
}
}

Categories