• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KuraiTsubasa

How to use Class?and what is return?

8 posts in this topic

Hi everyone i have a question about how to use a class so i make this script:

 

 

>>> class className:
             def createName(self,name):
                          self.name=name
             def displayName(self):
                          return self.name
             def saying(self):
                          print 'Hello %s' % self.name

 

>>> first=className

>>> first.createName('X')

 

I learn this from a tutorial but i have an error:

 

 

Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
first.createName('X')
TypeError: unbound method createName() must be called with className instance as first argument (got str instance instead)

 

 

the tutorial i watched is using 2.6 and i'm using 2.7 also what is the difference between return and print in def

 

and why i need to use (self,name) instead of only (self)

 

Thank you very much

1

Share this post


Link to post
Share on other sites

 

  1. Please give us the link which tutorial you watched
  2. the difference between return print and def are:
    1. def say you want define a new function
    2. return say you want return something from a function
    3. print is a function
  3. to the last one i can only say you need that self only in the definition of a class in python from what i know...
  4. You forgot to use '()' in the end of className at the line where you create this object!
    1. first = className()
    2. otherwise you would not call the ctor(constructor)

 

http://docs.python.org/2/tutorial/classes.html

 

i know where is my mistake i forgot to use () in first = className() thank you very muchbiggrin.png 

0

Share this post


Link to post
Share on other sites

To put it really simply, return sets the value of the function, while print simply prints to the screen.  In very simple functions, they can appear to be the same thing because of the way the Python interpreter works, but they're very different.  For example, compare these two functions.



def foo_return():
 return 5
 return 3

def foo_print():
 print 5
 print 3

foo_return()
foo_print()

v1 = foo_return()
v2 = foo_print()

Call foo_return() and you will get "5", because the interpreter itself prints return values.  Call foo_print() and you'll see 5 followed by 3.  Likewise, v1 will be equal to 5.  v2 won't be equal to anything, because you didn't return anything.  However, since you called the function, you'll print the two values again.  So if you run the above in python as-is, you'll see something like:



5
5
3
5
3

As far as the reason you need "self" in python, it's a bit complicated, but it basically chalks down to the way classes are built under the hood.  You don't really need to worry about it as long as you make sure to include it.

Thank you for the explaination but why i can use (self,name) and not only (self)smile.png 

0

Share this post


Link to post
Share on other sites

print 'Hello %s' % self.name

use

print('Hello %s' % self.name)

for Python 2 and 3 versions compatibility

also you can use

class className:
    def __init__(self,name):
        self.name=name
........

cls_inst = className('X')
cls_inst.saying()
Edited by pyirrlicht
0

Share this post


Link to post
Share on other sites

It took me a while to understand classes but I will see if I can help. 

 

A class is a way to classify variables. An apple is a type of fruit. Apple is the variable, and fruit is the class. In python an apple is made a fruit this way:

apple = Fruit()

However, an orange is a fruit as well. So we can also make it a fruit:

orange = Fruit()

Now, all fruit have certain characteristics. They all have a taste. They also have a color. You can also classify them by size.

 

Taste, color, and size would be variables that are a part of your fruit class, so that you could type:

print (orange.color) 

And you could get the color of the orange. 

 

Whenever you make a variable a part of a class, you are making it an instance of the class. You are also making it an object (because now it has characteristics). The apple is one instance and the orange is another instance. 

 

The self.name is nothing special really other than the fact that we want to be able to type apple.name to get the apple's name rather than just name. The name of your object will be substituted for the self. So you can read the self as apple.name or orange.name

 

They have to make self.name = name so that they can use name as an argument. That means that wherever the word name is in the class, switch it out for something else. 

 

>>> class className:
             def createName(self,name):
                          self.name=name
             def displayName(self):
                          return self.name
             def saying(self):
                          print 'Hello %s' % self.name

 

>>> first=className

>>> first.createName('X')

 

 

As for return. It works just like return does for functions. 

 

If you had a function called Add(x,y) that adds two numbers x and You would want the function to add the two numbers and then return the answer. 

def Add(x,y):
answer = x + y
return answer

So if you type

 

Add(3,4) 

 

This function should return 7.

 

Without the return part, the function would add, but it wouldn't output an answer. 

 

