Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need help with a problem in my game  (Read 3329 times)

0 Members and 1 Guest are viewing this topic.

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Need help with a problem in my game
« on: February 22, 2014, 12:52:50 pm »
Hi community,

at first my english is not the best because im german. If you dont understand something just ask for it and i'll try to explain. Now to my problem. Currently i am writing a game. It's very simple. For testing i use colored RectangleShapes later i will add sprites. So you have the one box for the character and you can only move up and down. 50 pixels up and 50 down until you hit the end of the window. Furthermore you have to collect coins. This coins will move from the right towards you.

To draw random coins in a "grid" i prepared a vector. So i can give each of the 12 coins a random number from 0 to 1 and then i draw one coin who has the number 1. My problem is that when i want to draw a new coin, the other one disappears. I need help with it that i have multiple coins. Here is the code you need:

The vector of the coins
Code: [Select]
vector<sf::RectangleShape>coins(12, sf::RectangleShape(coin));

Using my function drawing one coin to use whichCoin to draw other coins
Code: [Select]
whichCoin = set_coins(coins, coinPosition);

while (mainWindow.isOpen())
{
loops = 0;
if(frameClock.getElapsedTime().asMilliseconds() >= updateRate && loops < maxFrameskip)
{
frameClock.restart();
coins[whichCoin].move(-2.5, 0);
if(coins[whichCoin].getPosition().x + 50 <= mainWindow.getSize().x - 50)
{
whichCoin = set_coins(coins, coinPosition);
}

loops++;
}

... continue of the while loop
}

The function i used + the function to draw these coins
Code: [Select]
int set_coins(vector<sf::RectangleShape>& object, int position)
{
int maxSpawn = 0; //max spawnable coins per column
int setSpawn[12]; //sets the spawn for each of the 12 coins

srand(time(NULL)); //reset the previous random numbers

for(int a = 0; a <= 11; a++)
{
setSpawn[a] = rand() % 2; //set 0 or 1 for each of the 12 coins
//cout << setSpawn[a] << endl; //test to check the spawns
}

for(int i = 0; i < object.size(); i++)
{
if(maxSpawn < 1) //allow only 1 coin to spawn per column
{
if(setSpawn[i] == 1) //if the spawn of the coin is 1
{
object[i].setSize(sf::Vector2f(50, 50));
object[i].setFillColor(sf::Color::Green);
object[i].setPosition(position, i*50);

return i;
}
}

maxSpawn = maxSpawn + setSpawn[i];
}
}

void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position)
{
window.draw(object[position]);
}

I hope you can help me ._.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Need help with a problem in my game
« Reply #1 on: February 22, 2014, 03:15:34 pm »
Are you drawing all the coins you want to display? Keep in mind the rendering process for a frame is always: clear screen, draw all the wanted objects, call display().

You might want to write a complete and minimal example (see FAQ), so we know exactly what you're doing. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with a problem in my game
« Reply #2 on: February 22, 2014, 03:45:58 pm »
Thank you for the reply. I totally forgot that i clear the screen. So i tried to remove the window.clear() function but i didnt work. I can just copy my main.cpp. You dont need the header files and it's just the beginning state of the game so it's no problem for me:

http://pastebin.com/ErYDz5nA

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Need help with a problem in my game
« Reply #3 on: February 22, 2014, 03:50:29 pm »
I totally forgot that i clear the screen. So i tried to remove the window.clear() function but i didnt work

You must always call clear, draw, and display every frame. This is not optional.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with a problem in my game
« Reply #4 on: February 22, 2014, 03:55:03 pm »
Yea makes sense :D I just tested it out

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with a problem in my game
« Reply #5 on: February 26, 2014, 04:05:12 pm »
Is there no one who can help me?

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Need help with a problem in my game
« Reply #6 on: February 26, 2014, 04:22:54 pm »
So what's the problem now? In your last post you stated that you "just tested it out". This looks like everything is fine.
If it isn't, you need to describe your current problem  ;)


If it's just the same as above (only one coin drawn): Your function draw_coins() is only called once per loop and only draws a single coin. So if you want to draw all coins you just have to loop over your vector and draw all spawned coins.


