Bay 12 Games Forum

Please login or register.

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

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

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1050 on: March 12, 2013, 09:28:39 am »


Technically, you only need the comma to create tuples, but it's good practice to use the parenthesis as well.

Do you have any examples you're confused about it in particular?

You just said it yourself, why is it good practice? :D

I have found another example,

if not(a):
  print ("a is false")

if not a:
  print ("a is false")

Work exactly the same, and I'm not sure what putting the 'a' in parentheses does. I just want to make sure so that I can actually understand why something might not work
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 #1051 on: March 12, 2013, 09:51:51 am »

You just said it yourself, why is it good practice? :D
:)

Mostly, it's good practice because that's the standard way to do it (like that's how most code will be written). But in a lot of cases it's not necessary:

Code: [Select]
>>> x = 1, 2, 3
>>> x
(1, 2, 3)
>>> y = (1, 2, 3)
>>> y
(1, 2, 3)
>>> x == y
True

In simple code like this, it doesn't actually matter if you have the parentheses or not. But one thing to note is that tuples will always be printed out with them (as you can see in the example above). That's one reason that it's generally best to put them when defining a tuple, so that your input looks like the output you'd get. The other is because in some more complicated expressions, it actually matters if you have the parentheses or not. For example:

Code: [Select]
>>> 1, 2 + 3, 4
(1, 5, 4)
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)

Addition is overloaded to concatenate tuples, that's what's going on in the second example. But addition also has a higher precedence, so when you're evaluating the above expressions, 2+3 is done first unless you use parentheses to make it go in the other order. It's the same as the difference between 1+2*3+4 => 11 and (1+2)*(3+4) => 14.

I have found another example,

if not(a):
  print ("a is false")

if not a:
  print ("a is false")

Work exactly the same, and I'm not sure what putting the 'a' in parentheses does. I just want to make sure so that I can actually understand why something might not work

In this case, the parentheses don't do anything. not isn't a function, otherwise you would need them. It's actually an operator, like +, only it's unary so it only takes one argument. It's basically the same thing as using the other unary operator: -. You can write -5 or -(5) and both will evaluate to the same thing. In this case, there's really no reason to have the parentheses there.

When you would want them is if a were a more complicated expression. For example:

Code: [Select]
>>> not True and False
False
>>> not (True and False)
True

Because and has a lower precedence than not, the first example should be read as (not True) and False => False and False => False. The second forces the evaluation of and first though, so you get not (True and False) => not False => True. Granted, it's a really contrived example and it took me about 10 minutes to actually come up with one that works, but it does show that such examples are at least possible. :)

Note: You can see the entire precedence table in Section 5.15 on this page: 5. Expressions. The further an operator is down the table, the higher precedence it has (and the earlier it would get evaluated). Personally, I always forget what order and / or go in, so I'll generally over-parentheses there, but otherwise, it gets to be pretty intuitive.
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 #1052 on: March 12, 2013, 08:07:17 pm »

