Jump to content

  • Log In with Google      Sign In   
  • Create Account


selve

Member Since 03 Nov 2012
Offline Last Active Mar 27 2013 08:32 PM
-----

Posts I've Made

In Topic: [C++] Delayed action of srand() - coordinates for object

22 November 2012 - 09:31 AM

But if would do " || " it will for example work like this:
The ball has reached Y point, but didn't reached X point, so the new X and the new Y are created.
And I want to do it if the ball reaches X and Y. Am I right?

In Topic: [C++] Delayed action of srand() - coordinates for object

21 November 2012 - 01:14 PM

balltarget.x = (rand() * 100) % SCREEN_WIDTH;


That code basically picks a random column that is a multiple of the greatest common divisor of 100 and SCREEN_WIDTH. Are you sure that's what you want?

So if I want a normal random not multiplied should I do this:

[source lang="cpp"]balltarget.x = ( (rand() * 100) % SCREEN_WIDTH) / 2;[/source]

In Topic: [C++] Delayed action of srand() - coordinates for object

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)< 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]

In Topic: [C++] Delayed action of srand() - coordinates for object

21 November 2012 - 01:05 PM

Thanks all, now it's working.
Instead of using:
[source lang="cpp"]if(ballposition.x == balltarget.x && ballposition.y == balltarget.y) { balltarget.x = (rand() * 100) % SCREEN_WIDTH; balltarget.y = (rand() * 100) % SCREEN_HEIGHT; }[/source]

I used:
[source lang="cpp"]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; }[/source]

In Topic: [C++] Delayed action of srand() - coordinates for object

21 November 2012 - 12:11 PM

So how should it look like?
I've did this in ball moving function:
[source lang="cpp"]if(ballposition.x == ballX && ballposition.y == ballY) { targetreached = true; }[/source]
And this in while(!gameover):
[source lang="cpp"]if(targetreached) { srand(time(NULL)); do { ballX = randx; ballY = randy; }while(abs(oldx-ballX) > 300 && abs(oldy-ballY) > 300); oldx = ballX; oldy = ballY; targetreached = false; }[/source]

But it is still not working ;/ The ball is just moving to one position and then doesn't move any more.

PARTNERS