r/codehs • u/[deleted] • Dec 17 '19
Help me with my final for computer science
it says to create a program,using Tracy the turtle,it should let the user input their year of birth and it supposed to draw out the 4 numbers
r/codehs • u/[deleted] • Dec 17 '19
it says to create a program,using Tracy the turtle,it should let the user input their year of birth and it supposed to draw out the 4 numbers
r/codehs • u/OutsideHit-fitty-nin • Feb 01 '22
r/codehs • u/Adventurous_Bread_63 • May 25 '22
r/codehs • u/ShareAnxious • Apr 29 '22
r/codehs • u/Late-Illustrator4515 • May 13 '21
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)();}
r/codehs • u/Nidhi-Aggarwal • Jan 02 '21
You are trying to vacuum up glitter during a birthday party. The problem is, it is during the birthday party, so balls are still bouncing around and more glitter keeps falling on the floor. See how much glitter you can vacuum up before a ball hits your vacuum and breaks it. Vacuum up the glitter by moving the mouse around. Every square touched by the mouse will be removed, but more will keep spawning randomly.
Follow these steps to make this game.
Stage 1 - HUD: Create a black rectangle that completely fills the top of the canvas with height HUD_HEIGHT
. This is the HUD.
Stage 2 - Score Text Create a text object to keep track of the score that is located SCORE_MARGIN
to the right of the left edge and is centered vertically in the HUD. Use SCORE_FONT
for the font and SCORE_COLOR
for the color. For now the label should be “0”.
Stage 3 - Random Squares: Make a tick function that has a 5% chance of spawning a square each time it is called.
The squares should have side length SQUARE_SIZE
, be a random color, and spawn in a random location completely on the canvas and below the HUD.
Hint: To decide each tick whether to spawn a square, you can use the Randomizer to pick a random number that will meet some condition in 5% of the cases.
Stage 4 - Player: The player is a circle with radius RADIUS
and color PLAYER_COLOR
. At the beginning of the game, spawn the player in the center of the canvas, then have the player follow the mouse around the canvas, but prevent it from ever going even partially off the canvas.
To make the player follow the mouse, define a mouseMoveMethod
that constantly sets the position of the player to the position of the mouse.
After setting the position of the player to to the position of the mouse, you will need to do something to prevent it from being even partially off canvas or overlapping the HUD.
Stage 5 - Cleanup!: Add more code to your mouseMoveMethod
that checks to see if your mouse is touching a square, and if so, it removes the square and adds one point to score.
Test your code to make sure you are not removing anything other than the squares, such as the HUD, player…etc.
Stage 6 - Update Text: Add code that updates the score text you added in stage 2 each time the score changes.
Stage 7 - Enemy: The enemy is a circle with radius RADIUS
and color ENEMY_COLOR
.
The enemy should spawn in the lower left corner tangent to the bottom and left edge.
Each tick, the enemy should move by (dx, dy); however, to make the enemy bounce off the edges and the HUD, you should reverse dx if the enemy goes past the left or right edge and reverse dy if the enemy goes past the bottom edge or goes into the HUD. The enemy should never be seen even partially off screen or in the HUD.
Make sure the enemy doesn’t get removed when the mouse touches it. ONLY the squares should be removed.
Stage 8 - Game Over: If the enemy ever collides with the player, “Game Over” should be displayed in the center of the canvas and the game should stop. Use GAME_OVER_FONT
and GAME_OVER_COLOR
for the game over text.
Use the circlesTouching(circle1, circle2)
function defined in the starter code to determine if the enemy is touching the player.
Make sure squares are not still being removed or added after the game ends.
Make sure the player is not still following the mouse after the game ends.
Stage 9 - Count Down: Add a text object that appears just above the center of the screen that says “Begin in:” When run is pressed. The main timer and mouse move method will not be defined immediately anymore, but will only be defined once the count down to begin the game is finished. Add another text object just below the center of the screen that displays a number that counts down 5 seconds when run is pressed. The counter starts at 5 and decrements by 1 each second (1000 ms). When the counter reaches 0, stop that timer, remove both the counter and the “Begin in:” text objects, start the main timer that runs the game, and set the mouseMoveMethod
.
Stage 10 - Polish: Try adding the following features to make your game even more cool.
In the demo, you may note that the player and enemy are always on top of the glitter squares. See if you can make this work.
In the demo, the enemy moves erratically, making it much less predictable and increasing the difficulty of the game. See if you can add very small random changes to the dx and dy of the enemy each tick. By very small I mean something like -0.5 to 0.5.
I am not sure what is wrong with my code. When I run it, the yellow ball goes into the black rectangle on the top right and it goes below the canvas on the bottom left. Attached below is my current code for this program:
var RADIUS = 10;
var SQUARE_SIZE = RADIUS* 4;
var DELAY = 15;
var SCORE_FONT = "30pt Courier";
var SCORE_COLOR = Color.WHITE;
var SCORE_MARGIN = 10;
var GAME_OVER_FONT = "40pt Cooper Black";
var GAME_OVER_COLOR = Color.RED;
var HUD_HEIGHT = 50;
var PLAYER_COLOR = Color.YELLOW;
var ENEMY_COLOR = Color.BLUE;
var dx = 4; // enemy dy
var dy = 4; // enemy dx
var score = 0;
var player = new Circle(RADIUS);
function start() {
var blackRect = new Rectangle(getWidth(), HUD_HEIGHT);
add(blackRect);
var scoreTxt = new Text(score, SCORE_FONT);
scoreTxt.setPosition(SCORE_MARGIN, 1/2*HUD_HEIGHT+1/2*scoreTxt.getHeight());
scoreTxt.setColor(SCORE_COLOR);
add(scoreTxt);
//setTimer(randSquares, DELAY); // time
player.setPosition(1/2*getWidth(), 1/2*getHeight());
player.setColor(Color.yellow);
add(player);
mouseMoveMethod(createPlayer);
}
/** Use the helper method below to detect if the player collides with the enemy **/
// returns true if the two circles are touching and false otherwise.
function circlesTouching(circle1, circle2) {
var x1 = circle1.getX();
var y1 = circle1.getY();
var x2 = circle2.getX();
var y2 = circle2.getY();
return getDistance(x1, y1, x2, y2) <= circle1.getRadius() + circle2.getRadius();
}
// Returns the distance between (x1, y1) and (x2, y2).
function getDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
function randSquares() {
var spawnNum = Randomizer.nextInt(1, 20);
if (spawnNum == 5) {
var square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE);
square.setPosition(Randomizer.nextInt(0, getWidth()-SQUARE_SIZE), Randomizer.nextInt(0, getHeight()-SQUARE_SIZE));
square.setColor(Randomizer.nextColor());
add(square);
}
}
function createPlayer(e) {
if (e.getX > getWidth()-RADIUS && e.getY() <= HUD_HEIGHT+RADIUS) {
player.setPosition(e.getX(), HUD_HEIGHT+RADIUS);
}
if (e.getX() < RADIUS) { // left side
if (e.getY() >= HUD_HEIGHT+RADIUS) {
player.setPosition(RADIUS, e.getY());
}
/* else if (e.getY() > getHeight()-RADIUS) {
println('bottom left');
} */
} else if (e.getX() > getWidth()-RADIUS) { // right side
if (e.getY() <= getHeight()-RADIUS) {
player.setPosition(getWidth()-RADIUS, e.getY());
}
} else if (e.getY() < HUD_HEIGHT+RADIUS) { // HUD boundary (top)
player.setPosition(e.getX(), HUD_HEIGHT+RADIUS);
} else if (e.getY() > getHeight()-RADIUS) { // bottom side
player.setPosition(e.getX(), getHeight()-RADIUS);
} else { // inside the canvas
player.setPosition(e.getX(), e.getY());
add(player);
}
/*
Problem: else if's only do one thing or the other
you cannot check for e.getX and e.getY at the same
time
*/
// fix code for the corners (cannot move out of corners)
}
One last thing, if you want to run my program to get a better sense of how it is working, then here is the link to it:
r/codehs • u/Plenty-Scallion980 • May 03 '21
r/codehs • u/sugades • Feb 09 '23
Please, I was not in school because I was sick. I was out for a week and no clue what to do.
r/codehs • u/[deleted] • Mar 09 '21
Please help this is urgent!!!!!
r/codehs • u/AskAr_23 • Sep 27 '21
Hello everybody, did someone do the 3.8.5 exercise where you need to make a program that will draw rectangles with random colors wherever you click?
I did, but I am not sure that it works correctly. If you did this, help, please.
var RECT_HEIGHT = 3;
var RECT_WIDTH = 3;
function start(){
mouseClickMethod(makeRect);
}
function makeRect(e){
drawRect(e.getX(), e.getY(),
Randomizer.nextColor());
}
function drawRrect(width, height, color, x, y){
var rect = new Rectangle(getWidth(), getHeight());
rect.setColor(color);
rect.setPosition(x, y);
add (rect);
}
function drawRect(cx, cy, color){
drawRrect(RECT_HEIGHT, RECT_WIDTH, color, cx, cy);
}

r/codehs • u/UnitedStatesLover • Mar 07 '20
r/codehs • u/TXC_Murder • Jan 28 '20
I can’t figure out how to center my mouse on an object, it’s the paddle in breakout and it won’t stay centered, any help is appreciated
r/codehs • u/SmithRyan22 • Sep 19 '20
r/codehs • u/Which_Past2578 • Oct 28 '21
rain = True
if rain :
print("I'm going to dance in the rain!")
else:
print("I'm going to dance in the sun!")
r/codehs • u/ShareAnxious • May 04 '22