For a more detailed explanation of OOP (Object Oriented Programming) check out my tutorial:

 

http://forum.maratis3d.com/viewtopic.php?id=839

Edited by Tutorial Doctor
2

Share this post


Link to post
Share on other sites

Larger programs usually model aspects of the program's domain. For a business application, the domain might be the business processes of a given company. For a RPG game, the domain is the fantasy world in which the events take place. For Pac-Man, the domain is the flat, 2D maze and the Ghost, pills and fruit that inhabit the world with the circular hero.

 

Object oriented languages are a popular way of modelling the domain. In this way of thinking, we want each interesting object in the domain to correspond to an object or set of objects in our program. An object could be a simple concrete thing, such as an enemy, a gun or a cactus. It can be more abstract, such as an unit or build order in an RTS game or a tile in a tile-based game.

 

We will quickly note that many of the objects come in groups, and are similar. For example, we will have different types of enemies, different guns or different orders we can give units. In an object oriented program, these groups are modelled as classes, and the individual objects are so-called instances. In an object oriented Pac-Man, there might be a Ghost class, and four instances for Inky, Binky, Pinky and Clyde. The fundamental idea is that you can write some code that will work with any Ghost, and sometime later in the program that code could be used on any particular instance of the Ghost class that might be around, without caring which particular ghost was used.

 

Most computer programs make a profound distinction between code and data. Most programming languages define the program code in a number of functions or procedures. The data is the contents of the variables in the program. At the time object orientation was being introduced, the popular languages at the time had a very simple mapping, code was represented as functions or procedures that acted on data, which was stored in data structures. Any function or procedure could typically change any data it wanted in the data structure. It could sometimes be quite difficult to track down the code that changed a variable to an unexpected or bad state in a large program.

 

Object orientated languages usually try and put the code (or behaviour) and data of an object together, often giving the programmer the option to hide certain bits of behaviour and data from the rest of the program. The belief is that an object should mange it's own data, and should be responsible for enforcing some rules about it's data. For instance, a Ghost in Pac-Man can either be alive (if you pardon the expression, I mean chasing Pac-Man), or be flashing blue, or be "dead" (returning to their pen). An object oriented purist would insist that the Ghost class be written so that it would not be possible to put the Ghost into an invalid state, or here multiple states. So any data that represents this state would be preferably hidden from the program, and the Ghost would only expose the methods necessary for the program to inform the Ghost of events such as Pac-Man eating a power pill, Pac-Man touching the Ghost, and the Ghost entering the pen. Then the ghost could decide to change it's state in response to these events.

 

Unfortunately, demonstrating the value of object orientation is challenging to a beginner, as the kind of complex program where this becomes an issue would be very difficult to explain. Generally speaking, the practical value of such rules is only evident to programmers when they start to build medium sized projects that pass a certain amount of complexity.

Edited by rip-off
0

Share this post


Link to post
Share on other sites
[quote name="Tutorial Doctor" post="5110512" timestamp="1384881652"]

It took me a while to understand classes but I will see if I can help.

[...]

For a more detailed explanation of[b] OOP[/b] ([b]Object Oriented Programming[/b]) check out my tutorial:

[url="http://forum.maratis3d.com/viewtopic.php?id=839"]http://forum.maratis3d.com/viewtopic.php?id=839[/url][/quote]
Thank you for you help and I will read all the tutorial [img]http://public.gamedev5.net//public/style_emoticons/default/biggrin.png[/img]

[quote name="rip-off" post="5110546" timestamp="1384889162"]

Larger programs usually model aspects of the program's [i]domain[/i]. For a business application, the domain might be the business processes of a given company. For a RPG game, the domain is the fantasy world in which the events take place. For Pac-Man, the domain is the flat, 2D maze and the Ghost, pills and fruit that inhabit the world with the circular hero.

[...]

Unfortunately, demonstrating the value of object orientation is challenging to a beginner, as the kind of complex program where this becomes an issue would be very difficult to explain. Generally speaking, the practical value of such rules is only evident to programmers when they start to build medium sized projects that pass a certain amount of complexity.[/quote]
Thanks for that explanation I still will keep practice thank you so much[img]http://public.gamedev5.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0