If statement for Game
in
Programming Questions
•
11 months ago
Trying to make a frogger game and I've gotten myself stuck. I wanna make it so the if, then statement in the last 4 lines of code says "IF the frog hits and of the cars, THEN println(1); " (eventually, i want it to show a 'game over' picture, and stop the game totally so u need to restart it, but i want to keep it simple for now!) I cant figure out how the if, then statement should be formatted, can anyone tell what I am doing wrong?
- PImage img;
- Frog frog1;
- Car[] c1;
- class Car {
- float xpos;
- int ypos;
- int sizel;
- int sizew;
- float yspeed;
- color c;
- Car(){
- xpos = 0;
- ypos = (int)random(120,480);
- sizel = (int)random(20,30);
- sizew = 15;
- yspeed = (float)random(1,3);
- c= color(random(255), random(255), random(255));
- }
- void carShape() {
- rectMode(CENTER);
- fill(c);
- rect(xpos,ypos,sizel,sizew);
- fill(0);
- rect(xpos-5, ypos-8, 8,5);
- rect(xpos+5, ypos-8, 8,5);
- rect(xpos-5, ypos+8, 8,5);
- rect(xpos+5, ypos+8, 8,5);
- }
- void moveCar () {
- xpos = xpos + yspeed;
- if (xpos > width) {
- xpos = 0;
- ypos = (int)random(120,480);
- }
- }
- }
- class Frog {
- int frogx;
- int frogy;
- Frog(){
- frogx = width/2-20;
- frogy = 527;
- }
- void drawFrog() {
- image(img,frogx,frogy);
- }
- }
- void setup() {
- size(800,600);
- img = loadImage("frog.png");
- frog1 = new Frog();
- c1 = new Car[20];
- for(int i=0; i<20; i++) {
- c1[i] = new Car();
- }
- }
- void draw() {
- background(100);
- for(int i=0; i<20; i++) {
- c1[i].carShape();
- c1[i].moveCar();
- }
- frog1.drawFrog();
- if (keyPressed == true) {
- if (keyCode == UP) {
- frog1.frogy = frog1.frogy - 1;
- }
- if (keyCode == DOWN) {
- frog1.frogy = frog1.frogy +1;
- }
- if (keyCode == LEFT) {
- frog1.frogx = frog1.frogx - 1;
- }
- if (keyCode == RIGHT) {
- frog1.frogx = frog1.frogx + 1;
- }
- }
- if ( frog1.frogx == c1.xpos && frog1.frogy == c1.ypos) {
- println("1");
- }
- }
1