Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <stdio.h>
- #include <iostream>
- #include <SDL_mixer.h>
- #include "Player.h"
- Mix_Music* music = NULL;
- Mix_Chunk* seffect = NULL;
- SDL_Window* window = NULL;
- SDL_Surface* windowsurface = NULL;
- SDL_Surface* character = NULL;
- SDL_Surface* character2 = NULL;
- SDL_Surface* bullet1 = NULL;
- SDL_Surface* background = NULL;
- SDL_GameController* controller = NULL;
- int main(int argc, char* args[]) {
- const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
- Player player1 = Player(100, 20, "BMPs/character.bmp", 15);
- Player player2 = Player(200, 20, "BMPs/character2.bmp", 15);
- std::cout << player1.startingx << std::endl;
- bool bullet1exists = false;
- bool y1 = false;
- bool y2 = false;
- bool x1 = false;
- bool x2 = false;
- bool y21 = false;
- bool y22 = false;
- bool x21 = false;
- bool x22 = false;
- int bulletready = 0;
- int bullet1speed = 10;
- SDL_Init(SDL_INIT_VIDEO);
- SDL_Init(SDL_INIT_AUDIO);
- Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
- bool pollevent = false;
- SDL_Event movementevent;
- int windowx = 500;
- int windowy = 500;
- int windoww = 1000;
- int windowh = 1000;
- SDL_Rect movement;
- movement.x = player1.startingx;
- movement.y = player1.startingy;
- SDL_Rect movement2;
- movement2.x = player2.startingx;
- movement2.y = player2.startingy;
- SDL_Rect bullet1movement;
- bullet1movement.x = 50;
- bullet1movement.y = 50;
- controller = SDL_GameControllerOpen(0);
- window = SDL_CreateWindow("BMPs/movement game", windowx, windowy, windoww, windowh, SDL_WINDOW_FULLSCREEN);
- windowsurface = SDL_GetWindowSurface(window);
- music = Mix_LoadMUS("F777_darkangel.wav");
- bullet1 = SDL_LoadBMP("BMPs/bullet.bmp");
- background = SDL_LoadBMP("BMPs/background.bmp");
- SDL_SetColorKey(player1.image, SDL_TRUE, 0xFF00FF);
- SDL_SetColorKey(bullet1, SDL_TRUE, 0xFF00FF);
- bool quit = false;
- while (quit == false) {
- if (Mix_PlayingMusic() == 0)
- {
- Mix_PlayMusic(music, -1);
- }
- if (Mix_PausedMusic() == 1)
- {
- Mix_ResumeMusic();
- }
- SDL_BlitSurface(background, NULL, windowsurface, NULL);
- if (player2.alive == true) {
- SDL_BlitSurface(player1.image, NULL, windowsurface, &movement);
- }
- if (player2.alive == true) {
- SDL_BlitSurface(player2.image, NULL, windowsurface, &movement2);
- }
- SDL_BlitSurface(bullet1, NULL, windowsurface, &bullet1movement);
- while (SDL_PollEvent(&movementevent) != 0) {
- if (player1.alive == true) {
- SDL_BlitSurface(player1.image, NULL, windowsurface, &movement);
- }
- if (player2.alive == true) {
- SDL_BlitSurface(player2.image, NULL, windowsurface, &movement2);
- }
- SDL_BlitSurface(bullet1, NULL, windowsurface, &bullet1movement);
- if (movementevent.type == SDL_QUIT) {
- quit = true;
- }
- if (movementevent.type == SDL_KEYDOWN) {
- if (currentKeyStates[SDL_SCANCODE_W])
- {
- y1 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_A]) {
- x1 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_S]) {
- y2 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_D]) {
- x2 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_UP]) {
- y21 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_LEFT]) {
- x21 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_DOWN]) {
- y22 = true;
- }
- if (currentKeyStates[SDL_SCANCODE_RIGHT]) {
- x22 = true;
- }
- }
- }
- if (y1 == true) {
- movement.y -= player1.speed;
- }
- if (x1 == true) {
- movement.x -= player1.speed;
- }
- if (y2 == true) {
- movement.y += player1.speed;
- }
- if (x2 == true) {
- movement.x += player1.speed;
- }
- if (y21 == true) {
- movement2.y -= player2.speed;
- }
- if (x21 == true) {
- movement2.x -= player2.speed;
- }
- if (y22 == true) {
- movement2.y += player2.speed;
- }
- if (x22 == true) {
- movement2.x += player2.speed;
- }
- SDL_UpdateWindowSurface(window);
- if (movementevent.type == SDL_KEYUP) {
- if (!currentKeyStates[SDL_SCANCODE_W])
- {
- y1 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_A]) {
- x1 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_S]) {
- y2 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_D]) {
- x2 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_UP]) {
- y21 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_LEFT]) {
- x21 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_DOWN]) {
- y22 = false;
- }
- if (!currentKeyStates[SDL_SCANCODE_RIGHT]) {
- x22 = false;
- }
- }
- if (bulletready > 500) {
- if (player1.alive && movement.y + movement.y - bullet1movement.x + bullet1movement.y < movement2.y + movement2.y - bullet1movement.x + bullet1movement.y) {
- if (movement.x < bullet1movement.x) {
- bullet1movement.x -= bullet1speed;
- }
- if (movement.x > bullet1movement.x) {
- bullet1movement.x += bullet1speed;
- }
- if (movement.y < bullet1movement.y) {
- bullet1movement.y -= bullet1speed;
- }
- if (movement.y > bullet1movement.y) {
- bullet1movement.y += bullet1speed;
- }
- }
- else if (player2.alive && movement.y + movement.y - bullet1movement.x + bullet1movement.y > movement2.y + movement2.y - bullet1movement.x + bullet1movement.y) {
- if (movement2.x < bullet1movement.x) {
- bullet1movement.x -= bullet1speed;
- }
- if (movement2.x > bullet1movement.x) {
- bullet1movement.x += bullet1speed;
- }
- if (movement2.y < bullet1movement.y) {
- bullet1movement.y -= bullet1speed;
- }
- if (movement2.y > bullet1movement.y) {
- bullet1movement.y += bullet1speed;
- }
- }
- if (movement.x + 50 > bullet1movement.x && movement.x - 50 < bullet1movement.x && movement.y + 50 > bullet1movement.y && movement.y - 50 < bullet1movement.y) {
- player1.kill();
- }
- if (movement2.x + 50 > bullet1movement.x && movement2.x - 50 < bullet1movement.x && movement2.y + 50 > bullet1movement.y && movement2.y - 50 < bullet1movement.y) {
- player2.kill();
- }
- }
- bulletready++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement