Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 70 71 [72] 73 74 ... 91

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

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1065 on: May 01, 2013, 10:52:39 pm »

I am assuming you are talking about 2D graphics, so I would suggest SDL/SFML for the library. Both libraries have bindings for a variety of languages, so you have some choice there.
Logged

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1066 on: May 01, 2013, 10:58:25 pm »

Depends on what you know. If you know any programming languages decently well, use what you know. It's rare that there's a language that doesn't already have a roguelike tutorial for it. (I'm writing a series for Racket, for example).

If you don't, Python is what I would suggest to pick up. It's one of the easier languages to learn (IMO) and you can go from 0 to game relatively easily.

Without more information though, that's really about all I can offer.
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

NobodyPro

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1067 on: May 02, 2013, 03:47:09 am »

Not a programming problem but: Why do I get redirected to the allkpop forums every time I try to access the last two pages?
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1068 on: May 02, 2013, 06:55:02 am »

Malware in some form or another.
Logged

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1069 on: May 09, 2013, 11:02:52 am »

Here's something that's been bugging me lately. How do I reference other classes within a class?
I've made a short crude example to show what I mean but this pretty much extends to all my other projects, is there any way to do this aside from making all the variables global?

Code: [Select]
class Shop ():
    def __init__(self,name,tax):
        self.name = name
        self.tax  = tax
        print ("Shop Name :",name)
    def display_stats (self):
        print ("Tax :",self.tax,"%")
    def display_items (self, weapon):
        HERE I WOULD LIKE TO DISPLAY THE WEAPONS THAT HAVE
        GENERATED FROM THE CLASS Weapons()
   
class Weapon():
    def __init__(self,name,price):
        self.name  = name
        self.price = price
       
brone_sword = Weapon ("Bronze Sword",140)
place = Shop(shop,4.5)
place.display_stats()
place.display_items(Weapon)

Would I need to use the return function?
I've been without internet for 2 weeks so my already bad programming has gotten even worse  :-[
Logged
I write music for video games!
www.joereynoldsaudio.com

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1070 on: May 09, 2013, 12:13:13 pm »

Here's something that's been bugging me lately. How do I reference other classes within a class?
I've made a short crude example to show what I mean but this pretty much extends to all my other projects, is there any way to do this aside from making all the variables global?

Code: [Select]
...
    def display_items (self, weapon):
        HERE I WOULD LIKE TO DISPLAY THE WEAPONS THAT HAVE
        GENERATED FROM THE CLASS Weapons()
...
place.display_items(Weapon)

That's not possible unless you do something like this, but I wouldn't recommend it.

Here is a naive implementation that does no checking of anything and accepts whatever is thrown at it (meaning: don't use this as-is, it's bad). Also, we use Python 2.7 at work so I stripped out parens on the prints. I also used string formatting.
Spoiler (click to show/hide)

If I remember after work and I'm not dead on my feet, I'll try better. I have a love/hate relationship with YAML and other markup languages, so I may end up using one.

Logged

Killjoy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1071 on: May 09, 2013, 01:16:33 pm »

I've made a short crude example to show what I mean but this pretty much extends to all my other projects, is there any way to do this aside from making all the variables global?
Go our about python lists. Or read a book about python, I heard it helps some people. Either way.

Firstly, if you want to put something into a string use the "+" sign to concatenate strings. This means, if you have a variable health, that is a number first cast it to a string, then append it to the string:
"hp: " + str(health) + " something more"
DONT do ("hp", health, "blah")
Because that creates a tuple, not a string.
I use the .format method, because it makes string formatting easier, but it might be a little to early for you to care about such things.

Secondly, here is an example of what you want.
Code: [Select]

class Shop():
    def __init__(self,name,tax):
        self.name = name
        self.tax  = tax
        self.weapons = []
        print ("Shop Name: {}".format(name))
    def display_stats (self):
        print ("Tax : {} %".format(self.tax))
    def display_items (self):
        for weapon in self.weapons:
             print(weapon)
    def add_item(self, weapon):
        self.weapons.append(weapon)
   
class Weapon():
    def __init__(self,name,price):
        self.name  = name
        self.price = price
    def __str__(self): #Print automatically calls __str__ method to convert a object to a string.
        return "Name: {} \n Price: {}".format(self.name, self.price)
       
bronze_sword = Weapon ("Bronze Sword",140)
place = Shop("shop", 4.5)
place.display_stats()
place.add_item(bronze_sword)
place.display_items()
Logged
Merchants Quest me programming a trading game with roguelike elements.

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1072 on: May 09, 2013, 02:40:27 pm »

Firstly, if you want to put something into a string use the "+" sign to concatenate strings. This means, if you have a variable health, that is a number first cast it to a string, then append it to the string:
"hp: " + str(health) + " something more"
DONT do ("hp", health, "blah")
Because that creates a tuple, not a string.
I use the .format method, because it makes string formatting easier, but it might be a little to early for you to care about such things.

Caveat: To concatenate things with +, both sides have to be strings. So "answer: " + 42 will not work. You can use the builtin function str to convert things to strings: "answer: " + str(42).

Personally, I use the % operator, but again that's a bit more to pick up if you're not used to printf style formatting.
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

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1073 on: May 09, 2013, 02:54:55 pm »

Aight, I got some issues up in dis here bee-yotch.

Whenever I run this method my game freezes, when I attempt to debug it freezes. I'm really not sure on this one, what it's supposed to do is read 196 files like:
Spoiler (click to show/hide)
take all those numbers in each one, take the number with the most occurrences and store it until all 196 files are read. Then it should take all those numbers and print them into 1 file that looks like the one above.

Code: [Select]
  }
   
    public void worldSaveConvert() throws IOException {
       
        int worldSaveArr[] = new int[196];
       
        for(int wsa = 0; wsa < 196; wsa++) {
           
            int regionInc2 = 0;
            Scanner localOpen = new Scanner(new File("C:\\Users\\Daniel\\Desktop\\Aldaron\\mapLocal\\MapSave\\" + localPath + "\\" + regionInc2 + ".txt"));
            String all = "";
            regionInc2++;
       
            while(localOpen.hasNextLine() == true) {
           
                all += localOpen.nextLine();
           
            }
           
            localOpen.close();
 
            String allToInt[] = all.split("\\.");
            int allToInt2[] = new int[196];
       
            for(int a = 0; a < 196; a++) {
           
                allToInt2[a] = Integer.parseInt(allToInt[a]);
           
            }
       
            int zeroc = 0;
            int onec = 0;
            int twoc = 0;
            int threec = 0;
            int fourc = 0;
       
            for(int a = 0; a < allToInt2.length; a++) {
           
                switch(allToInt2[a]){
               
                    case(0):
                        zeroc += 1;
                        break;
                    case(1):
                        onec += 1;
                        break;
                    case(2):
                        twoc += 1;
                        break;
                    case(3):
                        threec += 1;
                        break;
                    default:
                        fourc += 1;
                        break;
               
                }
           
            }
       
            if(zeroc > onec && zeroc > twoc && zeroc > threec && zeroc > fourc) {
               
                worldSaveArr[wsa] = 0;
                break;
               
            }
            else if(onec > zeroc && onec > twoc && onec > threec && onec > fourc) {
               
                worldSaveArr[wsa] = 1;
                break;
               
            }
            else if(twoc > zeroc && twoc > onec && twoc > threec && twoc > fourc) {
               
                worldSaveArr[wsa] = 2;
                break;
               
            }
            else if(threec > zeroc && threec > onec && threec > twoc && threec > fourc) {
               
                worldSaveArr[wsa] = 3;
                break;
               
            }
            else if(fourc > zeroc && fourc > onec && fourc > twoc && fourc > threec) {
               
                worldSaveArr[wsa] = 4;
                break;
               
            }
           
        }
       
        FileWriter world = new FileWriter("C:\\Users\\Daniel\\Desktop\\Aldaron\\mapWorld\\MapSave\\" + worldPath + ".txt");
        PrintWriter printWorld = new PrintWriter(world);
       
        int inc1 = 0;
       
        for(int write = 0; write < 14; write++) {
           
            String line = "";
           
            for(int write2 = 0; write2 < 14; write++) {
               
                line += worldSaveArr[write2] + ".";
               
            }
           
            printWorld.println(line);
            inc1 += 14;
           
        }
       
        world.close();
        printWorld.close();
       
    }

for reference, the program IS taking from and printing to the right locations. However, it does NOT print the correct information, in fact it doesn't print anything, it just creates an empty file.

I am, of course, happy to try and provide more info. It's probably something stupid anyways. As always, thanks for any help!
Logged
This conversation is getting disturbing fast, disturbingly erotic.

Killjoy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1074 on: May 09, 2013, 03:45:44 pm »

Personally, I use the % operator, but again that's a bit more to pick up if you're not used to printf style formatting.
A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop.
http://docs.python.org/release/3.0.1/whatsnew/3.0.html#pep-3101-a-new-approach-to-string-formatting

So yeah, start using .format(), "%" might not work in the future.
Logged
Merchants Quest me programming a trading game with roguelike elements.

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1075 on: May 09, 2013, 10:38:13 pm »

So yeah, start using .format(), "%" might not work in the future.
Well... technically when I write Python I still use Python 2.7. None of the systems that I work on (but don't have control over) have updated to Python 3 yet; it was already hard enough to get them to update that far... (I do have Python 3 installed for my user account, but it doesn't always play nicely.) Also there are still a few libraries that I depend on which don't have a Python 3 version. Sigh. (Also I'm still a bit bitter about the lambda thing...)

That being said, the format syntax does look nice (much nicer than %'s), although % has the advantage of using the same format that just about everything else out there uses (for better or for worse). Maybe I'll give it a try for the web project that I'm writing at the moment.

Although most of what I write nowadays is in Racket anyways, so it mostly doesn't matter. :)
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

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1076 on: May 10, 2013, 11:09:33 am »

...
Your code could be significantly tightened up and I'd be happy to show you if you'd like, but for the actual question you're asking, here's your problem:

for(int write2 = 0; write2 < 14; write++) {
(inner loop, near the end) should be:
for(int write2 = 0; write2 < 14; write2++) {

You're incrementing write instead of write2 and thus getting an infinite loop.
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

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1077 on: May 10, 2013, 01:18:43 pm »

Thanks man, I appreciate it!

And sure neat code is best code, so if you have time Id love some sort of demonstration. You can pm me or post here if you want.
Logged
This conversation is getting disturbing fast, disturbingly erotic.

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1078 on: May 10, 2013, 01:54:32 pm »

Here's what I came up with: pastebin

Theoretically this does the right thing. If I understand correctly, you have a 14x14 grid of 14x14 grids and you want to take the most common number from each small grid and put it in the large one. Basically a world map / region map thing, yes?

The major tricks / revisions:
- Use Scanner's useDelimiter function to skip the dots and whitespace, then nextInt will just work™
- Use an array for the read values, so that we can loop to find the most common
- Keep the output file open and write as we go so we don't have to store the array of values
- Avoid magic numbers (like a 14x14 grid size); it'll save a lot of heartache if you decide to change them later
- Comments! I probably over comments, but it's amazingly helpful when you have to read your own code even a few days later

The rest is mostly style stuff. If you have any questions, let me know.
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

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: Programming Help Thread (For Dummies)
« Reply #1079 on: May 10, 2013, 05:18:45 pm »

None.

« Last Edit: May 11, 2013, 08:04:08 am by kytuzian »
Logged
Pages: 1 ... 70 71 [72] 73 74 ... 91