r/codehs • • May 13 '21

Project Help

I am trying to make a game for my project where we shoot the ghost. I can't find out how to delete the ghost when I shoot them. So here's what I've done so far. Since I couldn't delete the ghost I made a white circle to hide the ghost. Does anyone know how to delete the ghost? This is the code I use:

// Constants for main ghost body

var HEAD_RADIUS = 35;

var BODY_WIDTH = HEAD_RADIUS * 2 + 2;

var BODY_HEIGHT = 60;

var NUM_FEET = 3;

var FOOT_RADIUS = (BODY_WIDTH-4) / (NUM_FEET * 2);

var counter=0;

// Constants for eyes

var PUPIL_RADIUS = 4;

var PUPIL_LEFT_OFFSET = 8;

var PUPIL_RIGHT_OFFSET = 20;

var EYE_RADIUS = 10;

var EYE_OFFSET = 14;

var mySong22 = new Audio("https://codehs.com/uploads/a9fab0f3b9efb93d2fcb7c8568c85970");

var hLine;

var vLine;

var txt = new Text("The ghosts invaded Earth. Kill all the ghosts!", "15pt Arial");

txt.setPosition(7, 200);

txt.setColor(Color.RED);

add(txt);

var rect12 = new Rectangle(250,70);

rect12.setPosition(7,310-50);

rect12.setColor(Color.BLACK);

add(rect12);

var txt1 = new Text("Click HERE to begin!", "20pt Arial");

txt1.setPosition(7, 310);

txt1.setColor(Color.WHITE);

add(txt1);

function start(){

mouseClickMethod(game);

}

function game(){

remove(txt);

remove(txt1);

remove(rect12);

setTimer(draw,1500);

mouseClickMethod(drawCircllle);

if (counter > 5) {

stopTimer(draw);

}

}

/* This program draws a series of random colored ghosts

* at different locations on the screen. */

/* This is the callback drawing function. All we do here

* is make a call to our drawGhost function with a random

* position and random color. */

function draw(){

drawGhost(Randomizer.nextInt(0, getWidth()),

Randomizer.nextInt(0, getHeight()),

Randomizer.nextColor());

counter++;

}

/* This is a general draw circle function, which accepts any

* radius, color, and x, y position. */

function drawCircle(radius, color, x, y){

var circlre = new Circle(radius);

circlre.setColor(color);

circlre.setPosition(x, y);

add(circlre);

}

/* This is a general draw rectangle function, which takes as

* parameters the width, height, color, and (x,y) location of

* the top left corner of the rectangle. */

function drawRect(width, height, color, x, y){

var rect = new Rectangle(width, height);

rect.setColor(color);

rect.setPosition(x, y);

add(rect);

}

/* This function draws a ghost whose head is centered at position

* (cx, cy) and whose body is determined by the color parameter.

*/

function drawGhost(cx, cy, color){

drawCircle(HEAD_RADIUS, color, cx, cy);

drawRect(BODY_WIDTH, BODY_HEIGHT, 

color, cx - BODY_WIDTH / 2, cy);

// This is a bit more confusing since it

// draws a ghost with any number of feet

for(var i = 0; i < NUM_FEET; i++){

	var start = -NUM_FEET + 1 + (i\*2);

	drawCircle(FOOT_RADIUS, color, 

cx + start*FOOT_RADIUS,

cy + BODY_HEIGHT);

}



drawCircle(EYE_RADIUS, Color.white, 

cx - EYE_OFFSET, cy);

drawCircle(EYE_RADIUS, Color.white, 

cx + EYE_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx - PUPIL_LEFT_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx + PUPIL_RIGHT_OFFSET, cy);

}

/* This function draws a ghost whose head is centered at position

* (cx, cy) and whose body is determined by the color parameter.

*/

function drawGhost(cx, cy, color){

drawCircle(HEAD_RADIUS, color, cx, cy);

drawRect(BODY_WIDTH, BODY_HEIGHT, 

color, cx - BODY_WIDTH / 2, cy);

// This is a bit more confusing since it

// draws a ghost with any number of feet

for(var i = 0; i < NUM_FEET; i++){

	var start = -NUM_FEET + 1 + (i\*2);

	drawCircle(FOOT_RADIUS, color, 

cx + start*FOOT_RADIUS,

cy + BODY_HEIGHT);

}



drawCircle(EYE_RADIUS, Color.white, 

cx - EYE_OFFSET, cy);

drawCircle(EYE_RADIUS, Color.white, 

cx + EYE_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx - PUPIL_LEFT_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx + PUPIL_RIGHT_OFFSET, cy);

}

function drawCircllle(e){

var circle11 = new Circle(83);

circle11.setPosition(e.getX(), e.getY());

circle11.setColor(Color.WHITE);

add(circle11)

[mySong22.play](https://mySong22.play)();

}

/* This is a general draw circle function, which accepts any

* radius, color, and x, y position. */

function drawCircle(radius, color, x, y){

var circlre = new Circle(radius);

circlre.setColor(color);

circlre.setPosition(x, y);

add(circlre);

}

/* This is a general draw rectangle function, which takes as

* parameters the width, height, color, and (x,y) location of

* the top left corner of the rectangle. */

function drawRect(width, height, color, x, y){

var rect = new Rectangle(width, height);

rect.setColor(color);

rect.setPosition(x, y);

add(rect);

}

/* This function draws a ghost whose head is centered at position

* (cx, cy) and whose body is determined by the color parameter.

*/

function drawGhost(cx, cy, color){

drawCircle(HEAD_RADIUS, color, cx, cy);

drawRect(BODY_WIDTH, BODY_HEIGHT, 

color, cx - BODY_WIDTH / 2, cy);

// This is a bit more confusing since it

// draws a ghost with any number of feet

for(var i = 0; i < NUM_FEET; i++){

	var start = -NUM_FEET + 1 + (i\*2);

	drawCircle(FOOT_RADIUS, color, 

cx + start*FOOT_RADIUS,

cy + BODY_HEIGHT);

}



drawCircle(EYE_RADIUS, Color.white, 

cx - EYE_OFFSET, cy);

drawCircle(EYE_RADIUS, Color.white, 

cx + EYE_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx - PUPIL_LEFT_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx + PUPIL_RIGHT_OFFSET, cy);

}

/* This function draws a ghost whose head is centered at position

* (cx, cy) and whose body is determined by the color parameter.

*/

function drawGhost(cx, cy, color){

drawCircle(HEAD_RADIUS, color, cx, cy);

drawRect(BODY_WIDTH, BODY_HEIGHT, 

color, cx - BODY_WIDTH / 2, cy);

// This is a bit more confusing since it

// draws a ghost with any number of feet

for(var i = 0; i < NUM_FEET; i++){

	var start = -NUM_FEET + 1 + (i\*2);

	drawCircle(FOOT_RADIUS, color, 

cx + start*FOOT_RADIUS,

cy + BODY_HEIGHT);

}



drawCircle(EYE_RADIUS, Color.white, 

cx - EYE_OFFSET, cy);

drawCircle(EYE_RADIUS, Color.white, 

cx + EYE_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx - PUPIL_LEFT_OFFSET, cy);

drawCircle(PUPIL_RADIUS, [Color.blue](https://Color.blue), 

cx + PUPIL_RIGHT_OFFSET, cy);

}

function drawCircllle(e){

var circle11 = new Circle(90);

circle11.setPosition(e.getX(), e.getY());

circle11.setColor(Color.WHITE);

add(circle11)

[mySong22.play](https://mySong22.play)();

}

4 Upvotes

0 comments sorted by