Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 67 68 [69] 70 71 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100581 times)

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1020 on: February 23, 2013, 04:05:57 am »

The file I linked is the definition for a class named Menu. self.whatever in this instance refers to methods and members that are contained within Menu objects.

Basically, if you're not doing OO programming you don't need to worry about using self.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: Programming Help Thread (For Dummies)
« Reply #1021 on: February 23, 2013, 07:45:16 am »

You know, the more I think about it the more I've realised I dislike the "standard" OOP approach. At least in C#.

I've fallen in love with the style we use at work, where a class tends to just contain a single function (two functions if you're unlucky) and often has absolutely no state to speak of. You inject dependencies in at construction the function uses to do it's work. Very testable, very understandable and, as it turns out, quite efficient.

Lately I've seen a lot of simplicity winning over ugly hacks in the performance department. Whilst we got a "quick win" performance increase in introducing caching to some legacy code, but by the end of a big refactoring of the code to bring it up to standard and make it more workable we actually found it actually performed better without that caching.
« Last Edit: February 23, 2013, 09:22:23 am by MorleyDev »
Logged

GalenEvil

  • Bay Watcher
    • View Profile
    • Mac-Man Games
Re: Programming Help Thread (For Dummies)
« Reply #1022 on: February 23, 2013, 09:24:17 am »

@MorleyDev: where can I learn more about this style? I am all for testable, understandable, and efficient code :D Is it delegate functionality being passed in at construction, and/or generic typing?
Logged
Fun is Fun......Done is Done... or is that Done is !!FUN!!?
Quote from: Mr Frog
Digging's a lot like surgery, see -- you grab the sharp thing and then drive the sharp end of the sharp thing in as hard as you can and then stuff goes flying and then stuff falls out and then there's a big hole and you're done. I kinda wish there was more screaming, but rocks don't hurt so I guess it can't be helped.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: Programming Help Thread (For Dummies)
« Reply #1023 on: February 23, 2013, 09:59:18 am »

@MorleyDev: where can I learn more about this style? I am all for testable, understandable, and efficient code :D Is it delegate functionality being passed in at construction, and/or generic typing?

It's fundamentally just applying the Single Responsibility Principle. If a class has public multiple functions, it probably has multiple responsibilities.

Dependency Injection is done in the constructor. Using the constructor is the simplest solution and if there is no state, there's little to no overhead from that construction. At the moment if we simply need to pass a factory function in to construct an object in code, it's done with a delegate (and using Func<IInterface> in the constructor).
« Last Edit: February 23, 2013, 10:07:33 am by MorleyDev »
Logged

Urist_McArathos

  • Bay Watcher
  • Nobody enjoys a good laugh more than I do.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1024 on: February 23, 2013, 11:31:33 am »

Anyway, does anyone know what I'm doing wrong?  I'm trying to get the class to return a rect surface (so I can have the mouse's position taken and see if it intersects with the rect's coordinates), but it doesn't seem to work.

EDIT: I can't seem to get it to create a button either; there's no obvious problems with the syntax I can find, but I can't get it to actually create a button from the module I'm making.  I'm not sure what I need to fix.
« Last Edit: February 23, 2013, 12:08:39 pm by Urist_McArathos »
Logged
Current Community/Story Projects:
On the Nature of Dwarves

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1025 on: February 23, 2013, 12:24:11 pm »

"doesn't seem to work" is very nondescript. You should really mention any error messages or behaviour you see that differ from the expected.

I know very little about python specifically, but from what I can see the Button class stores no information, Button1 never receives any useful assignment and the call to mouse_over is done with incorrect arguments.

I'm not entirely sure what the menuButton function is meant to accomplish, but it doesn't seem to do anything other than draw the button to the windowSurface, then return a copy of (or reference to, depending on how pyton works with arguments and return values) the same windowSurface.
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1026 on: February 23, 2013, 01:02:03 pm »

I should have taken the time to respond to the question asked rather than hocking my wares.

Spoiler (click to show/hide)
drawText isn't used anywhere? Any reason it's not a part of the class?

Quote
Spoiler (click to show/hide)
These two methods should be refactored. Button initialization should be moved into __init__ and button drawing should be moved into a different function, likely named draw. The variables inherent to a Button should be turned into instance members.

As it is used currently, you're returning a surface object but never assigning it to anything, so it is immediately discarded.

Quote
Spoiler (click to show/hide)

This  could be replaced with this. If everything is properly factored later, this could be a parameter-less function. buttonSurface should be a data member of Button and the mouse coords are easily available from everywhere.

Quote
Spoiler (click to show/hide)

If this is an explanation, shouldn't this be commented out as well? Button1 doesn't exist yet.

Quote
Spoiler (click to show/hide)

If you refactor create_button and menuButton as I suggested, you could create one button before the loop and not have to repeatedly create them within.

I recommend reading up on an introduction to object-oriented programming in Python.
Logged

Urist_McArathos

  • Bay Watcher
  • Nobody enjoys a good laugh more than I do.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1027 on: February 23, 2013, 02:10:45 pm »

Wow...I'm probably in over my head here.  I don't even know what __init__ is, and didn't realize I needed to know about object-oriented programming before trying this.
Logged
Current Community/Story Projects:
On the Nature of Dwarves

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1028 on: February 23, 2013, 02:17:54 pm »

__init__ is a method just like any other. When you call Button.Button() in your code, it actually calls the __init__ method of your Button class. Since you don't have an __init__ method, it calls a parameterless one that results in an empty object.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1029 on: February 25, 2013, 11:43:19 am »

