My assignment is to create a program that allows the user to drive a tank around the screen. It needs to have walls on at the edges of the screen and 2 boundaries that are each larger than the tank. When a tank is moved into a boundary that move will be undone, but the tank will keep the direction of the pressed key.
- Required Method: boolean hitBoundary()
- Returns true when the tank intersects a wall or boundary and false otherwise.
I can’t figure out how to create the boundaries.
PImage right,up,down,left;
int x=30;
int y=30;
int LEFT = 0;
int RIGHT = 1;
int DOWN = 2;
int UP = 3;
int dir;
int speedx=2;
int speedy=2;
boolean tankright,tankup,tankleft,tankdown;
void setup()
{
size(400,400);
rectMode(CORNER);
}
void draw()
{
//bg
fill(110,185,96);
rect(0,0,400,400);
noStroke();
fill(0);
//boundary
rect(70,250,90,40);
rect(250,100,60,100);
rect(0,0,400,20);
rect(380,0,20,400);
rect(0,0,20,400);
rect(0,380,400,20);
//images
if(tankright){
PImage right=loadImage("Blue Tank Right.png");
image(right,x,y);
}
if(tankleft){
PImage left=loadImage("Blue Tank Left.png");
image(left,x,y);
}
if(tankup){
PImage up=loadImage("Blue Tank Up.png");
image(up,x,y);
}
if(tankdown){
PImage down=loadImage("BlueTank Down.png");
image(down,x,y);
}
}
void keyPressed()
{
if(key == 'w'){
y-=speedy;
dir=UP;
tankup=true;
tankright=false;
tankleft=false;
tankdown=false;
}
if(key == 's'){
y+=speedy;
dir=DOWN;
tankdown=true;
tankup=false;
tankright=false;
tankleft=false;
}
if(key == 'a'){
x+=speedx;
dir=LEFT;
tankleft=false;
tankup=false;
tankright=true;
tankdown=false;
}
if(key == 'd'){
dir=RIGHT;
tankright=false;
tankup=false;
tankleft=true;
tankdown=false;
}
if(dir == RIGHT){
x-=speedx;
}
//speed control x
if(key == '0'){
speedx=0;
speedy=0;
}
if(key == '1' && dir == RIGHT){
speedx=-1;
}
if(key == '1' && dir == LEFT){
speedx=+1;
}
if(key == '2' && dir == RIGHT){
speedx=-2;
}
if(key == '2' && dir == LEFT){
speedx=+2;
}
if(key == '3' && dir == RIGHT){
speedx=-3;
}
if(key == '3' && dir == LEFT){
speedx=+3;
}
//speed control y
if(key == '1' && dir == UP){
speedy=-1;
}
if(key == '1' && dir == DOWN){
speedy=+1;
}
if(key == '2' && dir == UP){
speedy=-2;
}
if(key == '2' && dir == DOWN){
speedy=+2;
}
if(key == '3' && dir == UP){
speedy=-3;
}
if(key == '3' && dir == DOWN){
speedy=+3;
}
}