生成一个坦克大战的代码
时间: 2023-06-23 11:04:10 浏览: 122
好的,下面是一个简单的C语言坦克大战游戏代码示例,使用SDL图形库:
```c
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define TANK_SIZE 32
#define WALL_SIZE 32
#define BULLET_SIZE 8
typedef struct {
int x, y;
int direction; // 0: up, 1: right, 2: down, 3: left
int health;
bool is_firing;
} Tank;
typedef struct {
int x, y;
} Wall;
typedef struct {
int x, y;
int direction; // 0: up, 1: right, 2: down, 3: left
bool is_active;
} Bullet;
void move_tank(Tank *tank, int dx, int dy) {
tank->x += dx;
tank->y += dy;
}
void fire_bullet(Tank *tank, Bullet *bullet) {
bullet->x = tank->x + TANK_SIZE / 2 - BULLET_SIZE / 2;
bullet->y = tank->y + TANK_SIZE / 2 - BULLET_SIZE / 2;
bullet->direction = tank->direction;
bullet->is_active = true;
}
void move_bullet(Bullet *bullet, int dx, int dy) {
bullet->x += dx;
bullet->y += dy;
}
bool is_colliding(Tank *tank, Bullet *bullet) {
if (bullet->x + BULLET_SIZE >= tank->x && bullet->x <= tank->x + TANK_SIZE &&
bullet->y + BULLET_SIZE >= tank->y && bullet->y <= tank->y + TANK_SIZE) {
return true;
}
return false;
}
void render_tank(SDL_Renderer *renderer, Tank *tank) {
SDL_Rect rect = { tank->x, tank->y, TANK_SIZE, TANK_SIZE };
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
void render_wall(SDL_Renderer *renderer, Wall *wall) {
SDL_Rect rect = { wall->x, wall->y, WALL_SIZE, WALL_SIZE };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &rect);
}
void render_bullet(SDL_Renderer *renderer, Bullet *bullet) {
if (bullet->is_active) {
SDL_Rect rect = { bullet->x, bullet->y, BULLET_SIZE, BULLET_SIZE };
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Tank War", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event;
Tank tank1 = { 0, 0, 0, 100, false };
Tank tank2 = { SCREEN_WIDTH - TANK_SIZE, SCREEN_HEIGHT - TANK_SIZE, 2, 100, false };
Wall wall1 = { SCREEN_WIDTH / 2 - WALL_SIZE / 2, SCREEN_HEIGHT / 2 - WALL_SIZE / 2 };
Wall wall2 = { SCREEN_WIDTH / 2 - WALL_SIZE / 2, SCREEN_HEIGHT / 2 - WALL_SIZE / 2 - WALL_SIZE };
Wall wall3 = { SCREEN_WIDTH / 2 - WALL_SIZE / 2, SCREEN_HEIGHT / 2 - WALL_SIZE / 2 + WALL_SIZE };
Bullet bullet1 = { 0, 0, 0, false };
Bullet bullet2 = { 0, 0, 0, false };
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_w:
tank1.direction = 0;
move_tank(&tank1, 0, -1);
break;
case SDLK_d:
tank1.direction = 1;
move_tank(&tank1, 1, 0);
break;
case SDLK_s:
tank1.direction = 2;
move_tank(&tank1, 0, 1);
break;
case SDLK_a:
tank1.direction = 3;
move_tank(&tank1, -1, 0);
break;
case SDLK_SPACE:
tank1.is_firing = true;
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_SPACE:
tank1.is_firing = false;
break;
}
break;
}
}
if (tank1.is_firing && !bullet1.is_active) {
fire_bullet(&tank1, &bullet1);
}
if (bullet1.is_active) {
switch (bullet1.direction) {
case 0:
move_bullet(&bullet1, 0, -1);
break;
case 1:
move_bullet(&bullet1, 1, 0);
break;
case 2:
move_bullet(&bullet1, 0, 1);
break;
case 3:
move_bullet(&bullet1, -1, 0);
break;
}
if (is_colliding(&tank2, &bullet1)) {
bullet1.is_active = false;
tank2.health -= 10;
}
if (bullet1.x < 0 || bullet1.y < 0 || bullet1.x + BULLET_SIZE > SCREEN_WIDTH || bullet1.y + BULLET_SIZE > SCREEN_HEIGHT) {
bullet1.is_active = false;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
render_tank(renderer, &tank1);
render_tank(renderer, &tank2);
render_wall(renderer, &wall1);
render_wall(renderer, &wall2);
render_wall(renderer, &wall3);
render_bullet(renderer, &bullet1);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
这只是一个简单的示例代码,实际的游戏需要更多的功能和细节。希望这个例子能够帮助您开始您自己的坦克大战游戏编写。
阅读全文