Untitled
unknown
c_cpp
2 years ago
7.6 kB
13
Indexable
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <SDL2/SDL_image.h>
// Screen dimensions
const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 600;
// Player dimensions
const int PLAYER_WIDTH = 50;
const int PLAYER_HEIGHT = 100;
// Player 1 initial position
int player1_x = 100;
int player1_y = SCREEN_HEIGHT - PLAYER_HEIGHT;
int player1_velocity_y = 0;
int player1_remaining_jumps = 2;
// Player 2 initial position
int player2_x = 650;
int player2_y = SCREEN_HEIGHT - PLAYER_HEIGHT;
int player2_velocity_y = 0;
int player2_remaining_jumps = 2;
// Jumping variables
bool player1_jumping = false;
bool player2_jumping = false;
const int JUMP_HEIGHT = 10000;
// Player speeds
const int PLAYER_SPEED = 5;
// Background image
SDL_Texture *backgroundTexture = NULL;
// Initialize SDL and create a window
bool init(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 false;
}
*window = SDL_CreateWindow("Two Players", 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 false;
}
*renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
if (*renderer == NULL)
{
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
return true;
}
// Load background image
bool loadBackground(SDL_Renderer *renderer)
{
SDL_Surface *backgroundSurface = IMG_Load("background.jpg");
if (backgroundSurface == NULL)
{
printf("Unable to load background image! SDL_Image Error: %s\n", IMG_GetError());
return false;
}
backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
if (backgroundTexture == NULL)
{
printf("Unable to create texture from background image! SDL Error: %s\n", SDL_GetError());
return false;
}
SDL_FreeSurface(backgroundSurface);
return true;
}
// Draw the players and background on the screen
void draw(SDL_Renderer *renderer)
{
// Clear the screen
// SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw the background
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
// Draw Player 1 (red rectangle)
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect player1Rect = {player1_x, player1_y, PLAYER_WIDTH, PLAYER_HEIGHT};
SDL_RenderFillRect(renderer, &player1Rect);
// Draw Player 2 (blue rectangle)
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_Rect player2Rect = {player2_x, player2_y, PLAYER_WIDTH, PLAYER_HEIGHT};
SDL_RenderFillRect(renderer, &player2Rect);
// Update the screen
SDL_RenderPresent(renderer);
}
bool checkCollision(int x1, int y1, int x2, int y2, int width, int height)
{
return (x1 < x2 + width &&
x1 + PLAYER_WIDTH > x2 &&
y1 < y2 + height &&
y1 + PLAYER_HEIGHT > y2);
}
int main(int argc, char *argv[])
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
if (!init(&window, &renderer))
{
return 1;
}
if (!loadBackground(renderer))
{
return 1;
}
bool quit = false;
SDL_Event e;
while (!quit)
{
Uint64 start = SDL_GetPerformanceCounter();
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = true;
}
if (e.type == SDL_KEYDOWN)
{
// Player 1 jump
if (e.key.keysym.sym == SDLK_SPACE)
{
if (player1_remaining_jumps > 0)
{
player1_jumping = true;
player1_velocity_y = -20;
player1_remaining_jumps--;
}
}
// Player 2 jump
if (e.key.keysym.sym == SDLK_UP)
{
if (player2_remaining_jumps > 0)
{
player2_jumping = true;
player2_velocity_y = -20;
player2_remaining_jumps--;
}
}
}
}
if (player1_jumping)
{
player1_y += player1_velocity_y;
player1_velocity_y += 1;
if (player1_y >= SCREEN_HEIGHT - PLAYER_HEIGHT)
{
player1_y = SCREEN_HEIGHT - PLAYER_HEIGHT;
player1_jumping = false;
player1_remaining_jumps = 2;
}
}
if (player2_jumping)
{
player2_y += player2_velocity_y;
player2_velocity_y += 1;
if (player2_y >= SCREEN_HEIGHT - PLAYER_HEIGHT)
{
player2_y = SCREEN_HEIGHT - PLAYER_HEIGHT;
player2_jumping = false;
player2_remaining_jumps = 2;
}
}
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
const Uint8 *currentKeyStates = SDL_GetKeyboardState(NULL);
if (checkCollision(player1_x, player1_y, player2_x, player2_y, PLAYER_WIDTH, PLAYER_HEIGHT))
{
if (player1_x < player2_x)
{
player1_x--;
player2_x++;
}
else
{
player1_x++;
player2_x--;
}
}
else
{
// Player 1 movement
if (currentKeyStates[SDL_SCANCODE_A])
{
player1_x -= PLAYER_SPEED;
}
if (currentKeyStates[SDL_SCANCODE_D])
{
player1_x += PLAYER_SPEED;
}
// Player 2 movement
if (currentKeyStates[SDL_SCANCODE_LEFT])
{
player2_x -= PLAYER_SPEED;
}
if (currentKeyStates[SDL_SCANCODE_RIGHT])
{
player2_x += PLAYER_SPEED;
}
// Keep players within the screen boundaries
if (player1_x < 0)
{
player1_x = 0;
}
if (player1_x > SCREEN_WIDTH - PLAYER_WIDTH)
{
player1_x = SCREEN_WIDTH - PLAYER_WIDTH;
}
if (player2_x < 0)
{
player2_x = 0;
}
if (player2_x > SCREEN_WIDTH - PLAYER_WIDTH)
{
player2_x = SCREEN_WIDTH - PLAYER_WIDTH;
}
}
draw(renderer);
Uint64 end = SDL_GetPerformanceCounter();
float elapsedMS = (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
// Cap to 60 FPS
SDL_Delay(floor(16.666f - elapsedMS));
}
// Cleanup and exit
SDL_DestroyTexture(backgroundTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}Editor is loading...