By the way: There are several things you could improve.
At first and foremost: EDIT: I've just seen that the formatting is wrecked by pastebin. The "RAW paste data" is ok. Pretty formatting! Your code is a mess. Just use consistent(!!!!!) and simple indendation (2 spaces, 4 spaces, 1 tab per block, just as you like, but be consistent).
And please get rid of those #pragma region things. Although they should make the code more readable, they clutter it more than necessary.

And then, the content: Why do you have a std::vector of somehow "constant" size? Why don't you use it the way it is supposed and push_back every new object? You could for example add newly spawned coins to the vector and remove any coins that would be collected or somehow moved offscreen. This would simplify drawing a lot! Just loop through the vector (google for C++11 for-range-loops) and draw each object. Bang, there you are.

You are making your code more complicated than necessary. KISS: Keep it simple and stupid :)
It seems you have a programming background somehow based on a mashup of C and C++. Where did you learn to program? Did you read a book? There is much potential for improvement, young padawan  ;D
« Last Edit: February 26, 2014, 04:26:57 pm by MadMartin »

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with a problem in my game
« Reply #7 on: February 26, 2014, 07:35:41 pm »
So what's the problem now? In your last post you stated that you "just tested it out". This looks like everything is fine.
If it isn't, you need to describe your current problem  ;)


If it's just the same as above (only one coin drawn): Your function draw_coins() is only called once per loop and only draws a single coin. So if you want to draw all coins you just have to loop over your vector and draw all spawned coins.


By the way: There are several things you could improve.
At first and foremost: EDIT: I've just seen that the formatting is wrecked by pastebin. The "RAW paste data" is ok. Pretty formatting! Your code is a mess. Just use consistent(!!!!!) and simple indendation (2 spaces, 4 spaces, 1 tab per block, just as you like, but be consistent).
And please get rid of those #pragma region things. Although they should make the code more readable, they clutter it more than necessary.

And then, the content: Why do you have a std::vector of somehow "constant" size? Why don't you use it the way it is supposed and push_back every new object? You could for example add newly spawned coins to the vector and remove any coins that would be collected or somehow moved offscreen. This would simplify drawing a lot! Just loop through the vector (google for C++11 for-range-loops) and draw each object. Bang, there you are.

You are making your code more complicated than necessary. KISS: Keep it simple and stupid :)
It seems you have a programming background somehow based on a mashup of C and C++. Where did you learn to program? Did you read a book? There is much potential for improvement, young padawan  ;D

Thank you very much :D I will try this when I find the time. My codes are usually more complicated than necessary :D Uhm.. I learned C++ more or less from a book. I havent even finished it completely but I plan to read it from 0 in the next time. Furthermore i will try to programm more useful programs for me to improve my experience.

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Need help with a problem in my game
« Reply #8 on: February 26, 2014, 10:14:26 pm »
May I ask for the title of the book?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Need help with a problem in my game
« Reply #9 on: February 26, 2014, 10:26:52 pm »
And some hints concerning forum posts for the next time: Please use a descriptive thread title ("Need help with a problem in my game" is one of the most generic titles ever), and don't quote the full message if it's just above. You can also use [code=cpp] [/code] tags for syntax highlighting.

Thanks :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Masaru

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with a problem in my game
« Reply #10 on: February 27, 2014, 08:40:59 am »
May I ask for the title of the book?

Of course you can. But the book is written in german. The title is "C++ Lernen und professionell anwenden" which simply means sth. like "How to learn C++ and use it professionally". You can suggest me a book too, i was searching some for later anyways. I guess it's ok if it is in english.

Quote
And some hints concerning forum posts for the next time: Please use a descriptive thread title ("Need help with a problem in my game" is one of the most generic titles ever), and don't quote the full message if it's just above. You can also use
 
tags for syntax highlighting.

Thanks :)

Thank you for the tips :D

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Need help with a problem in my game
« Reply #11 on: February 27, 2014, 10:36:46 am »
Ok, I know that book, it is ok, although a little bit outdated.
If you want some suggestions: C++ Primer, written by Lippman/Lajoie/Moo. Buy the last english(!) edition, since it covers the new C++2011 standard.