It's fundamentally just applying the Single Responsibility Principle. If a class has public multiple functions, it probably has multiple responsibilities.

Dependency Injection is done in the constructor. Using the constructor is the simplest solution and if there is no state, there's little to no overhead from that construction. At the moment if we simply need to pass a factory function in to construct an object in code, it's done with a delegate (and using Func<IInterface> in the constructor).

If you have single function objects with no state I have to wonder why your using objects at all? Seems much closer to a functional programming style than classic OOP. Obviously this doesn't apply to objects where you have state, encapsulation is a good thing in that case.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: Programming Help Thread (For Dummies)
« Reply #1030 on: February 25, 2013, 04:51:09 pm »

If you have single function objects with no state I have to wonder why your using objects at all? Seems much closer to a functional programming style than classic OOP. Obviously this doesn't apply to objects where you have state, encapsulation is a good thing in that case.

Well yeah, the functional programming style is very neat. As for why classes, well the classes are largely for the dependency injection. Personally I'd regard state as an "evil", just a sometimes necessary one. After all, what's simpler: To hide the moving parts, or to remove them? Of course it helps to be working on a system with very well defined inputs and outputs so may be more applicable to business than game development.
Logged

Urist_McArathos

  • Bay Watcher
  • Nobody enjoys a good laugh more than I do.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1031 on: February 25, 2013, 06:35:21 pm »

Thanks for the advice, Mephisto.  I'm still having trouble getting the results I want, but I think I'm closer.  I'll post more code and more specifics if I don't figure the rest out soon, but otherwise thanks again everyone.
Logged
Current Community/Story Projects:
On the Nature of Dwarves

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1032 on: February 27, 2013, 11:51:02 am »

You know, the more I think about it the more I've realised I dislike the "standard" OOP approach. At least in C#.

I've fallen in love with the style we use at work, where a class tends to just contain a single function (two functions if you're unlucky) and often has absolutely no state to speak of. You inject dependencies in at construction the function uses to do it's work. Very testable, very understandable and, as it turns out, quite efficient.

Lately I've seen a lot of simplicity winning over ugly hacks in the performance department. Whilst we got a "quick win" performance increase in introducing caching to some legacy code, but by the end of a big refactoring of the code to bring it up to standard and make it more workable we actually found it actually performed better without that caching.


In that case, couldn't you completely drop the concept of classes and use closures to encapsulate the state? Or do you still need inheritance?
Logged

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1033 on: February 27, 2013, 05:15:41 pm »

Hey guys,

Uber-n00b here. I've started learning c++ to eventually do some fun things with it(even if it is in a few years). Anyway, to try and improve, I've started working on a text based adventure and I've hit a caveat (I've searched numerous places before coming here, because I bet this has been asked a million times). The problem is, in the game you have the option to press 0 for an option and 1 for another option and I'm having a difficult time trying to code in " when you press this, this displays". At first I tried 'if' and 'else if' statements but that didn't seem like that the right option so here is what I have.

Code: [Select]
#include <iostream>
using namespace std;
int main()
{
    int a,b;
  //  a= 1;
  //  b= 0;
cout << "Press 'Enter' to reveal the next sentence";
cin.ignore();
cout << "When a sentence is revealed, press the bracketed number to choose that option";
cin.ignore ();
cout << "You enter a dark room with 2 hallways,\none to your left (0) and one to your right(1)\n";
cin.ignore();
cin >> a;
cout << " You are in the right room ";
cin >> b;
cout << " You are in the left room";
}


Again, all my learning has been on the interwebs so my code is probably really inefficient and badly written but whatever...Help?
Logged
I write music for video games!
www.joereynoldsaudio.com

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1034 on: February 27, 2013, 05:37:57 pm »

You're trying to have branching behavior, so you're almost surely going to need an if.

The bigger problem here is that cin.ignore() is gobbling the 0/1 that you're trying to read. In the first two cases, cin.ignore() is reading in the 'Enter' key and ignoring it. In the third, it's reading and ignoring either the 0 or the 1 then reading another number after that.

Try this:

Code: [Select]
#include <iostream>
using namespace std;
int main()
{
  int a;

  cout << "Press 'Enter' to reveal the next sentence";
  cin.ignore();

  cout << "When a sentence is revealed, press the bracketed number to choose that option";
  cin.ignore ();

  cout << "You enter a dark room with 2 hallways,\none to your left (0) and one to your right(1)\n";
  cin >> a;

  if (a == 1) {
    cout << " You are in the left room ";
  } else {
    cout << " You are in the right room";
  }
}

On a related (and un-solicited) note, if you really are starting out from scratch I would suggest not starting with C++. It's definitely the industry standard, but that's not because it's a particularly good / easy language to learn. It's just because that's what always been used and it's what every one knows. Just about everything you learn in any language can be transferred over, so you're not losing anything starting with something else. I would personally suggest Python. The code you had above would look something like this:

Code: [Select]
print "Press 'Enter' to reveal the next sentence";
raw_input()

print "When a sentence is revealed, press the bracketed number to choose that option";
raw_input()

print "You enter a dark room with 2 hallways,\none to your left (0) and one to your right(1)\n";
choice = int(raw_input())

if choice == 0:
    print " You are in the left room ";
else:
    print " You are in the right room";
« Last Edit: February 27, 2013, 05:40:08 pm by JanusTwoface »
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession
Pages: 1 ... 67 68 [69] 70 71 ... 91