So problably something stupid I've done again, but I just can't seem to figure it out, I have finally fixed my problems with getting my map to render, but now I'm having trouble parsing the file for some reason, it's giving my an outofbounds exception (for reference, both mapWorld.mapHeight and .mapWidth are equal to 14), take a look (if you don't mind!):

Code: [Select]
package aldaron;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class preRenderLoad {
   
    private static String test[];

    public void preRenderLoad(){
    }
   
    public static mapWorld[][] load() {
   
    mapWorld[][] tilesAct = new mapWorld[mapWorld.mapHeight][mapWorld.mapWidth];
       
    try (Scanner load = new Scanner(new File("C:\\Users\\Daniel\\Desktop\\Aldaron\\mapWorld\\MapSave\\default.txt"))) {
            String loadInstance;
           
            for(int sizeCheck = 0; sizeCheck < mapWorld.mapHeight; sizeCheck++) {
               
                //sets the string to the line
                loadInstance = load.nextLine();
                test = loadInstance.split(".");
               
                for(int sizeCheck2 = 0; sizeCheck2 < mapWorld.mapWidth; sizeCheck2++) {
                   
                    //seperates #s via tokenization; stores in wordCurrent; changes it to an int
                    //StringTokenizer tileTypeGet = new StringTokenizer(loadInstance, ".");
                    //String wordCurrent = tileTypeGet.nextToken();
                    //int wordToInt = Integer.getInteger(wordCurrent);
                   
                    //////////////////////////////////////////////////////////////////////////
                    //alternative implementation
                    int wordToInt2 = Integer.parseInt(test[sizeCheck2]);               
                    //int wordToInt3 = load.nextInt();
                   
                    //sets each tile's constructor; gives terrain and render type
                    tilesAct[sizeCheck][sizeCheck2] = new mapWorld(wordToInt2);
                    //tilesAct[sizeCheck][sizeCheck2] = new mapWorld(wordToInt3);
                    //tileLoader[sizeCheck][sizeCheck2] = test[sizeCheck2];
                   
                }
            }
        }
        catch (IOException e) {
           
            for(int y = 0; y < mapWorld.mapHeight; y++) {
           
                for(int x = 0; x < mapWorld.mapWidth; x++) {   
                 
                    tilesAct[y][x] = new mapWorld(3);
                   
                }
            }
         }
   
         return tilesAct;
   
    }
   
}

It's a bit cluttered, sorry, but before I gave this code segment its own class, I was working out some other problems.

anyways, here's the file it's reading (which is the format all files it'll have to read will be in):

Spoiler (click to show/hide)

thanks for any help!
Logged
This conversation is getting disturbing fast, disturbingly erotic.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1053 on: March 12, 2013, 08:14:27 pm »

The split function of Java's String class splits strings in such a way that ".1.1.1.1.1.1.1.1.1.1.1.1.1.1" will become
"","1","1","1","1","1","1","1","1","1","1","1","1","1","1". You should get rid of the first period in each line.
Logged

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1054 on: March 13, 2013, 07:10:44 am »

Alright, but the problem continues to persist, looking at the variable values during debugging, I noticed that test appears to only have a length of 0 at the line that contains wordToInt2. I don't know if that's it.
Logged
This conversation is getting disturbing fast, disturbingly erotic.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1055 on: March 13, 2013, 07:28:08 am »

What value does loadInstance get assigned to?
Logged

Berserker

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1056 on: March 13, 2013, 11:19:29 am »

If I remember correctly, the first argument of String.split is a regular expression, and "." would mean splitting at any character.

Try escaping the dot character like this:
Code: [Select]
loadInstance.split("\\.");
This should work also if you import java.util.regex.Pattern first:
Code: [Select]
loadInstance.split(Pattern.quote("."));
Logged

Urist McScoopbeard

  • Bay Watcher
  • Damnit Scoopz!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1057 on: March 13, 2013, 02:28:17 pm »

even if I remove the first "." then the problem still persists.

...But i'll also try what you said berserker

EDIT:

ahaha! IT LIVES, this is the first time since I started debugging my map rendering that my map renders properly(and now I can edit the map!)

sub-edit: don't mind the weird debug stuff on the right!

Spoiler (click to show/hide)

thanks, I can finally go forward with this project, im so excited! Map generation up next!
« Last Edit: March 13, 2013, 02:44:24 pm by Urist McScoopbeard »
Logged
This conversation is getting disturbing fast, disturbingly erotic.

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1058 on: March 14, 2013, 01:03:15 pm »

EDIT: NEVERMIND, I FIGURED IT OUT!

Okay, so I'm trying to declare a couple of vectors inside a class. Some some straight up told me how to do it and I'm not sure what exactly is going on so it's kinda a no no, but I'm pressing on regardless because I know I'm just making a nice dynamic array.

Anyways, declaring one works but when I try and declare another the same way it naturally tells me I cant overload the class.

Now, I know this code doesnt work, and I know why it doesnt work, but I dont know how to actually make the declaration.

Code: [Select]
struct ObjectKing
{

    ObjectGeneric *objectsG;
    ObjectGeneric *objectsA;
    ObjectKing() : objectsG(new ObjectGeneric[NUM_OBJ]) {}
    ObjectKing() : objectsA(new ObjectActor[NUM_OBJ]) {}

};

I know I need to put the objectsA(new ObjectActor[NUM_OBJ]) somewhere, but I dont know where.

ANSWER:
Code: [Select]
ObjectKing() :objectsG(new ObjectGeneric[NUM_OBJ]), objectsA(new ObjectActor[NUM_OBJ]) {}
« Last Edit: March 14, 2013, 01:06:27 pm by thobal »
Logged
Signature goes here.

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1059 on: March 14, 2013, 01:31:42 pm »

Right, so Lua's I/O stuff frustrates me to no end. Can someone give me a quick rundown of how the thing works, and how relative paths (starting from the location of your lua file) work? I want to export chunks of the code in its own small files, and so far my failures have been nothing but miserable.
Logged
Girls are currently preparing signature, please wait warmly until it is ready.

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1060 on: March 20, 2013, 04:34:16 pm »

And again....

Okay, so, in my quest to make a little simulation game where a dog eats an animal when it reaches a certain hunger, I have decided (quite obviously) that I need to learn about pathfinding. Naturally I'll be using the A* algorithm as that's the one that I've read about and can kind of understand quite well. Before I even get to that though, I'm going to need to draw a grid of 10x10 squares to do my pathfinding on. At the moment,the grid itself is not working. I think it's due to the placement of the statements within the while loop or maybe just their indentation.

Theoretically right now it should print out 10 rectangles from left to right. They should also look like one big white line.
Once I have this row of squares done, then I will work on the columns.

Maybe it's just because I'm tired but I cannot for the life of me see the error in this code.

Edit: Feel free to point out any bad practices so that I can learn the proper way :D

Code: [Select]
#Draw a 10x10 grid
import pygame
pygame.init()

#Window info
size = [255,255]
screen = pygame.display.set_mode(size)
pygame.display.set_caption ("F G H")

#Colours
black = (0,0,0)
white = (255,255,255)

#coordinates
x=0
y=0
x_change = 10
y_change = 10

#Sizes
height = 20
width  = 20
margin = 5

done = False

clock = pygame.time.Clock()

#Main loop
while done == False:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            done=True

#Drawing commands
    screen.fill(black)
    for column in range (10):
        pygame.draw.rect (screen,white,[column,y,height,width,])
    pygame.display.flip()
   
clock.tick(20)
pygame.quit
 
« Last Edit: March 20, 2013, 04:43:51 pm by professorlamp »
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 #1061 on: March 21, 2013, 12:44:14 am »

Hmm. A few comments:
- the order for the third argument to rect is [x, y, width, height], so swap the third and fourth (this is the standard order for most drawing frameworks)
- stylistically, the '#Drawing commands' comment should be indented
- the clock.tick(20) only happens after you finish the loop; if you want it to happen during the loop it needs to be indented
- pygame.quit needs parentheses to actually call it as a function (not that it matters, it will shut down when the program does)

But the actual problem that you're having is that you aren't correctly setting the x coordinates. You want to draw the rectangles at (0, 0), (10, 0), (20, 0), ... since each is 10 wide, correct? So you need to multiple column by the width otherwise you'll get 10 squares that are mostly overlapping.

So try changing line 38 to this (also reorders the parameters correctly):
Code: [Select]
pygame.draw.rect(screen,white, [column * width, y, width, height])
To see the rectangles better, you can do this:
Code: [Select]
pygame.draw.rect(screen,white, [column * width, y, width - 1, height])
Alternatively, you could use the three argument version of range(minimum, maximum, step). It starts at min and adds step instead of 1 each time until it's greater or equal to maximum. That would look something like this:

Code: [Select]
for x in range(0, width * 10, width):
pygame.draw.rect(screen,white, [x, y, width, height])
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

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1062 on: March 25, 2013, 08:30:09 pm »

Thanks for the reply Janus.
I noticed the problem in the morning before I came on here, guess I was just tired  :-[

Just a(nother) quick question.

How can I get Python to remember a random value? I think this is easily described by my code
Code: [Select]
    print ("You have defeated the Spider")
    print ("You have found", random.choice(ara_item),item_list.append.(The random ara_item that previously generated),"!")
    print (item_list)

I'm having trouble at the moment as I'm not sure how to store that random choice for future use...
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 #1063 on: March 25, 2013, 08:37:13 pm »

Store it in a variable. Something like this:
Code: [Select]
defeated_thing = random.choice(ara_item)
item_list.append(defeated_thing)
print ("You have defeated the Spider")
print ("You have found", defeated_thing, "!")
print (item_list)

Alternatively, you could append it to your list and use this syntax to extract the last item of the list:
Code: [Select]
item_list.append(random.choice(ara_item))
print ("You have defeated the Spider")
print ("You have found", item_list[-1], "!")
print (item_list)

A negative index indexes from the end of a list rather than the front.
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

bulborbish

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

Ok, I guess here is the best place to ask this question.

After having the great Idea of posting my idea for a rougelike onto the "Games you wished existed" topic (and then a couple of people solving the one excuse I had not to make it), I thought I would pop into this thread to ask a general question: What programming language/library would be best suited for making a (semi) graphical rougelike?
Pages: 1 ... 69 70 [71] 72 73 ... 91