Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualselve

Posted 21 November 2012 - 01:11 PM

Here's the whole code:

[source lang="cpp"]#include "SDL/SDL.h"#include "SDL/SDL_ttf.h"#include "SDL/SDL_mixer.h"#include "SDL/SDL_image.h"#include <windows.h>#include <stdbool.h>#include <time.h>#define SCREEN_WIDTH 1175#define SCREEN_HEIGHT 585using namespace std;SDL_Surface * screen;SDL_Surface * background;SDL_Surface * ball;SDL_Rect ballposition;SDL_Rect balltarget;SDL_Event event;int mouseX, mouseY;int oldx, oldy;int ballspeed = 3;bool gameover = false;bool targetreached = false;void moveBall(){ if(ballposition.x > balltarget.x) { ballposition.x -= ballspeed; } if(ballposition.x < balltarget.x) { ballposition.x += ballspeed; } if(ballposition.y > balltarget.y) { ballposition.y -= ballspeed; } if(ballposition.y < balltarget.y) { ballposition.y += ballspeed; } if(abs(ballposition.x-balltarget.x)< ballspeed && abs(ballposition.y-balltarget.y)< ballspeed) { balltarget.x = (rand() * 100) % SCREEN_WIDTH; balltarget.y = (rand() * 100) % SCREEN_HEIGHT; }}int main(int argc, char * args[]){ SDL_Init(SDL_INIT_EVERYTHING); Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096); Mix_Chunk * click = Mix_LoadWAV("sounds/click.wav"); screen = SDL_SetVideoMode(1280, 720, 32, SDL_DOUBLEBUF | SDL_SWSURFACE); background = IMG_Load("images/texture.jpg"); ball = IMG_Load("images/ball.png"); ballposition.x = 500; ballposition.y = 400; ballposition.h = 80; ballposition.w = 80; srand(time(0)); balltarget.x = (rand() * 100) % SCREEN_WIDTH; balltarget.y = (rand() * 100) % SCREEN_HEIGHT; while(!gameover) { while(SDL_PollEvent(&amp;event)) { if(event.type == SDL_QUIT) { SDL_Quit(); return 0; } if(event.type == SDL_MOUSEMOTION) { mouseX = event.motion.x; mouseY = event.motion.y; } if(event.type == SDL_MOUSEBUTTONDOWN) if(event.button.button == SDL_BUTTON_LEFT &amp;&amp; ( mouseX >= ballposition.x && mouseX <= ballposition.x + ballposition.w ) &amp;&amp; ( mouseY >= ballposition.y && mouseY <= ballposition.y + ballposition.h ) ) { ballspeed++; Mix_PlayChannel(0, click, 0); } } moveBall(); SDL_BlitSurface(background, NULL, screen, NULL); SDL_BlitSurface(ball, NULL, screen, &amp;ballposition); SDL_Flip(screen); }}[/source]

#1selve

Posted 21 November 2012 - 01:08 PM

Here's the whole code:

[source lang="cpp"]#include "SDL/SDL.h"#include "SDL/SDL_ttf.h"#include "SDL/SDL_mixer.h"#include "SDL/SDL_image.h"#include <windows.h>#include <stdbool.h>#include <time.h>#define SCREEN_WIDTH 1175#define SCREEN_HEIGHT 585using namespace std;SDL_Surface * screen;SDL_Surface * background;SDL_Surface * ball;SDL_Rect ballposition;SDL_Rect balltarget;SDL_Event event;int mouseX, mouseY;int oldx, oldy;int ballspeed = 3;bool gameover = false;bool targetreached = false;void moveBall(){ if(ballposition.x > balltarget.x) { ballposition.x -= ballspeed; } if(ballposition.x < balltarget.x) { ballposition.x += ballspeed; } if(ballposition.y > balltarget.y) { ballposition.y -= ballspeed; } if(ballposition.y < balltarget.y) { ballposition.y += ballspeed; } if(abs(ballposition.x-balltarget.x)<10 && abs(ballposition.y-balltarget.y)< 10) { balltarget.x = (rand() * 100) % SCREEN_WIDTH; balltarget.y = (rand() * 100) % SCREEN_HEIGHT; }}int main(int argc, char * args[]){ SDL_Init(SDL_INIT_EVERYTHING); Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096); Mix_Chunk * click = Mix_LoadWAV("sounds/click.wav"); screen = SDL_SetVideoMode(1280, 720, 32, SDL_DOUBLEBUF | SDL_SWSURFACE); background = IMG_Load("images/texture.jpg"); ball = IMG_Load("images/ball.png"); ballposition.x = 500; ballposition.y = 400; ballposition.h = 80; ballposition.w = 80; srand(time(0)); balltarget.x = (rand() * 100) % SCREEN_WIDTH; balltarget.y = (rand() * 100) % SCREEN_HEIGHT; while(!gameover) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { SDL_Quit(); return 0; } if(event.type == SDL_MOUSEMOTION) { mouseX = event.motion.x; mouseY = event.motion.y; } if(event.type == SDL_MOUSEBUTTONDOWN) if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= ballposition.x && mouseX <= ballposition.x + ballposition.w ) && ( mouseY >= ballposition.y && mouseY <= ballposition.y + ballposition.h ) ) { ballspeed++; Mix_PlayChannel(0, click, 0); } } moveBall(); SDL_BlitSurface(background, NULL, screen, NULL); SDL_BlitSurface(ball, NULL, screen, &ballposition); SDL_Flip(screen); }}[/source]

PARTNERS