テニスゲーム
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 50
#define HEIGHT 25
int ballX, ballY, ballDirX, ballDirY, paddleY;
char screen[WIDTH][HEIGHT];
void initialize()
{
int i, j;
for (i = 0; i < WIDTH; i++) {
for (j = 0; j < HEIGHT; j++) {
screen[i][j] = ' ';
}
}
ballX = WIDTH / 2;
ballY = HEIGHT / 2;
ballDirX = -1;
ballDirY = 0;
paddleY = HEIGHT / 2 - 2;
}
void drawScreen()
{
int i, j;
system("cls");
for (j = 0; j < HEIGHT; j++) {
for (i = 0; i < WIDTH; i++) {
putchar(screen[i][j]);
}
putchar('\n');
}
}
void moveBall()
{
if (screen[ballX + ballDirX][ballY + ballDirY] == '#') {
ballDirX = -ballDirX;
} else if (ballY + ballDirY == 0 || ballY + ballDirY == HEIGHT - 1) {
ballDirY = -ballDirY;
} else if (ballX + ballDirX == 0) {
printf("Game Over\n");
exit(0);
} else if (ballX + ballDirX == WIDTH - 1) {
ballDirX = -ballDirX;
} else if (ballX + ballDirX == WIDTH - 2) {
if (ballY + ballDirY >= paddleY && ballY + ballDirY <= paddleY + 3) {
ballDirX = -ballDirX;
} else {
printf("Game Over\n");
exit(0);
}
}
screen[ballX][ballY] = ' ';
ballX += ballDirX;
ballY += ballDirY;
screen[ballX][ballY] = 'O';
}
void movePaddle(int dir)
{
if (paddleY + dir >= 0 && paddleY + dir + 3 < HEIGHT) {
screen[WIDTH - 1][paddleY] = ' ';
screen[WIDTH - 1][paddleY + 1] = ' ';
screen[WIDTH - 1][paddleY + 2] = ' ';
screen[WIDTH - 1][paddleY + 3] = ' ';
paddleY += dir;
screen[WIDTH - 1][paddleY] = '|';
screen[WIDTH - 1][paddleY + 1] = '|';
screen[WIDTH - 1][paddleY + 2] = '|';
screen[WIDTH - 1][paddleY + 3] = '|';
}
}
int main()
{
int ch;
initialize();
while (1) {
if (_kbhit()) {
ch = _getch();
if (ch == 'w') {
movePaddle(-1);
} else if (ch == 's') {
movePaddle(1);
} else if (ch == 'q') {
break;
}
}
moveBall();
この記事が気に入ったらサポートをしてみませんか?