I am working on creating a random dungeon generator for a game I am going to make. I just barely did the basics. In order to avoid having to make 20 different variables for the 20 different rectangles I drew, I used two arrays called rx and ry. However, when I went to draw the rectangles using:
rect(rx[19], rx[19], 50, 50)
It didnt put the last rectangle in the correct spot. Rather than putting it at the value that is determined by my code, it always puts rectangle 19 at 0, 0. I am not entirely sure that it is in fact rectangle 19, but I'm assuming it is. I made my two arrays using:
int[] rx = new int[20];
and
int[] ry = new int[20];
Anything helps. Here is the rest of my code just in case it will be useful, however, if you would like me to only give the important bits, tell me. Thanks!
int gx = 400;
int gy = 400;
int gDir;
int gCheckX;
int gCheckY;
int[] rx = new int[20];
int[] ry = new int[20];
int i = 0;
void setup() {
size(700, 700);
}
void draw() {
fill(0);
rect(0, 0, width, height);
fill(#696969);
if(i < 19) {
i++;
gDir = int(random(1, 5));
if(gDir == 1) {
gCheckX = 25;
gCheckY = -25;
}
if(gDir == 2) {
gCheckX = 25;
gCheckY = 75;
}
if(gDir == 3) {
gCheckX = -25;
gCheckY = 25;
}
if(gDir == 4) {
gCheckX = 75;
gCheckY = 25;
}
while(get(gCheckX, gCheckY) == color(105, 105, 105)) {
gDir = int(random(1, 5));
if(gDir == 1) {
gCheckX = 25;
gCheckY = -25;
}
if(gDir == 2) {
gCheckX = 25;
gCheckY = 75;
}
if(gDir == 3) {
gCheckX = -25;
gCheckY = 25;
}
if(gDir == 4) {
gCheckX = 75;
gCheckY = 25;
}
}
if(gDir == 1) {gy -= 50;}
if(gDir == 2) {gy += 50;}
if(gDir == 3) {gx -= 50;}
if(gDir == 4) {gx += 50;}
rx[i] = gx;
ry[i] = gy;
println(rx[i] + " " + ry[i]);
}
else {
rect(rx[0], ry[0], 50, 50);
rect(rx[1], ry[1], 50, 50);
rect(rx[2], ry[2], 50, 50);
rect(rx[3], ry[3], 50, 50);
rect(rx[4], ry[4], 50, 50);
rect(rx[5], ry[5], 50, 50);
rect(rx[6], ry[6], 50, 50);
rect(rx[7], ry[7], 50, 50);
rect(rx[8], ry[8], 50, 50);
rect(rx[9], ry[9], 50, 50);
rect(rx[10], ry[10], 50, 50);
rect(rx[11], ry[11], 50, 50);
rect(rx[12], ry[12], 50, 50);
rect(rx[13], ry[13], 50, 50);
rect(rx[14], ry[14], 50, 50);
rect(rx[15], ry[15], 50, 50);
rect(rx[16], ry[16], 50, 50);
rect(rx[17], ry[17], 50, 50);
rect(rx[18], ry[18], 50, 50);
rect(rx[19], ry[19], 50, 50);
}
}
Got it answered by #Jeremy Kahan. I just had to set i to -1 and it fixed itself!
Since this was my answer, I'm writing it as an answer. The key was to initialize int i = -1; rather than int i = 0;
It works because since the first thing you do inside your if block is i++, you were working with 1, 2, 3, ..., 18, and 19, but never with index 0. Starting at -1 fixed that so you worked with all twenty entries (whose indices are 0 to 19).
Related
How do I create a random car barrier for my game? I have this top-down car game that I need to make. This game aims to make the car move up and down (on the y-axis) to avoid the barriers, while the barriers are moving toward the car (on the x-axis). Is it possible to create a random generation of the barriers when they spawn, making the game enjoyable? Right now, I only have a set of barriers that last for 12 seconds before I get easily through them. Can I also keep the spacing of the barriers the same? So the car can fit.
code
color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);
float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 700;
float lanes;
void lane1(float x, float y){
fill(white);
rect(x + 100, y,30,100);
rect(x + 250, y + 100,30,100);
rect(x + 250, y + 200, 30, 100);
rect(x + 400, y + 100, 30, 100);
rect(x + 550, y + 100, 30, 100);
rect(x + 550, y, 30, 100);
rect(x + 700, y + 100, 30, 100);
rect(x + 700, y + 200, 30, 100);
}
void background(){
background(green);
}
void car(){
fill(red);
rect(carX, carY, 60, 40);
}
void setup(){
surface.setTitle("dodge");
size(900, 500);
}
void draw(){
background();
noStroke();
fill(grey);
rect(roadx - 400, road1y - 30, 1500, 100);
stroke(yellow);
strokeWeight(5);
rect(roadx - 400, road2y - 30, 1500, 100);
noStroke();
rect(roadx - 400, road3y - 27, 1500, 100);
car();
lane1(laneX, 100);
laneX -= 1;
lanes = 1;
}
void keyPressed(KeyEvent event){
if (key == 'w'){
if(carY != 130){
carY = carY - 100;
}
}
if (key == 's'){
if(carY != 330){
carY = carY + 100;
}
}
}
I've modified your lane rendering and added dynamic creation of barriers. Every 150 pixels moved, the barriers are updated (leftmost barriers are removed, new ones are added at the rightmost position and the cycle starts over).
The barriers are stored as an integer between 0 and 6, as there are 7 possibilities for the barrier positions:
int | 0 1 2 3 4 5 6
------+--------------------
lane0 | # # #
lane1 | # # #
lane2 | # # #
Hope this helps.
Modified code:
import java.util.*;
private static final int SCREEN_X = 900;
private static final int SCREEN_Y = 500;
private static final int LANE_HEIGHT = 100;
private static final int BARRIER_DISTANCE = 150;
private static final int BARRIER_HEIGHT = 100;
private static final int BARRIER_WIDTH = 30;
color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);
float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 50;
float lanes;
List<Integer> barriers;
void lane1(float x){
fill(white);
for (Integer b : barriers)
{
switch(b)
{
case 0: // ___
break;
case 1: // X__
rect(x, LANE_HEIGHT, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
case 2: // _X_
rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
case 3: // __X
rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
case 4: // _XX
rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
case 5: // XX_
rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
case 6: // X_X
rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
break;
}
x += BARRIER_DISTANCE;
}
}
void background(){
background(green);
}
void car(){
fill(red);
rect(carX, carY, 60, 40);
}
void setup(){
surface.setTitle("dodge");
size(900, 500);
// Initially generate 7 barriers (one more than the screen can fit)
barriers = new ArrayList<Integer>();
for (int i = 0; i <= (SCREEN_X / BARRIER_DISTANCE); i++)
{
barriers.add(int(random(0, 6)));
}
// Set the first to barriers to 'no barrier' to the player initially has some time
barriers.set(0, 0);
barriers.set(1, 0);
}
void draw(){
background();
noStroke();
fill(grey);
rect(roadx - 400, road1y - 30, 1500, LANE_HEIGHT);
stroke(yellow);
strokeWeight(5);
rect(roadx - 400, road2y - 30, 1500, LANE_HEIGHT);
noStroke();
rect(roadx - 400, road3y - 27, 1500, LANE_HEIGHT);
car();
lane1(laneX);
laneX -= 1;
// laneX cycles from 50 to -100 (because of draw position)
// So, every 150 pixels moved
if (laneX == -100)
{
// Reset laneX
laneX = 50;
// Remove leftmost barrier
barriers.remove(0);
// Add new barriers, incoming from the right
barriers.add(int(random(0, 6)));
}
lanes = 1;
}
void keyPressed(KeyEvent event){
if (key == 'w'){
if(carY != 130){
carY = carY - 100;
}
}
if (key == 's'){
if(carY != 330){
carY = carY + 100;
}
}
}
So i've used a rect to divide the screen for two different background colours. The code im writing is for a minigame and its supposed to move a bubble up the screen, but when I click my mouse the rect I used to divide the screen moves as well. I probably did a very poor job at describing this so heres the code and you'll see what I mean.
PFont font1;
int dice = 100;
int num = 0;
float circlex = 300;
float circley = 830;
float xmove = 0;
float ymove = 0;
void setup ()
{
noLoop();
frameRate(10);
size (600, 900);
//background (#29C4FF);
//fill (#C4FFEC);
//strokeWeight(3);
//line(1, 225, 599, 225);
//noStroke ();
//rect (0, 0, 599, 225);
font1 = loadFont ("ArialMT-18.vlw");
ellipseMode(CENTER);
}
void draw ()
{
//OCEAN
background (#29C4FF);
fill (#C4FFEC);
strokeWeight(3);
line(1, 225, 599, 225);
noStroke ();
rect (0, 0, 599, 225);
textFont(font1, 18);
fill(0);
text("Click on the dice to help free Aang from the iceberg.", 100, 50);
//BUBBLE
fill(0, 0, 200);
ellipse(circlex, circley, 125, 125);
noStroke();
fill (210);
ellipse(circlex, circley, 118, 118);
//AANG
//BODY
fill(#FF8E03);
noStroke ();
triangle(255, 830, 345, 830, 300, 890);
//HEAD
fill(#027A9D);
ellipse(275, 820, 10, 15);
ellipse(325, 820, 10, 15);
ellipse(300, 820, 50, 55);
rectMode(CENTER);
fill(255);
rect(300, 800, 10, 15);
triangle(290, 805, 310, 805, 300, 820);
rect(288, 815, 8, 3);
rect(312, 815, 8, 3);
//DICE
fill(#027A9D);
rect(80, 130, 100, 100, 12);
fill(#8EC1EA);
rect(80, 130, 90, 90, 8);
//NUMBERS(DOTS)
fill(150, 0, 0);
int num = int(random(1, 7));
if (num == 1 || num == 3 || num == 5)
ellipse(80, 130, dice/5, dice/5);
if (num == 2 || num == 3 || num == 4 || num == 5 || num == 6) {
ellipse(80 - dice/4, 130 - dice/4, dice/5, dice/5);
ellipse(80 + dice/4, 130 + dice/4, dice/5, dice/5);
}
if (num == 4 || num == 5 || num == 6) {
ellipse(80 - dice/4, 130 + dice/4, dice/5, dice/5);
ellipse(80 + dice/4, 130 - dice/4, dice/5, dice/5);
}
if (num == 6) {
ellipse(80, 130 - dice/4, dice/5, dice/5);
ellipse(80, 130 + dice/4, dice/5, dice/5);
}
if (num == 1 || num == 2) {
circlex = circlex + xmove;
xmove = +20;
}
if (num == 3 || num == 4) {
circlex = circlex + xmove;
xmove = -20;
}
if (num == 5) {
circley = circley + ymove;
ymove = -25;
}
if (num == 6) {
circley = circley + ymove;
ymove = -50;
}
//ROLL
if (mousePressed && mouseButton == LEFT)
noLoop();
}
void mousePressed() {
loop();
}
rectMode(CENTER);
This line modifies how your rectangles are drawn. On the first run its still set to default (CORNER), but afterwards the rect get drawn from the center and thus moves to the top left.
Reference:
https://processing.org/reference/rectMode_.html
Solution 1:
Add rectMode(CORNER) before drawing the background.
Solution 2:
Move rectMode(CENTER) to the setup and draw all shapes a bit different.
In general I suggest you put the background into a function for a better readability and flexibility.
void setup () {
noLoop();
frameRate(10);
size (600, 900);
ellipseMode(CENTER);
rectMode(CENTER); // added rectMode here.
}
void draw () {
drawStage();
//BUBBLE
// ...
}
// Function to draw the background
void drawStage() {
//OCEAN
background (#29C4FF);
fill (#C4FFEC);
noStroke();
rect(width/2, 125, width, 250); // rect drawn with "width" scales if you choose to adjust your sketch size.
fill(0);
text("Click on the dice to help free Aang from the iceberg.", 100, 50);
}
making a circle with 3 random points with small elipses on them and making a triangle with those small elipses/points
i get a line or a broken triangle, i found out that the coords on the triangle has to have 1 number that is different like triangle(500, 500, 200 ,200 ,100 , 50);
but i cannot find a something that fixes that
int num = 3;
float[] numbers = new float[6];
int count = 0;
void setup(){
size(880,880);
translate(width/2,height/2);
ellipse(0, 0, 512, 512);
fill(256,100,0);
ellipse(0, 0, 5, 5);
}
void draw(){
float r = random(0, 256);
float s = random(0, 256);
fill(0,100,256);
translate(width/2,height/2);
if (count < 3)
{
ellipse(256*cos(TWO_PI/float(1) + r ),256*sin(TWO_PI/float(1) + r),10,10);
stroke(100,256,0);
numbers[count] = r;
count++;
numbers[count] = s;
}
else if (count == num)
{
beginShape();
vertex(256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[0]));
vertex(256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[1]));
vertex(256*cos(TWO_PI/float(1) + numbers[4]),256*cos(TWO_PI/float(1) + numbers[5]));
endShape(CLOSE);
//triangle (256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[2]),256*cos(TWO_PI/float(1) + numbers[2]));
count++;
}
}
void keyPressed() {
count = 0;
}
Calculate the points by points of the triangle by an random angle:
float angle = random(0, 360);
float x = 256*cos(angle);
float y = 256*sin(angle);
Store the points of the triangle to an array:
numbers[count*2] = x;
numbers[count*2+1] = y;
count++;
Now a triangle can by drawn with ease:
triangle(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5] );
Full draw function:
void draw(){
if (count == 0 )
{
for (int i=0; i < 3; ++i ) {
float angle = random(0, 360);
numbers[i*2] = 256*cos(angle);
numbers[i*2+1] = 256*sin(angle);
}
count = 3;
}
translate(width/2,height/2);
background(160);
fill(255);
ellipse(0, 0, 512, 512);
fill(255,100,0);
ellipse(0, 0, 5, 5);
fill(0,100,255);
ellipse(numbers[0], numbers[1], 10, 10);
ellipse(numbers[2], numbers[3], 10, 10);
ellipse(numbers[4], numbers[5], 10, 10);
stroke(100,256,0);
triangle(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5] );
}
Demo
private JPanel contentPane;
private KeyListener myKeyListener;
JLabel lblUp;
JLabel lblMiddle;
JLabel lblDown;
JButton btnStart;
JLabel lblScore;
int lblu = 0;
int x = 0;
int y = 50;
int u = 1;
int w = 1;
int rxx = 0;
int ryy = 0;
int s = 0;
Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game_1 frame = new Game_1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Game_1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblScore = new JLabel("0");
lblScore.setHorizontalAlignment(SwingConstants.CENTER);
lblScore.setForeground(Color.GREEN);
lblScore.setBounds(388, 0, 46, 14);
contentPane.add(lblScore);
addKeyListener(this);
}
public void keyPressed(KeyEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
if(y < 0){
y = maxh -50;
}
if(y > maxh-45){
y = 0;
}
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
y = y - 10;
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if(u ==1){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
Timer timer = new Timer(100, this);
timer.start();
u = 0;
}else if(u ==0){
x = x +10;
}
} else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
y = y+ 10;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
x = x- 10;
if (x < 0) {
pen.fillRect(x - 30, y, 50, 50);
x = maxw;
}
}
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void run() {
}
#Override
public void actionPerformed(ActionEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
if(w ==1){
Random r = new Random();
int ry = r.nextInt(maxh - 0) + 100;
int rx = r.nextInt(maxw - 0) + 100;
rxx = rx;
ryy = ry;
pen.setColor(Color.RED);
pen.fillRect(rx, ry, 10, 10);
w = 0;
}
pen.setColor(Color.RED);
pen.fillRect(rxx, ryy, 10, 10);
if(x-50 <= rxx && x > rxx && y > ryy && y-50 <= ryy){
s ++;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(rxx, ryy, 10, 10);
w = 1;
}
}
}
here is the Problem: it only detects that they touch in the left top >corner(the wrong code is at the end, the last if)
you can start the game by pressing the Right arrow Button on you're Keyboard.Moving upwards: up Key on you're Keyboard.Moving downwards:down Key on your're Keyboard.The right Button also lets you move to the right, the left Button to the left.Picture of the Game
I want, that the Rect detects that it touches the other in every part
of it, not only in the left top corner
Let's assume you've got first rect with: x1, y1, width1, height1 and second rect with x2, y2, width2, height2. The first rect touched the second in every part of it (so the second one is contained in the first one) when x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height
so
if (x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height) {
// the second rect is contained in the first rect
}
So I finally fixed it. For those who are interested, here is the fixed part of the code:
if (x+d >= x2 && x <= x2 && y+d >=y2 && y <= y2) {
s++;
d = d + s*10;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(x2, y2, 10, 10);
w = 1;
if (s == 10) {
JOptionPane.showMessageDialog(frame, "Achievement get: " + s + "Punkte!");
}
I'm getting this error: The nested type cannot hide an enclosing type. I have looked it up, and other people seem to be declaring their class twice, which I am not.
What's weird is if I copy and paste the code into a new processing document, it works with no error. But as I'm converting it to js i need it to work with no errors after saving and opening again.
MotorBike Bike1, Bike2, Bike3, Bike4, Bike5, Bike6, Bike7, Bike8, Bike9;
int Score_Bike1 = 0;
int Score_Bike2 = 0;
int Score_Bike3 = 0;
int Score_Bike4 = 0;
int Score_Bike5 = 0;
int Score_Bike6 = 0;
int Score_Bike7 = 0;
int Score_Bike8 = 0;
int Score_Bike9 = 0;
String Score_Spacing = " ";
int GameState = 0;
class MotorBike {
float Pos_X;
int Pos_Y;
float Speed;
int Size = 30;
float WheelSize = Size / 3;
color Color;
MotorBike(int Declare_X, int Declare_Y, int Declare_Speed, color Declare_Color)
{
this.Pos_X = Declare_X;
this.Pos_Y = Declare_Y;
Speed = Declare_Speed;
Color = Declare_Color;
}
void move()
{
if (GameState == 1) {
Speed = (random(0, 50) / 10);
Pos_X = Pos_X + Speed;
}
}
void render()
{
fill(Color);
triangle(Pos_X, Pos_Y, Pos_X + Size, Pos_Y, Pos_X + Size / 2, Pos_Y -Size / 2);
fill(255);
strokeWeight(1.5);
ellipse(Pos_X, Pos_Y, WheelSize, WheelSize);
ellipse(Pos_X + Size, Pos_Y, WheelSize, WheelSize);
}
}
void setup()
{
size(700, 600);
background(200);
SpawnBikes();
}
void draw()
{
background(200);
strokeWeight(3);
line(50, 10, 50, 590);
line(650, 10, 650, 590);
strokeWeight(1);
MoveBikes();
DetectWinner();
DisplayScore();
}
void MoveBikes()
{
Bike1.render();
Bike1.move();
Bike2.render();
Bike2.move();
Bike3.render();
Bike3.move();
Bike4.render();
Bike4.move();
Bike5.render();
Bike5.move();
Bike6.render();
Bike6.move();
Bike7.render();
Bike7.move();
Bike8.render();
Bike8.move();
Bike9.render();
Bike9.move();
}
void DetectWinner()
{
textSize(15);
fill(0);
if (Bike1.Pos_X >= 620) {
noLoop();
text("Bike 1 Wins", 310, 10, 350, 50);
Score_Bike1 += 1;
GameState = 2;
}
if (Bike2.Pos_X >= 620) {
noLoop();
text("Bike 2 Wins", 310, 10, 350, 50);
Score_Bike2 += 1;
GameState = 2;
}
if (Bike3.Pos_X >= 620) {
noLoop();
text("Bike 3 Wins", 310, 10, 350, 50);
Score_Bike3 += 1;
GameState = 2;
}
if (Bike4.Pos_X >= 620) {
noLoop();
text("Bike 4 Wins", 310, 10, 350, 50);
Score_Bike4 += 1;
GameState = 2;
}
if (Bike5.Pos_X >= 620) {
noLoop();
text("Bike 5 Wins", 310, 10, 350, 50);
Score_Bike5 += 1;
GameState = 2;
}
if (Bike6.Pos_X >= 620) {
noLoop();
text("Bike 6 Wins", 310, 10, 350, 50);
Score_Bike6 += 1;
GameState = 2;
}
if (Bike7.Pos_X >= 620) {
noLoop();
text("Bike 7 Wins", 310, 10, 350, 50);
Score_Bike7 += 1;
GameState = 2;
}
if (Bike8.Pos_X >= 620) {
noLoop();
text("Bike 8 Wins", 310, 10, 350, 50);
Score_Bike8 += 1;
GameState = 2;
}
if (Bike9.Pos_X >= 620) {
noLoop();
text("Bike 9 Wins", 310, 10, 350, 50);
Score_Bike9 += 1;
GameState = 2;
}
}
void DisplayScore()
{
textSize(15);
fill(0);
text("Bike 1: " + Score_Bike1 + Score_Spacing + "Bike 2: " + Score_Bike2 + Score_Spacing + "Bike 3: " +
Score_Bike3 + Score_Spacing + "Bike 4: " + Score_Bike4 + Score_Spacing + "Bike 5: " + Score_Bike5 + Score_Spacing +
"Bike 6: " + Score_Bike6 + Score_Spacing + "Bike 7: " + Score_Bike7 + Score_Spacing + "Bike 8: " + Score_Bike8 +
Score_Spacing + "Bike 9: " + Score_Bike9, 65, 530, 635, 700);
}
void keyPressed()
{
if (keyPressed) {
if (key == ' ')
{
if (GameState == 0) {
GameState = 1;
}
if (GameState == 2) {
loop();
background(200);
SpawnBikes();
GameState = 0;
}
}
}
}
void SpawnBikes()
{
Bike1 = new MotorBike(50, 100, 2, color(255, 0, 0));
Bike2 = new MotorBike(50, 150, 2, color(0, 255, 0));
Bike3 = new MotorBike(50, 200, 2, color(0, 0, 255));
Bike4 = new MotorBike(50, 250, 2, color(255, 255, 0));
Bike5 = new MotorBike(50, 300, 2, color(0, 255, 255));
Bike6 = new MotorBike(50, 350, 2, color(255, 0, 255));
Bike7 = new MotorBike(50, 400, 2, color(100, 255, 0));
Bike8 = new MotorBike(50, 450, 2, color(0, 100, 255));
Bike9 = new MotorBike(50, 500, 2, color(255, 0, 100));
}
Your problem is caused by the fact that you're naming your sketch the same thing as a class you're using inside your sketch. Your sketch can't be named MotorBike if you have a MotorBike class inside that sketch.
Either rename your sketch, or rename your class.
Behind the scenes, this is because Processing exports your sketch as a Java class, and any classes in your sketch become inner classes of that Java class. So your sketch becomes something like this:
class MotorBike{
void draw(){
//whatever
}
class MotorBike{
int x;
//whatever
}
}
This is illegal Java, which is what's causing your error. You can't have an inner class with the same name as a parent class. In other words, a nested type cannot hide an enclosing type.
This is also why it works okay when you copy it into a new sketch- Processing gives your sketch a random default name, so you don't have this name collision until you save your sketch as something else.