Game: character won't move
in
Programming Questions
•
2 years ago
I'm starting to make a game where you're supposed to be able to click to shoot and press the arrow keys to move. What I did to have the objects be able to move without having them appear to multiply, is set the playfield again before it's created. Since the playfield consists of not just a background but images and lines, I made a function called resetback in place of only calling a new background. This is the code so far (not much), at the bottom is the character's move function which doesn't work for some reason. I tried calling that movePlayer function all over the place- doesn't seem to matter.
boolean titleScreen;
boolean instructions;
boolean gameStart;
PFont font;
PImage playerRight1;
float px;
float py;
boolean lefttrue = false;
PImage football;
float footballX;
float footballY;
PImage plane;
float planeX;
float planeY;
PImage ptero;
float pteroX;
float pteroY;
PImage head;
PImage cloud43;
PImage lightbulbdx;
float lightbulbX;
float lightbulbY;
boolean bulbOn;
void setup(){
titleScreen = true;
instructions = false;
gameStart = false;
bulbOn = false;
size(1000, 800);
font = loadFont("Dialog-24.vlw");
playerRight1 = loadImage("playerRight1.png");
plane = loadImage("plane.png");
planeX = 1000;
planeY = 0;
football = loadImage("football.png");
footballX = 1000;
footballY = 800;
ptero = loadImage("ptero.png");
pteroX = 0;
pteroY = 0;
head = loadImage("head.png");
cloud43 = loadImage("cloud43.png");
lightbulbdx = loadImage("lightbulbdx.png");
}
void draw(){
px = 500;
py = 200;
if(titleScreen){
playTitleScreen();
}
if(instructions){
playInstructions();
}
if(gameStart){
gamePlaying();
}
}
void playTitleScreen(){
background(100);
smooth();
textFont(font);
fill(0);
text("Click to start", 424, 620);
if(titleScreen && mousePressed){
titleScreen = false;
instructions = true;
}
}
void playInstructions(){
background(100, 100, 255);
smooth();
textFont(font);
fill(0);
text("[Press ENTER to start]", 360, 620);
if(instructions && keyCode == ENTER){
instructions = false;
gameStart = true;
}
}
void gamePlaying(){
resetback();
resetlightbulb();
float distractTime = second();
if(distractTime == 0){
planeAppear();
}
if(distractTime$ == 0){
footballAppear();
}
if(distractTime6 == 0){
pteroAppear();
}
if(distractTime == 0 && bulbOn == false){
lightbulbAppear();
}
//if cloud43's x and y become > too big, cloudGameover();
movePlayer();
if(mousePressed){
shoot();
}
//if(lefttrue == true){
//pushMatrix();
//scale(-1.0, 1.0);
//image(playerRight1,-playerRight1.width,0);
//popMatrix();
//}else if(!lefttrue){
// image(playerRight1, px, py);
//}
}
void resetback(){
background(100, 100, 255);
image(head, 500, 400);
float cloudTime = millis();
imageMode(CENTER);
image(cloud43, 530, 390, 20, 20);
image(cloud43, 535, 379, 26, 26);
image(cloud43, 500, 390, 30+cloudTime*0.003, 30+cloudTime*0.003);
noFill();
strokeWeight(2);
smooth();
stroke(0);
line(241, 150, 759, 150);
line(241, 650, 759, 650);
line(250, 141, 250, 659);
line(750, 141, 750, 659);
line(241, 200, 759, 200);
line(241, 600, 759, 600);
line(300, 141, 300, 659);
line(700, 141, 700, 659);
for(int i = 200; i < 600; i += 50){
line(241, i+50, 309, i+50);
line(691, i+50, 759, i+50);
}
for(int n = 200; n < 700; n+=50){
line(n+50, 141, n+50, 209);
line(n+50, 591, n+50, 659);
}
}
void planeAppear(){
resetback();
image(plane, planeX, planeY);
planeX -= 3;
planeY += 3;
}
void footballAppear(){
resetback();
image(football, footballX, footballY);
footballX -= 5;
footballY -= 5;
}
void pteroAppear(){
resetback();
image(ptero, pteroX, pteroY);
pteroX += 8;
pteroY += 8;
}
void lightbulbAppear(){
resetback();
bulbOn = true;
image(lightbulbdx, lightbulbX, lightbulbY);
}
void resetlightbulb(){
bulbOn = false;
lightbulbX = random(270, 730);
lightbulbY = random(150, 610);
//set these values to static?
//noLoop();
}
void resetplane(){
planeX = 1000;
planeY = 0;
}
void resetfootball(){
footballX = 1000;
footballY = 800;
}
void resetptero(){
pteroX = 0;
pteroY = 0;
}
void shoot(){
stroke(255, 0, 0, 100);
fill(255, 60, 60);
ellipse(mouseX, mouseY, 8, 8);
stroke(255, 0, 0, 100);
line(px, py, mouseX, mouseY);
}
void movePlayer(){ //This part doesn't work properly
if(keyPressed && (key == CODED)){
if(key == LEFT && px > 270){
px -= 3;
//lefttrue = true;
}else if(key == RIGHT && px < 730){
px += 3;
//lefttrue = false;
}else if(key == UP && py > 150){
py -= 3;
}else if(key == DOWN && py < 610){
py += 3;
}
}
boolean titleScreen;
boolean instructions;
boolean gameStart;
PFont font;
PImage playerRight1;
float px;
float py;
boolean lefttrue = false;
PImage football;
float footballX;
float footballY;
PImage plane;
float planeX;
float planeY;
PImage ptero;
float pteroX;
float pteroY;
PImage head;
PImage cloud43;
PImage lightbulbdx;
float lightbulbX;
float lightbulbY;
boolean bulbOn;
void setup(){
titleScreen = true;
instructions = false;
gameStart = false;
bulbOn = false;
size(1000, 800);
font = loadFont("Dialog-24.vlw");
playerRight1 = loadImage("playerRight1.png");
plane = loadImage("plane.png");
planeX = 1000;
planeY = 0;
football = loadImage("football.png");
footballX = 1000;
footballY = 800;
ptero = loadImage("ptero.png");
pteroX = 0;
pteroY = 0;
head = loadImage("head.png");
cloud43 = loadImage("cloud43.png");
lightbulbdx = loadImage("lightbulbdx.png");
}
void draw(){
px = 500;
py = 200;
if(titleScreen){
playTitleScreen();
}
if(instructions){
playInstructions();
}
if(gameStart){
gamePlaying();
}
}
void playTitleScreen(){
background(100);
smooth();
textFont(font);
fill(0);
text("Click to start", 424, 620);
if(titleScreen && mousePressed){
titleScreen = false;
instructions = true;
}
}
void playInstructions(){
background(100, 100, 255);
smooth();
textFont(font);
fill(0);
text("[Press ENTER to start]", 360, 620);
if(instructions && keyCode == ENTER){
instructions = false;
gameStart = true;
}
}
void gamePlaying(){
resetback();
resetlightbulb();
float distractTime = second();
if(distractTime == 0){
planeAppear();
}
if(distractTime$ == 0){
footballAppear();
}
if(distractTime6 == 0){
pteroAppear();
}
if(distractTime == 0 && bulbOn == false){
lightbulbAppear();
}
//if cloud43's x and y become > too big, cloudGameover();
movePlayer();
if(mousePressed){
shoot();
}
//if(lefttrue == true){
//pushMatrix();
//scale(-1.0, 1.0);
//image(playerRight1,-playerRight1.width,0);
//popMatrix();
//}else if(!lefttrue){
// image(playerRight1, px, py);
//}
}
void resetback(){
background(100, 100, 255);
image(head, 500, 400);
float cloudTime = millis();
imageMode(CENTER);
image(cloud43, 530, 390, 20, 20);
image(cloud43, 535, 379, 26, 26);
image(cloud43, 500, 390, 30+cloudTime*0.003, 30+cloudTime*0.003);
noFill();
strokeWeight(2);
smooth();
stroke(0);
line(241, 150, 759, 150);
line(241, 650, 759, 650);
line(250, 141, 250, 659);
line(750, 141, 750, 659);
line(241, 200, 759, 200);
line(241, 600, 759, 600);
line(300, 141, 300, 659);
line(700, 141, 700, 659);
for(int i = 200; i < 600; i += 50){
line(241, i+50, 309, i+50);
line(691, i+50, 759, i+50);
}
for(int n = 200; n < 700; n+=50){
line(n+50, 141, n+50, 209);
line(n+50, 591, n+50, 659);
}
}
void planeAppear(){
resetback();
image(plane, planeX, planeY);
planeX -= 3;
planeY += 3;
}
void footballAppear(){
resetback();
image(football, footballX, footballY);
footballX -= 5;
footballY -= 5;
}
void pteroAppear(){
resetback();
image(ptero, pteroX, pteroY);
pteroX += 8;
pteroY += 8;
}
void lightbulbAppear(){
resetback();
bulbOn = true;
image(lightbulbdx, lightbulbX, lightbulbY);
}
void resetlightbulb(){
bulbOn = false;
lightbulbX = random(270, 730);
lightbulbY = random(150, 610);
//set these values to static?
//noLoop();
}
void resetplane(){
planeX = 1000;
planeY = 0;
}
void resetfootball(){
footballX = 1000;
footballY = 800;
}
void resetptero(){
pteroX = 0;
pteroY = 0;
}
void shoot(){
stroke(255, 0, 0, 100);
fill(255, 60, 60);
ellipse(mouseX, mouseY, 8, 8);
stroke(255, 0, 0, 100);
line(px, py, mouseX, mouseY);
}
void movePlayer(){ //This part doesn't work properly
if(keyPressed && (key == CODED)){
if(key == LEFT && px > 270){
px -= 3;
//lefttrue = true;
}else if(key == RIGHT && px < 730){
px += 3;
//lefttrue = false;
}else if(key == UP && py > 150){
py -= 3;
}else if(key == DOWN && py < 610){
py += 3;
}
}
1

I definitely missed that. Awesome, thanks.