Trying to display a character on top of my maze
in
Programming Questions
•
6 months ago
I'm creating a maze game for a project at my university. It's a maze game and I want the character to detect if a color near them is black and if they are moving in that direction they will stop moving in that direction. Basically I'm trying to do collision detection with color. When I try to display my Player character it just displays a grayish screen. I'm not sure exactly why my code isn't displaying the character. I have a file "hero.png" in the same folder as my .pde. Any suggestions?
///////////////////////////////Code below//////////////////////////////////////////
//Code by D. Witt.
PImage heroImg;
Player hero;
Enemy baddie;
void setup()
{
size(1000, 760);
background(250,250,250);
heroImg = loadImage("hero.png");
hero = new Player(30, 730);
}
void draw()
{
background(250,250,250);
//float distance = dist(baddie.xpos, baddie.ypos, hero.xpos, hero.ypos);
//hero.display();
drawMaze();
}
void mouseClicked()
{
}
void drawMaze(){
fill(0);
rect(-10, -10, 12, 780);
rect(-10, -10, 1020, 12);
rect(996, -10, 12, 1020);
rect(0, 756, 1020, 12);
rect(0, 700, 100, 4);
rect(160, 700, 600, 4);
rect(840, 700, 100, 4);
rect(940, 504, 4, 200);
rect(940, 504, 60, 4);
rect(940, 0, 4, 440);
rect(360, 50, 4, 490);
rect(760, 624, 4, 80);
rect(840, 510, 4, 190);
rect(100, 514, 4, 190);
rect(50, 464, 4, 240);
rect(160, 650, 4, 50);
rect(160, 646, 140, 4);
rect(160, 600, 4, 50);
rect(360, 646, 340, 4);
rect(160, 595, 140, 4);
rect(360, 596, 4, 50);
rect(160, 464, 4, 80);
rect(220, 464, 4, 80);
rect(280, 464, 4, 80);
rect(300, 544, 4, 55);
rect(160, 540, 60, 4);
rect(280, 540, 24, 4);
rect(100, 460, 64, 4);
rect(50, 224, 4, 185);
rect(100, 54, 4, 405);
rect(50, 220, 50, 4);
rect(50, 0, 4, 165);
rect(100, 50, 200, 4);
rect(300, 110, 4, 300);
rect(160, 110, 4, 290);
rect(160, 400, 60, 4);
rect(220, 400, 4, 64);
rect(360, 50, 200, 4);
rect(630, 50, 310, 4);
rect(420, 220, 4, 374);
rect(240, 110, 4, 240);
rect(240, 350, 60, 4);
rect(480, 160, 4, 440);
rect(420, 100, 4, 60);
rect(420, 160, 60, 4);
rect(360, 594, 64, 4);
rect(420, 100, 260, 4);
rect(740, 100, 200, 4);
rect(544, 160, 336, 4);
rect(680, 100, 4, 60);
rect(544, 440, 350, 4);
rect(890, 440, 4, 200);
rect(890, 504, 50, 4);
rect(620, 510, 224, 4);
rect(540, 440, 4, 160);
rect(760, 570, 4, 100);
rect(620, 570, 80, 4);
rect(700, 570, 4, 80);
rect(420, 380, 224, 4);
rect(700, 300, 4, 140);
rect(540, 320, 100, 4);
rect(540, 220, 4, 100);
rect(540, 220, 220, 4);
rect(600, 270, 104, 4);
rect(700, 270, 4, 100);
rect(760, 220, 4, 164);
rect(760, 380, 130, 4);
rect(890, 380, 4, 60);
rect(880, 160, 4, 60);
rect(820, 160, 4, 160);
rect(890, 280, 4, 120);
rect(880, 220, 60, 4);
}
void keyPressed()
{
if (key == 'w')
{
hero.moveUp();
}
if (key == 's')
{
hero.moveDown();
}
if (key == 'a')
{
hero.moveLeft();
}
if (key == 'd')
{
hero.moveRight();
}
}
class Player{
float xpos;
float ypos;
Player(float tempX, float tempY){
xpos = tempX;
ypos = tempY;
}
void moveLeft(){
color leftColor = get((int(xpos) - 10), int(ypos));
if(leftColor != color(0, 0, 0)){
xpos--;
}
}
void moveRight(){
color rightColor = get((int(xpos) + 10), int(ypos));
if(rightColor != color(0, 0, 0)){
xpos++;
}
}
void moveUp(){
color upColor = get(int(xpos), (int(ypos) - 10));
if(upColor != color(0, 0, 0)){
ypos--;
}
}
void moveDown(){
color downColor = get(int(xpos), (int(ypos) + 10));
if(downColor != color(0, 0, 0)){
ypos++;
}
}
void display(){
image(heroImg, xpos, ypos);
}
}
class Treasure{
float xpos;
float ypos;
void pickup(){
}
}
class Enemy{
float xpos;
float ypos;
void walkCycle(){
}
void killPlayer(float tempDist){
float distanceFromPlayer = tempDist;
if(distanceFromPlayer < 25){
setup();
}
}
}
1