Need help with final! Simple dropping game with errors such as "class or type cannot be found"!
in
Programming Questions
•
10 months ago
Hello everyone,
I've created a dropping objects game for a final due tomorrow in which the player must capture falling objects that will either increase or lower its speed. There are two categories to the objects: "good" and "bad". If the player misses more than three "good" objects, then the game is over. If the player captures any "bad" food, it will decrease its speed, but capturing "good" food will revive it.
I finished all of my coding, but when I run it, I get one error on top of another (next time, I know to definitely work in pieces and continuously run to make sure it all works!). The one I am currently stuck on is "Cannot find a class or type named 'Player'", even though I do. I even tried renaming it, and checking in the Sketch folder if my class file is there, and it still doesn't work.
One thing I'm also skeptical about working but Processing didn't seem to have any problems with is that for every second, a food object drops at random, but each type of food has a certain probability. I executed this by setting a one second timer, so each time it is finished, food would drop.
Anyways, here is my code, and if you notice any other errors that may pop up from it, that'd be awesome if I could get some insight on it! Thank you so much for everyone's help!
//Main code//
Player me;
Timer timer;
PImage bg, intropic, gameover, player, food1, food2, food3, food4, food5, food6;
int randomFood = (int)random(1, 99.999999);
int Intro = 0;
int RunGame = 1;
int GameOver = 2;
String gameState = "Intro";
Food[] food;
int lost = 0;
void setup() {
size(360,540);
background(0);
smooth();
frameRate(60);
Food [] food = new Food [100];
bg = loadImage ("bg.png");
intropic = loadImage ("introbg.jpg");
gameover = loadImage ("gameoverbg.jpg");
player = loadImage ("squirrel.jpg");
food1 = loadImage ("basicgood1.jpg");
food2= loadImage ("basicbad2.jpg");
food3= loadImage ("verygood1.jpg");
food4= loadImage ("verybad2.jpg");
food5= loadImage ("extremelygood1.jpg");
food6= loadImage ("extremelybad2.jpg");
timer = new Timer(1000);
timer.start();
me = new Player ();
speedChange();
}
//Sets the three game states
void draw() {
if(gameState.equals("Intro"))
{
intro();
}
if (gameState.equals("RunGame")) {
rungame();
}
if (gameState.equals("GameOver")) {
gameover();
}
// Check collisions
for(int i = 0; i < food.length; i++)
{
float distance = dist(me.xpos, me.ypos, food.xpos, food.ypos);
if(distance < 25)
{
if(food.type.equals("basicGood"))
{
me.speed >= 20 = me.speed + 1;
}
if(food.type.equals("basicBad"))
{
me.speed = me.speed - 5;
}
if(food.type.equals("veryGood"))
{
me.speed >= 20 = me.speed + 2;
}
if(food.type.equals("veryBad"))
{
me.speed = me.speed - 10;
}
if(food.type.equals("extremelyGood"))
{
me.speed = me.speed + 10;
}
if(food.type.equals("extremelyBad"))
{
me.speed = me.speed - 17;
}
}
}
if(food.type.equals("basicGood").ypos > 550)
{
lost++;
}
if(food.type.equals("veryGood").ypos > 550)
{
lost++;
}
if(food.type.equals("extremelyGood").ypos > 550)
{
lost++;
}
if(lost >= 3)
{
gameover();
}
}
//Picks a random value from food array to drop and displays food
void dropfood()
{
randomFood = (int)random(1, 99.999999);
//int randomFood = random(0, 100); //this gets a random element from the food array
while(food[randomFood].getY() >= 0)
{
randomFood = random(1, 99.999999);
}
food[randomFood].display();
food[randomFood].move();
food[randomFood].setY();
}
//The introduction picture -- will notify player to click anywhere
//to begin (this is taken care of by the mousepressed function below)
void intro() {
imageMode(CORNER);
image (intropic, 0, 0);
}
//The gameplay
void rungame()
{
//Displays background
imageMode(CORNER);
image(bg, 0, 0);
//Displays player
me.display();
//Drops a piece of food every second
if (timer.isFinished())
{
// setY(add or subtract of images --> double check height/2 **make them all same height);
dropfood();
}
//Assigned values to the different types of food being dropped every second
//to ensure a certain probability of each type (35% basics, 25% verys, 5% extremes)
if(randomFood >= 1 && randomFood <= 28)
{
food[i] = new Food("basicGood");
}
if(randomFood >= 29 && randomFood <= 68)
{
food[i] = new Food("basicBad");
}
if(randomFood >= 69 && randomFood <= 80)
{
food[i] = new Food("veryGood");
}
if(randomFood >= 81 && randomFood <= 92)
{
food[i] = new Food("veryBad");
}
if(randomFood >= 93 && randomFood <= 96)
{
food[i] = new Food("extremelyGood");
}
if(randomFood >= 97 && randomFood <= 99)
{
food[i] = new Food("extremelyBad");
}
}
void gameover() {
imageMode(CORNER);
image(gameover, 0, 0);
//because of the mousepressed function, if the player clicks
//anywhere, then the game will restart
}
//Starts and/or resets game
void mousePressed() {
if (gameState == GameOver || gameState == Intro) {
gameState = RunGame; // start the game
}
}
//Player Controls: Left and right only
void keyPressed ()
{
if(key==CODED)
{
if(keyCode == RIGHT)
{
me.xPos = me.xPos + me.speed;
}
if (keyCode == LEFT)
{
me.xPos = me.xPos - me.speed;
}
}
}
//Player class//
class Player
{
float xPos;
float yPos;
float speed;
Player ()
{
xPos= 180;
yPos = 500;
speed = 20;
}
//Displays the player
void display ()
{
imageMode(CENTER);
image (player, xPos, yPos);
}
void speedChange(){
//The lowest reduction in speed that a player can reach
if(speed < 3)
{
speed = 3;
}
}
//Food class//
class Food
{
// Food properties
float xPos;
float yPos;
float speed;
String type;
//Constructor
Food (String t)
{
type = t;
xPos = random(0, width);
yPos = -200; //starts off screen
speed = random(1, 5);
}
void setY(int passedY)
{
yPos = passedY;
}
int getY()
{
return yPos;
}
void move()
{
yPos = yPos + speed;
if(yPos + 54 > height)
{
yPos == -200;
}
}
//Displays the images of each different types of food
void display ()
{
imageMode(CENTER);
if(type.equals("basicGood"))
{
image(food1, random(0, width), -200);
}
if(type.equals("basicBad"))
{
image(food2, random(0, width), -200);
}
if(type.equals("veryGood"))
{
image(food3, random(0, width), -200);
}
if(type.equals("veryBad"))
{
image(food4, random(0, width), -200);
}
if(type.equals("extremelyGood"))
{
image(food5, random(0, width), -200);
}
if(type.equals("extremelyBad"))
{
image(food6, random(0, width), -200);
}
}
//Timer class//
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (totalTime%passedTime == 0) {
return true;
} else {
return false;
}
}
}
}
}
1