Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 642 643 [644] 645 646 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 903876 times)

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9645 on: May 29, 2016, 08:17:23 pm »

<snip>

A few things. "list" is a builtin. Since you're never shadowing it, you're passing it into the call to getstats(). As you're passing the builtin "list" to your function, you're trying to subscript a type.

Second, input() returns a string. Calling "list(input())" produces a list with one item in it. You'll probably want to "split()" the returned string instead. If you don't want to use commas, input().split() will work. If you want to use commas, you could use input().split(',').
Logged

Elephant Parade

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9646 on: May 29, 2016, 08:36:14 pm »

<snip>

A few things. "list" is a builtin. Since you're never shadowing it, you're passing it into the call to getstats(). As you're passing the builtin "list" to your function, you're trying to subscript a type.

Second, input() returns a string. Calling "list(input())" produces a list with one item in it. You'll probably want to "split()" the returned string instead. If you don't want to use commas, input().split() will work. If you want to use commas, you could use input().split(',').
Man, I am terrible at naming variables. Wow. Thanks!

One other thing: It seems to be putting in the variables as strings, rather than floats.

Code: [Select]
### IMPORTS ###

#currently none

### FUNCTIONS ###

def getstats(statlist):
    HPF = int(statlist[1] * (statlist[0] + 5))
    SPF = int(statlist[2] * (statlist[0] + 5))
    ATKF = int(statlist[3] * (statlist[0] + 5))
    DEFF = int(statlist[4] * (statlist[0] + 5))
    SPAF = int(statlist[5] * (statlist[0] + 5))
    SPDF = int(statlist[6] * (statlist[0] + 5))
    SPEF = int(statlist[7] * (statlist[0] + 5))

    print("HP: " + str(HPF))
    print("SP: " + str(SPF))
    print("ATK: " + str(ATKF))
    print("DEF: " + str(DEFF))
    print("SPA: " + str(SPAF))
    print("SPD: " + str(SPDF))
    print("SPE: " + str(SPEF))

### OTHER DEFINITIONS ###

loop = 1 #this is what makes the loop work

### THE LOOP ###

while loop != 0:

    Name = str(input("NAME: "))
    statlist = list(input("LV, HP, SP, ATK, DEF, SPA, SPD, SPE: ").split())
    print()
    print(Name)
    getstats(statlist)   
    print()
Code: [Select]
Traceback (most recent call last):
  File "C:/Users/.../stats-list.py", line 36, in <module>
    getstats(statlist)
  File "C:/Users/.../stats-list.py", line 8, in getstats
    HPF = int(statlist[1] * (statlist[0] + 5))
TypeError: Can't convert 'int' object to str implicitly

I might be able to fix it by throwing float() and int() into the function, but that'd turn it into a mess. I assume I need to add float() somewhere inside statlist(), but I have no clue where to put it.

edit: I bit the bullet and threw in the extra tags, so it doesn't matter too much. I'll see about googling a general solution later.
« Last Edit: May 29, 2016, 09:00:01 pm by Elephant Parade »
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9647 on: May 30, 2016, 12:40:52 am »

The best fix would in fact be to throw a float() into the function.

Alternatively you could go through and turn each thing in statlist into a float, like so:
Code: [Select]
for i in statlist:
    statlist[statlist.index(i)] = float(i)
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9648 on: May 30, 2016, 09:43:14 am »

Code: [Select]
statlist = [float(stat) for stat in input().split()]
Sorry for terseness. "programming" on a phone isn't pleasant.
« Last Edit: May 30, 2016, 10:20:41 am by Mephisto »
Logged

Elephant Parade

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9649 on: May 30, 2016, 09:46:30 am »

The best fix would in fact be to throw a float() into the function.

Alternatively you could go through and turn each thing in statlist into a float, like so:
Code: [Select]
for i in statlist:
    statlist[statlist.index(i)] = float(i)
Code: [Select]
statlist = [float(stat) for stat in input().split()]
Ah, thanks.
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9650 on: May 30, 2016, 05:13:07 pm »

