Im making a game with ChatGPT. and I need some help to add some simple features

So far I am making a dxball game. It works very well. I have one thing I would like to add. Here is what the game does so far:

  • a ball and a bar
  • when ball hits bar you score 1 point
  • every time the ball hits the bar, the ball goes slightly faster
  • game over if you miss the ball

Whenever you hit 10 score points, the ball duplicates.

I have tried multiple ways to get this right but I failed everytime.

Code as follow:

// Define variables for the canvas and drawing context
var canvas = document.getElementById(“myCanvas”);
var ctx = canvas.getContext(“2d”);

// Define variables for the ball and its starting position and speed
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
var ballSpeed = 2;
var points = 0;

// Define variables for the bar and its starting position and size
var barHeight = 10;
var barWidth = 75;
var barX = (canvas.width-barWidth)/2;

// Define a variable for the score
var score = 0;

// Define a function to draw the ball on the canvas
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = “#0095DD”;
ctx.fill();
ctx.closePath();
}

// Define a function to draw the bar on the canvas
function drawBar() {
ctx.beginPath();
ctx.rect(barX, canvas.height-barHeight, barWidth, barHeight);
ctx.fillStyle = “#0095DD”;
ctx.fill();
ctx.closePath();
}

// Define a function to move the ball and detect collisions with the walls and the bar
function moveBall() {
x += dx * ballSpeed;
y += dy * ballSpeed;

if (x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
    dx = -dx;
}

if (y + dy < ballRadius) {
    dy = -dy;
} else if (y + dy > canvas.height-ballRadius) {
    if (x > barX && x < barX + barWidth) {
        dy = -dy;
        ballSpeed += 0.1;
        score++;
    } else {
        alert("GAME OVER");
        document.location.reload();
    }
}

if (rightPressed && barX < canvas.width-barWidth) {
    barX += 7;
} else if (leftPressed && barX > 0) {
    barX -= 7;
}

}

// Define functions to handle user input
var rightPressed = false;
var leftPressed = false;

function keyDownHandler(event) {
if (event.keyCode == 39) {
rightPressed = true;
} else if (event.keyCode == 37) {
leftPressed = true;
}
}

function keyUpHandler(event) {
if (event.keyCode == 39) {
rightPressed = false;
} else if (event.keyCode == 37) {
leftPressed = false;
}
}

// Add event listeners for keyboard input
document.addEventListener(“keydown”, keyDownHandler, false);
document.addEventListener(“keyup”, keyUpHandler, false);

// Define a function to update the game state and draw the game elements on the canvas
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawBar();
moveBall();
drawScore();
requestAnimationFrame(draw);
}

// Define a function to draw the score on the canvas
function drawScore() {
ctx.font = “16px Arial”;
ctx.fillStyle = “#0095DD”;
ctx.fillText("Score: " + score, 8, 20);
}

// Start the game loop
draw();

Essentially I want to make a super simple fxball game, without bricks. And the game starts off slow and gets more difficult and insane. Im bored, so I thought that would be fun to try to make and play

Was there something specific about the failure? Or is the entire codebase somehow cursed by an intangible AI-related quality?

So ive had multiple tries.

Sometimes it happened that the ball would duplicate endlessly.

Sometimes the game would not run.

I think when the ball duplicated endlessly I was getting close, but then I gave up and I did not save that specific iteration of the code because I got frustrated.

I think what I need to do is to make sure that the game calculates that EVERY 10 points, a new ball appears. For some reason the ball was appearing non stop for any number under 10, and it was spawning endlessly.

The AI is great, its me who lack the vocabulary, and the knowledge to explain what I want clearly to the AI. I am sure for anyone who knows a bit of code it must fasten your process for basic games insanely fast.

can you post a smaller snippet of just the ball duplication logic? maybe we can just stare at it enough to figure out why it’s going wrong :microscope:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.