selve

Member
  • Content count

    18
  • Joined

  • Last visited

Community Reputation

145 Neutral

About selve

  • Rank
    Member

Personal Information

  1. Hi guys, I want to know how to rotate an image and make a collision with another object like this: First collision it's just an easiest collision without rotating any image. When the ball collides with the square, the XVEL of the ball changes to -XVEL. Now the question for you: how to make a rotated image and collision with the ball that changes the xVelocity and yVelocity depending on the rotated image position.  
  2. 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?
  3. [quote name='Álvaro' timestamp='1353525039' post='5002987'] [quote][code] balltarget.x = (rand() * 100) % SCREEN_WIDTH;[/code][/quote] 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? [/quote] So if I want a normal random not multiplied should I do this: [source lang="cpp"]balltarget.x = ( (rand() * 100) % SCREEN_WIDTH) / 2;[/source]
  4. 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 585 using 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]
  5. 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]
  6. 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.
  7. Oh, sorry. Random numbers are defined here: [source lang="cpp"]#define randx rand()*100%1175 #define randy rand()*100%585[/source] 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 randx rand()*100%1175 #define randy rand()*100%585 using namespace std; SDL_Surface * screen; SDL_Surface * background; SDL_Surface * ball; SDL_Rect ballposition; SDL_Event event; int mouseX, mouseY; int ballX = randx, ballY = randy; int oldx, oldy; int ballspeed = 1; bool gameover = false; void moveBall() { if(ballposition.x > ballX) { ballposition.x -= ballspeed; } if(ballposition.x < ballX) { ballposition.x += ballspeed; } if(ballposition.y > ballY) { ballposition.y -= ballspeed; } if(ballposition.y < ballY) { ballposition.y += ballspeed; } } 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; while(!gameover) { srand(time(NULL)); do { ballX = randx; ballY = randy; }while(abs(oldx-ballX) > 300 && abs(oldy-ballY) > 300); oldx = ballX; oldy = ballY; 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.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] The point of the game is just to click on the moving ball which causes increase of its speed.
  8. Hi, I've created the small program where the ball is moving all the time on the screen to random coordinates made like that: [source lang="cpp"]srand(time(NULL)); do { ballX = randx; ballY = randy; }while(abs(oldx-ballX) > 300 && abs(oldy-ballY) > 300); oldx = ballX; oldy = ballY;[/source] It's included in while(!gameover) function. That moves the ball: [source lang="cpp"]void moveBall() { if(ballposition.x > ballX) { ballposition.x -= ballspeed; } if(ballposition.x < ballX) { ballposition.x += ballspeed; } if(ballposition.y > ballY) { ballposition.y -= ballspeed; } if(ballposition.y < ballY) { ballposition.y += ballspeed; } }[/source] The only problem is that the ball is moving to the right coordinates X and Y, then sometimes stays for a moment and then another move and so on. When I try to delete the "srand(time(NULL));" it causes the ball to move really fast in each direction but no lag. How can I fix this?
  9. [SDL/C++] Asking player for text

    So if I would like to make asking for name should I do states for all characters? How it will look?
  10. Hi guys. I want to do in my SDL Game asking for text. For example when I'll come close to the doors, there will be a code and u will have to type it by a keyboard. How can I do it?
  11. Thank You very much guys ;)
  12. Hello, I'm writing a RPG game, which will be actually the horror RPG. I've done the animation for player and all that based stuff, but now I wanted to do buttons on the left corner of the screen: [img]http://i47.tinypic.com/27yvm76.png[/img] For example if I press by mouse "QUESTS", this appears on the center of the screen: [img]http://i45.tinypic.com/2n9xoyb.png[/img] And then it's waiting for me to press for example ESCAPE to turn the picture off. If I click options, I would like to have for example such buttons on screen like: CONTROLS, BACKGROUND MUSIC, FULLSCREEN or BACK: [img]http://i48.tinypic.com/f2u6c.png[/img] And if I click CONTROLS, the last picture disappears and this one is on the screen: [img]http://i48.tinypic.com/2zdog9e.png[/img] Then it's waiting for me to press ESCAPE or another button on screen, for example BACK. [b]Here's the part of code:[/b] [source lang="cpp"]if(event.type == SDL_MOUSEMOTION ) { mouseX = event.motion.x; mouseY = event.motion.y; } if(event.type == SDL_MOUSEBUTTONDOWN ) { //Buttons if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonquestslocation.x && mouseX <= buttonquestslocation.x + buttonquestslocation.w ) && ( mouseY >= buttonquestslocation.y && mouseY <= buttonquestslocation.y + buttonquestslocation.h ) ) { Mix_PlayChannel(-1, paper, 0); SDL_BlitSurface(levelonemap, NULL, screen, &paperlocation); SDL_Flip(screen); //Here's the place where I want the screen to wait until the player will press the button. I didn't know how to do that so i put a Delay. SDL_Delay(3000); } if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonoptionslocation.x && mouseX <= buttonoptionslocation.x + buttonoptionslocation.w ) && ( mouseY >= buttonoptionslocation.y && mouseY <= buttonoptionslocation.y + buttonoptionslocation.h ) ) { SDL_BlitSurface(options, NULL, screen, &optionslocation); SDL_BlitSurface(buttoncontrols, NULL, screen, &buttoncontrolslocation); SDL_Flip(screen); Mix_PlayChannel(-1, paper, 0); //Now player must choose between: CONTROLS, BACKGROUND MUSIC, FULLSCREEN or BACK } if(event.button.button == SDL_BUTTON_LEFT && ( mouseX >= buttonexitlocation.x && mouseX <= buttonexitlocation.x + buttonexitlocation.w ) && ( mouseY >= buttonexitlocation.y && mouseY <= buttonexitlocation.y + buttonexitlocation.h ) ) { SDL_Quit(); return 0; } }[/source]
  13. Ok, thank you both guys ;)
  14. The whole map is 1280x720, so adding 1 to it will I think don't work. I just want that if the food spawns, and after I picked it up, it is checking, if the difference between new x, old x, new y, old y is more than for example 100. If not, take another random. Is that possible?