Certainly! Below is an example of a simple game you can create using the game development features available on Code.org, particularly using their App Lab interface. This example is a basic "Catch the Ball" game where players try to catch a falling ball by moving a paddle.
Game Concept
- The ball will fall from the top of the screen, and the player must move a paddle left and right to catch it.
- Each time the player catches the ball, they earn a point.
- The game will keep going until the player misses the ball.
Code Implementation
-
Create the User Interface:
- Create a screen with a paddle and a ball. You can use
drawImage
to display these.
- Create a screen with a paddle and a ball. You can use
-
Initialize Variables:
- Create variables to track the position of the paddle, the ball, and the score.
-
Game Loop:
- Use a
setInterval
to update the position of the ball and to check for collisions with the paddle.
- Use a
Here's how the code could look:
// Set up canvas dimensions
var canvasWidth = 400;
var canvasHeight = 400;
setCanvasSize(canvasWidth, canvasHeight);
// Initialize variables
var ballX = Math.random() * canvasWidth;
var ballY = 0; // Start at the top
var ballDiameter = 20;
var paddleX = canvasWidth / 2 - 40; // Start in the center
var paddleWidth = 80;
var paddleHeight = 10;
var score = 0;
// Ball falling speed
var ballSpeed = 5;
// Draw the game elements
function drawGame() {
// Clear the canvas
clearCanvas();
// Draw the ball
fillStyle("red");
ellipse(ballX, ballY, ballDiameter);
// Draw the paddle
fillStyle("blue");
rect(paddleX, canvasHeight - paddleHeight, paddleWidth, paddleHeight);
// Display the score
textSize(20);
textAlign("left", "top");
text("Score: " + score, 10, 10);
}
// Update game state
function updateGame() {
ballY += ballSpeed; // Move ball down
// Check for collision with paddle
if (ballY + ballDiameter >= canvasHeight - paddleHeight &&
ballX + ballDiameter > paddleX &&
ballX < paddleX + paddleWidth) {
// Player caught the ball
score++;
resetBall();
}
// Check if the ball fell past the paddle
if (ballY > canvasHeight) {
// Reset score and ball position
score = 0;
resetBall();
}
}
// Reset ball position
function resetBall() {
ballX = Math.random() * (canvasWidth - ballDiameter);
ballY = 0;
}
// Move paddle based on mouse position
function movePaddle(event) {
var mouseX = event.x;
paddleX = mouseX - paddleWidth / 2; // Center the paddle on mouse
paddleX = Math.max(0, Math.min(canvasWidth - paddleWidth, paddleX)); // Keep paddle within bounds
}
// Start the game loop
setInterval(function() {
updateGame();
drawGame();
}, 1000 / 30); // 30 frames per second
// Listen for mouse movement
onEvent("canvas", "mousemove", movePaddle);
Instructions to Use the Code
-
Create a New Project:
- Go to Code.org and create a new App Lab project or game project.
-
Set Up Canvas:
- Make sure to have a
canvas
element in your design where the game will be displayed.
- Make sure to have a
-
Implement the Code:
- Copy and paste the above code into the code editor in Code.org.
-
Run the Game:
- Test the game by running it in the App Lab environment. You can move the paddle left and right with your mouse to catch the falling ball.
Further Improvements
- You can add sounds for catching the ball and missing it.
- Increase the ball's falling speed as the score increases.
- Add levels or bonuses for greater dynamics.
This game serves as a beginner-friendly programming exercise while using the fun elements of game development! Enjoy coding!