c++ code for bouncing ball
#include <SDL2/SDL.h>
#include <stdio.h>
// Screen dimensions
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// Ball dimensions
const int BALL_SIZE = 20;
// Ball attributes
int ballX, ballY; // Ball position
int ballVelocityX, ballVelocityY; // Ball velocity
// Function to initialize SDL and create a window
int initSDL(SDL_Window** window, SDL_Renderer** renderer) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*window = SDL_CreateWindow("Bouncing Ball", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (*window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
if (*renderer == NULL) {
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
return 1;
}
// Function to handle events (e.g., quit)
int handleEvents(SDL_Event* e) {
while (SDL_PollEvent(e) != 0) {
if (e->type == SDL_QUIT) {
return 0;
}
}
return 1;
}
// Function to update ball position
void updateBallPosition() {
ballX += ballVelocityX;
ballY += ballVelocityY;
// Bounce when hitting the screen edges
if (ballX < 0 || ballX + BALL_SIZE > SCREEN_WIDTH) {
ballVelocityX = -ballVelocityX;
}
if (ballY < 0 || ballY + BALL_SIZE > SCREEN_HEIGHT) {
ballVelocityY = -ballVelocityY;
}
}
// Function to render the ball
void render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect ballRect = { ballX, ballY, BALL_SIZE, BALL_SIZE };
SDL_RenderFillRect(renderer, &ballRect);
SDL_RenderPresent(renderer);
}
int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Event e;
if (!initSDL(&window, &renderer)) {
return 1;
}
// Initialize ball attributes
ballX = SCREEN_WIDTH / 2 - BALL_SIZE / 2;
ballY = SCREEN_HEIGHT / 2 - BALL_SIZE / 2;
ballVelocityX = 2;
ballVelocityY = 2;
int quit = 0;
while (!quit) {
quit = !handleEvents(&e);
updateBallPosition();
render(renderer);
SDL_Delay(10); // Add a small delay to control frame rate
}
// Cleanup and exit
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Creating a basic bouncing ball game in C without a graphics library like SDL can be challenging, as C does not have built-in support for graphics. However, you can use a console-based approach to simulate a simple bouncing ball animation. Here's a basic text-based bouncing ball game in C:
```c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Windows-specific sleep function
// Console dimensions
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
// Ball attributes
int ballX, ballY; // Ball position
int ballVelocityX = 1; // Ball X velocity
int ballVelocityY = 1; // Ball Y velocity
// Function to clear the console screen
void clearScreen() {
system("cls");
}
// Function to set the cursor position in the console
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Function to update the ball position
void updateBallPosition() {
ballX += ballVelocityX;
ballY += ballVelocityY;
// Bounce when hitting the screen edges
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 1) {
ballVelocityX = -ballVelocityX;
}
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 1) {
ballVelocityY = -ballVelocityY;
}
}
int main() {
// Initialize ball attributes
ballX = SCREEN_WIDTH / 2;
ballY = SCREEN_HEIGHT / 2;
while (1) {
updateBallPosition();
clearScreen();
gotoxy(ballX, ballY);
printf("O");
// Sleep for a short duration to control frame rate
Sleep(100);
// Flush the output buffer to update the screen
fflush(stdout);
}
return 0;
}
```
Please note that this code is specifically designed for Windows consoles, as it uses the `windows.h` library for console manipulation. If you're using a different platform, you may need to use platform-specific libraries or adopt a cross-platform library like NCurses for console-based graphics.
This is a very basic example and doesn't have many game features. You can enhance it by adding keyboard controls, multiple balls, scoring, and more, depending on your requirements.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Windows-specific sleep function
// Console dimensions
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
// Ball attributes
int ballX, ballY; // Ball position
int ballVelocityX = 1; // Ball X velocity
int ballVelocityY = 1; // Ball Y velocity
// Function to clear the console screen
void clearScreen() {
system("cls");
}
// Function to set the cursor position in the console
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Function to update the ball position
void updateBallPosition() {
ballX += ballVelocityX;
ballY += ballVelocityY;
// Bounce when hitting the screen edges
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 1) {
ballVelocityX = -ballVelocityX;
}
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 1) {
ballVelocityY = -ballVelocityY;
}
}
int main() {
// Initialize ball attributes
ballX = SCREEN_WIDTH / 2;
ballY = SCREEN_HEIGHT / 2;
while (1) {
updateBallPosition();
clearScreen();
gotoxy(ballX, ballY);
printf("O");
// Sleep for a short duration to control frame rate
Sleep(100);
// Flush the output buffer to update the screen
fflush(stdout);
}
return 0;
}
PYTHON CODE FOR BOUNCING BALL
Creating a simple bouncing ball game in Python can be done using a graphics library like Pygame. Here's a basic example of a bouncing ball game using Pygame:
```python
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BALL_SIZE = 20
BALL_COLOR = (255, 0, 0)
BG_COLOR = (0, 0, 0)
BALL_SPEED_X = 5
BALL_SPEED_Y = 5
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Bouncing Ball Game")
# Ball attributes
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_velocity_x = BALL_SPEED_X
ball_velocity_y = BALL_SPEED_Y
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Update ball position
ball_x += ball_velocity_x
ball_y += ball_velocity_y
# Bounce when hitting the screen edges
if ball_x <= 0 or ball_x >= SCREEN_WIDTH - BALL_SIZE:
ball_velocity_x = -ball_velocity_x
if ball_y <= 0 or ball_y >= SCREEN_HEIGHT - BALL_SIZE:
ball_velocity_y = -ball_velocity_y
# Fill the background
screen.fill(BG_COLOR)
# Draw the ball
pygame.draw.ellipse(screen, BALL_COLOR, (ball_x, ball_y, BALL_SIZE, BALL_SIZE))
# Update the display
pygame.display.update()
```
Before running this code, you'll need to install Pygame if you haven't already. You can install Pygame using pip:
```bash
pip install pygame
```
This code creates a Pygame window and animates a bouncing ball inside it. You can customize the ball's size, speed, colors, and other properties according to your preferences.
#include <SDL2/SDL.h>
#include <stdio.h>
// Screen dimensions
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// Ball dimensions
const int BALL_SIZE = 20;
// Ball attributes
int ballX, ballY; // Ball position
int ballVelocityX, ballVelocityY; // Ball velocity
// Function to initialize SDL and create a window
int initSDL(SDL_Window** window, SDL_Renderer** renderer) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*window = SDL_CreateWindow("Bouncing Ball", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (*window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
if (*renderer == NULL) {
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
return 1;
}
// Function to handle events (e.g., quit)
int handleEvents(SDL_Event* e) {
while (SDL_PollEvent(e) != 0) {
if (e->type == SDL_QUIT) {
return 0;
}
}
return 1;
}
// Function to update ball position
void updateBallPosition() {
ballX += ballVelocityX;
ballY += ballVelocityY;
// Bounce when hitting the screen edges
if (ballX < 0 || ballX + BALL_SIZE > SCREEN_WIDTH) {
ballVelocityX = -ballVelocityX;
}
if (ballY < 0 || ballY + BALL_SIZE > SCREEN_HEIGHT) {
ballVelocityY = -ballVelocityY;
}
}
// Function to render the ball
void render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect ballRect = { ballX, ballY, BALL_SIZE, BALL_SIZE };
SDL_RenderFillRect(renderer, &ballRect);
SDL_RenderPresent(renderer);
}
int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Event e;
if (!initSDL(&window, &renderer)) {
return 1;
}
// Initialize ball attributes
ballX = SCREEN_WIDTH / 2 - BALL_SIZE / 2;
ballY = SCREEN_HEIGHT / 2 - BALL_SIZE / 2;
ballVelocityX = 2;
ballVelocityY = 2;
int quit = 0;
while (!quit) {
quit = !handleEvents(&e);
updateBallPosition();
render(renderer);
SDL_Delay(10); // Add a small delay to control frame rate
}
// Cleanup and exit
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Creating a basic bouncing ball game in C without a graphics library like SDL can be challenging, as C does not have built-in support for graphics. However, you can use a console-based approach to simulate a simple bouncing ball animation. Here's a basic text-based bouncing ball game in C:
```c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Windows-specific sleep function
// Console dimensions
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
// Ball attributes
int ballX, ballY; // Ball position
int ballVelocityX = 1; // Ball X velocity
int ballVelocityY = 1; // Ball Y velocity
// Function to clear the console screen
void clearScreen() {
system("cls");
}
// Function to set the cursor position in the console
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Function to update the ball position
void updateBallPosition() {
ballX += ballVelocityX;
ballY += ballVelocityY;
// Bounce when hitting the screen edges
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 1) {
ballVelocityX = -ballVelocityX;
}
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 1) {
ballVelocityY = -ballVelocityY;
}
}
int main() {
// Initialize ball attributes
ballX = SCREEN_WIDTH / 2;
ballY = SCREEN_HEIGHT / 2;
while (1) {
updateBallPosition();
clearScreen();
gotoxy(ballX, ballY);
printf("O");
// Sleep for a short duration to control frame rate
Sleep(100);
// Flush the output buffer to update the screen
fflush(stdout);
}
return 0;
}
```
Comments
Post a Comment
NAME
EMAIL ID
MESSAGE