Hey here's my Pong game written in python using pygame, no AI implemented yet:

http://pastebin.com/b3ZYfN4n

What I don't like about my implementation is that shitton of horrible code in the mainloop, especially those lines which have to do with game states. How could I handle game states and struct my program better? 
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9651 on: May 30, 2016, 06:50:07 pm »

Put all your mainloop code into functions. Then, group those functions into broad categories and move them into separate modules.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9652 on: May 30, 2016, 08:02:37 pm »

As an aside, you're using screen.flip() which is very slow. Ideally you'd draw a rect around each part of the screen you individually want to update, and then pass a list of those rects into screen.update()
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9653 on: May 31, 2016, 12:25:38 pm »

After I got fed up with SDL2 (Streaming textures! ARRRGH!) and Orwell Dev-C++ (Stop ignoring my tabs!!!) I decided to jump ship and join the SFML party. I just spent two hours configuring Code::Blocks, a 64-bit version of GCC and SFML. Got it working and I was greeted by this in the tutorial:

Code: [Select]
#include <sfml/Graphics.hpp>

using namespace sf;

int main()
{
    RenderWindow window(VideoMode(200, 200), "SFML works!");
    CircleShape shape(100.f);
    shape.setFillColor(Color::Red);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

Holy cow, that's like ten times easier! Building a window is one line of code! I can edit shapes as C++ objects! And later on, I can work with OpenGL shaders instead of SDL's stupid streaming textures! Why didn't I get into SFML sooner? <_>

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9654 on: May 31, 2016, 05:15:17 pm »

I just spent two hours configuring Code::Blocks, a 64-bit version of GCC and SFML.
[…]
Holy cow, that's like ten times easier!
I am shocked by the amount of scaffolding C/C++ needs over and over.
Logged
Taste my Paci-Fist

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9655 on: May 31, 2016, 05:16:51 pm »

Scaffholding? What do you mean by that? I'm not familiar with the term. :X

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9656 on: May 31, 2016, 07:47:38 pm »

Antsan meant boilerplate code I think.  It tends to surprise me too just how much extra code it can take to do something in C or C++ compared to any modern scripting language.  Compare just how annoying it is to concatenate strings in C compared to JavaScript, PHP or similar scripting languages, for example.

C has good reasons for being like it is, but it doesn't change the fact that it's a chore to use for most things if you have alternatives.  C++ is better but still far from perfect.  Using a std::map feels like pulling teeth compared to similar functionality in JavaScript or PHP, even if, yes, it's probably going to be faster.
Logged
Through pain, I find wisdom.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9657 on: May 31, 2016, 08:16:24 pm »

Hoo boy, you got that right...After getting Code::Blocks set up for SFML, I tried to statically link it so all of the .dlls could be baked into the executable. This would have involved downloading several Windows SDKs and adding a dozen includes to my compiler's project options. Nooo thank you. Strings/maps are also a pain. I remember both being very easy in Python but C++ requires you to jump through hoops for simple stuff like that. :I

Fortunately, I expect all of this verbosity and extra work to pay off in the end. From what I've seen, most indie developers start with proprietary engines like Game Maker / Multimedia Fusion. They outgrow those limitations, and halfway through development they start from scratch in something more powerful. Instead of wasting my time like that, I decided to jump right into the harder stuff. It's been a slow and strenuous learning process for me but thankfully I've been making progress in the past few months. :D

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9658 on: May 31, 2016, 11:55:56 pm »

Compare just how annoying it is to concatenate strings in C compared to JavaScript, PHP or similar scripting languages, for example.
And then compare those to import pythonmasterrace
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9659 on: June 01, 2016, 06:46:54 am »

Scaffholding? What do you mean by that? I'm not familiar with the term. :X
Boilerplate code is part of it, but tools other than the compiler too are part of what I called scaffolding – like make systems, development environments and so on.
2 hours for simply setting up your development environment and libraries? Absolutely ridiculous, I say. That stuff should be a matter of 15 minutes tops.
Logged
Taste my Paci-Fist
Pages: 1 ... 642 643 [644] 645 646 ... 796