Ali hussain
u#include
#include
#include
// Constants
int tomX = 100, tomY = 100;
int jerryX = 300, jerryY = 300;
int tomSpeed = 5;
int jerrySpeed = 3;
int radiusTom = 20;
int radiusJerry = 10;
// Function to detect collision (avoiding sqrt)
int detectCollision(int tomX, int tomY, int jerryX, int jerryY) {
int dx = tomX - jerryX;
int dy = tomY - jerryY;
int distanceSquared = dx * dx + dy * dy; // Distance squared
int radiusSum = radiusTom + radiusJerry;
if (distanceSquared <= radiusSum * radiusSum) {
return 1; // Collision detected
}
return 0; // No collision
}
// Function to draw Tom and Jerry
void drawCharacters() {
cleardevice();
// Draw Tom
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
fillellipse(tomX, tomY, radiusTom, radiusTom);
// Draw Jerry
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(jerryX, jerryY, radiusJerry, radiusJerry);
delay(100); // Adding a small delay
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Game loop
while (1) {
drawCharacters();
// Move Jerry randomly
jerryX += jerrySpeed;
if (jerryX > getmaxx() - radiusJerry || jerryX < radiusJerry) {
jerrySpeed = -jerrySpeed;
}
// Control Tom with arrow keys
if (kbhit()) {
char ch = getch();
if (ch == 27) // ESC to exit
break;
if (ch == 0) {
ch = getch(); // For arrow keys
if (ch == 75) tomX -= tomSpeed; // Left arrow
if (ch == 77) tomX += tomSpeed; // Right arrow
if (ch == 72) tomY -= tomSpeed; // Up arrow
if (ch == 80) tomY += tomSpeed; // Down arrow
}
}
// Check collision
if (detectCollision(tomX, tomY, jerryX, jerryY)) {
outtextxy(200, 200, "Jerry Caught!");
break;
}
}
getch();
closegraph();
return 0;
}
C Program Topics:
Basic C Program
Control Flow
Pattern Printing
Functions
Arrays
Strings
Conversions
Pointers
Structures and Unions
File IO
Date and Time
More C Programs
Prerequisite: Before you start with these C programs, take a look at our C tutorial to get a good understanding of the basics. Bascally, C programming is a versatile language that powers many foundational systems. For those looking to strengthen their skills in C and master data structures, the C Programming Course Online with Data Structures offers a variety of example programs and deep dives into practical applications.
C Program – Basic
C Hello World Program
C Program to Print Your Own Name
C Program to Print an Integer Entered By the User
C Program to Add Two Numbers
C Program to Check Whether a Number is Prime or Not
C Program to Multiply two Floating-Point Numbers
C Program to Print the ASCII Value of a Character
C Program to Swap Two Numbers
C Program to Calculate Fahrenheit to Celsius
C Program to Find the Size of int, float, double, and char
C Program to Add Two Complex Numbers
C Program to Print Prime Numbers From 1 to N
C Program to Find Simple Interest
C Program to Find Compound Interest
C Program for Area And Perimeter Of Rectangle
https://sites.google.com/view/a-comput/home
https://sites.google.com/view/a-comput/home
ReplyDelete👍 good like Kar ro
Deletewatch now
ReplyDelete"This is a great example of using graphics in C to create a simple game. The collision detection logic is straightforward and effective. It would be interesting to see how this could be expanded with more features or integrated into a larger project."
ReplyDeletebest regards
starlink residential lite australia