-
Notifications
You must be signed in to change notification settings - Fork 1
/
run game in c
95 lines (84 loc) · 1.92 KB
/
run game in c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <conio.h>
#include <windows.h>
// Function to set the cursor position
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Function to clear the screen
void clearScreen() {
system("cls");
}
// Player structure
struct Player {
int x;
int y;
int width;
int height;
int speed;
int isJumping;
int jumpHeight;
int jumpCount;
};
// Initialize the player
void initPlayer(struct Player* player) {
player->x = 2;
player->y = 20;
player->width = 2;
player->height = 2;
player->speed = 1;
player->isJumping = 0;
player->jumpHeight = 5;
player->jumpCount = 0;
}
// Keyboard input handling
void handleInput(struct Player* player) {
if (_kbhit()) {
int key = _getch();
if (key == 224) {
key = _getch();
if (key == 72 && !player->isJumping) { // Up arrow key
player->isJumping = 1;
player->jumpCount = 0;
}
else if (key == 80) { // Down arrow key
player->y += player->speed;
}
}
}
}
// Update player position
void updatePlayer(struct Player* player) {
if (player->isJumping) {
player->y -= player->speed;
player->jumpCount += player->speed;
if (player->jumpCount >= player->jumpHeight) {
player->isJumping = 0;
}
}
}
// Draw the player
void drawPlayer(struct Player* player) {
gotoxy(player->x, player->y);
printf("##\n");
printf("##\n");
}
// Game loop
void gameLoop() {
struct Player player;
initPlayer(&player);
while (1) {
clearScreen();
handleInput(&player);
updatePlayer(&player);
drawPlayer(&player);
Sleep(20); // Pause to control the game speed
}
}
int main() {
gameLoop();
return 0;
}