Help with a little game (collisions)
in
Programming Questions
•
1 year ago
Hi everyone....I'm trying to make a really simple game but I'm having some problem with the collisions....any idea why? I believe the coordinates are correct but still no luck......the code is quite long so case anyone is willing to take a look, the entire folder sketch can be downloaded in:
http://jupiter.esec.pt/~apgomes/whali.rar
http://jupiter.esec.pt/~apgomes/whali.rar
Thank you so much in advance
- import processing.core.*;
- import processing.xml.*;
- import java.applet.*;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.event.MouseEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.FocusEvent;
- import java.awt.Image;
- import java.io.*;
- import java.net.*;
- import java.text.*;
- import java.util.*;
- import java.util.zip.*;
- import java.util.regex.*;
- public class whali extends PApplet {
- // vars
- int px,py,ly,posz,posa,v,vdist,car = 0;
- int posx = 510; // whale x-axis
- int posy = 550; // posicao eixo y do carro
- int vel = 10; // framerate
- int velc = 10; // speedometer
- boolean start = false;
- boolean ultrapassou, ylinha;
- // instructions
- String textoO = "Avoid the boats!";
- String textoI = "Instruction:";
- String texto1 = "1)Start 'S'";
- String texto2 = "2)Left / Right - move";
- String texto3 = "3)Up / Down - increase / decrease speed";
- String texto6 = "Game Over";
- String texto7 = "Boats avoided: ";
- String texto8 = "Score: ";
- String texto9 = "Restart 'R'";
- int posPista = 0; // Posicao da pista
- int posPista2 = -768; // Posicao da pista 2
- PImage pista, pista2, jogo, baleia, boat1,boat2,boat3;
- public void setup()
- {
- size(1024, 768);
- smooth();
- noCursor();
- pista = loadImage("data/oceantop2.png");
- pista2 = loadImage("data/oceantop1.png");
- jogo = loadImage("data/cave.png");
- baleia = loadImage("data/baleia.png");
- boat1 = loadImage("data/boat1.png");
- boat2 = loadImage("data/boat2.png");
- boat3 = loadImage("data/boat3.png");
- }
- public void draw()
- {
- if (!start) {
- background(180);
- image(jogo, 0,0);
- textAlign(LEFT, TOP);
- fill(color(50, 10, 10));
- textSize(20);
- text(textoO, 10, 20, width, height/2);
- fill(color(250, 100, 100));
- textSize(30);
- text(textoI, 10, 80, width, height/2);
- fill(color(250, 250, 150));
- textSize(20);
- text(texto1, 10, 120, width, height/2);
- text(texto2, 10, 140, width, height/2);
- text(texto3, 10, 160, width, height/2);
- }
- else {
- background(0);
- // increase speed
- if (v > 99) {
- velc += 10;
- vel += 5;
- vdist += v;
- v = 0;
- ly = 0;
- ylinha = true;
- }
- frameRate(vel); // define speed
- // boat track
- if (posa == 0) {
- posz = PApplet.parseInt(random(1024));
- ultrapassou = false;
- }
- // track movement
- posPista += 35;
- posPista2 += 35;
- // trackscrolling
- if(posPista >= 768) posPista = -768;
- if(posPista2 >= 768) posPista2 = -768;
- // draw tracks
- image(pista, 0, posPista);
- image(pista2, 0, posPista2);
- boats(posz, posa);
- baleia(posx, posy , true );
- posa += 30; // boats movement
- v ++;
- // collision
- avoidboat(posx, posy, posz, posa);
- // counts as passed
- if (posa > 768) {
- posa = 0;
- car += 1;
- }
- }
- }
- public void keyPressed() {
- if (keyCode == 83) { start = true; } // start (s)
- if ((keyCode == 82) && ( start == false)) { // restart (r)
- start = true;
- posx = 295;
- posy = 520;
- posa = 0;
- vel = 10;
- velc = 10;
- v = 0;
- vdist = 0;
- car = 0;
- ylinha = false;
- loop();
- }
- // arrow movements
- if (keyCode == 37) { // right
- posx -= 340;
- if (posx < 165) { posx = 165; }
- }
- else if (keyCode == 39) { // left
- posx += 340;
- if (posx > 885) { posx = 885; }
- }
- else if (keyCode == 38) { // up
- posy -= 50;
- vel += 5;
- velc += 10;
- v += 5;
- if (v > 99) { v = 100; }
- if (posy < 85) { posy = 85; }
- }
- else if (keyCode == 40) { // down
- posy += 50;
- vel -= 5;
- velc -= 10;
- v -= 5;
- if (vel <= 0 ) { vel = 5; }
- if (velc <=0 ) { velc = 10; }
- if (posy > 944) { posy = 944; }
- }
- }
- public void boats(int x, int y) {
- // position centered on track
- if (x >= 0 && x <= 340) { x = 165; }
- else if (x > 340 && x <= 720) { x = 505; }
- else if (x > 720 ) { x = 845; }
- image(boat1,x,y);
- }
- public void baleia(int x, int y, boolean yvel) {
- // center whale in track
- if (x >= 0 && x <= 340) { x = 165; }
- else if (x > 340 && x <= 720) { x = 505; }
- else if (x > 720 ) { x = 845; }
- image(baleia,x,y);
- if (yvel) { // show speed
- fill(color(0, 0, 0));
- textSize(10);
- text(str(velc)+"nmi", x, y, 45, 45);
- }
- }
- public void avoidboat(int xc1, int yc1, int xc2, int yc2) {
- if (xc2 >= 0 && xc2 <= 340) { xc2 = 165; }
- else if (xc2 > 340 && xc2 <= 720) { xc2 = 505; }
- else if (xc2 > 720 ) { xc2 = 845; }
- // check avoidance of the boat
- if ((yc1<=yc2) && (xc1!=xc2)) { ultrapassou = true; }
- // final messages
- if (yc2>=yc1) {
- if ((xc1==xc2) && (!ultrapassou)) {
- background(180);
- image(jogo,0,0);
- textAlign(CENTER, CENTER);
- fill(color(250, 100, 50));
- textSize(40);
- text(texto6, -350, -120, width, height/2);
- fill(color(50, 100, 250));
- textSize(30);
- text(texto7+str(car) , -350, -65, width, height/2);
- text(texto8+str((vdist+v)*car), -350, 0, width, height/2);
- fill(color(0, 0, 0));
- textSize(35);
- text(texto9 , 300, -20, width, height/2);
- start = false;
- noLoop();
- }
- }
- }
- static public void main(String args[]) {
- PApplet.main(new String[] { "--bgcolor=#F0F0F0", "whale race" });
- }
- }
1