Help with Pygame!

Help with Pygame!

Postby ChristianCareaga » Mon Jun 24, 2013 7:46 pm

Im making a Pygame and basically you draw you're own ship background and enemy ship

here is the github repo :
https://github.com/C-Dubb/Doodle

if you look at it please star it

Ok so if anyone wants to take the time and play it and look through the code and tell me how i could make the code more efficient or maybe you're ideas that would be really appreciated!

Thank You!
ChristianCareaga
 
Posts: 45
Joined: Sat Jun 22, 2013 9:54 am

Re: Help with Pygame!

Postby metulburr » Mon Jun 24, 2013 8:24 pm

I am unable to see the pointer, untill i start drawing

os.path.join() paths together such as:
Code: Select all
background = pygame.image.load('game/background.png')

in linux it's fine, but if you run it in windows the path is game\\background.png breaking the game

flooding the namespace with:
Code: Select all
from globals import *


for example:
Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from Tkinter import *
>>> dir()
['ACTIVE', 'ALL', 'ANCHOR', 'ARC', 'At', 'AtEnd', 'AtInsert', 'AtSelFirst', 'AtSelLast', 'BASELINE', 'BEVEL', 'BOTH', 'BOTTOM', 'BROWSE', 'BUTT', 'BaseWidget', 'BitmapImage', 'BooleanType', 'BooleanVar', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'Button', 'CASCADE', 'CENTER', 'CHAR', 'CHECKBUTTON', 'CHORD', 'COMMAND', 'CURRENT', 'CallWrapper', 'Canvas', 'Checkbutton', 'ClassType', 'CodeType', 'ComplexType', 'DISABLED', 'DOTBOX', 'DictProxyType', 'DictType', 'DictionaryType', 'DoubleVar', 'E', 'END', 'EW', 'EXCEPTION', 'EXTENDED', 'EllipsisType', 'Entry', 'Event', 'FALSE', 'FIRST', 'FLAT', 'FileType', 'FloatType', 'Frame', 'FrameType', 'FunctionType', 'GROOVE', 'GeneratorType', 'GetSetDescriptorType', 'Grid', 'HIDDEN', 'HORIZONTAL', 'INSERT', 'INSIDE', 'Image', 'InstanceType', 'IntType', 'IntVar', 'LAST', 'LEFT', 'Label', 'LabelFrame', 'LambdaType', 'ListType', 'Listbox', 'LongType', 'MITER', 'MOVETO', 'MULTIPLE', 'MemberDescriptorType', 'Menu', 'Menubutton', 'Message', 'MethodType', 'Misc', 'ModuleType', 'N', 'NE', 'NO', 'NONE', 'NORMAL', 'NS', 'NSEW', 'NUMERIC', 'NW', 'NoDefaultRoot', 'NoneType', 'NotImplementedType', 'OFF', 'ON', 'OUTSIDE', 'ObjectType', 'OptionMenu', 'PAGES', 'PIESLICE', 'PROJECTING', 'Pack', 'PanedWindow', 'PhotoImage', 'Place', 'RADIOBUTTON', 'RAISED', 'READABLE', 'RIDGE', 'RIGHT', 'ROUND', 'Radiobutton', 'S', 'SCROLL', 'SE', 'SEL', 'SEL_FIRST', 'SEL_LAST', 'SEPARATOR', 'SINGLE', 'SOLID', 'SUNKEN', 'SW', 'Scale', 'Scrollbar', 'SliceType', 'Spinbox', 'StringType', 'StringTypes', 'StringVar', 'Studbutton', 'TOP', 'TRUE', 'Tcl', 'TclError', 'TclVersion', 'Text', 'Tk', 'TkVersion', 'Toplevel', 'TracebackType', 'Tributton', 'TupleType', 'TypeType', 'UNDERLINE', 'UNITS', 'UnboundMethodType', 'UnicodeType', 'VERTICAL', 'Variable', 'W', 'WORD', 'WRITABLE', 'Widget', 'Wm', 'X', 'XRangeType', 'XView', 'Y', 'YES', 'YView', '__builtins__', '__doc__', '__name__', '__package__', 'getboolean', 'getdouble', 'getint', 'image_names', 'image_types', 'mainloop', 're', 'sys', 'tkinter', 'wantobjects']
>>>


its better to do:
Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import Tkinter
>>> dir()
['Tkinter', '__builtins__', '__doc__', '__name__', '__package__']
>>>


at that point you address those variables and methods in Tkinter as Tkinter.LEFT (for example). It may seem more convenient to do otherwise, but its one of those things that you should just get use to. And as you progress it becomes more convenient to just do import Tkinter. Of course replace Tkinter with any other module.

Its more pythonic to instead of:
Code: Select all
        self.state = 1
        self.draw = True
       
        while 1:

to do:
Code: Select all
        self.state = True
        self.draw = True
       
        while True:



You should reload the module instead of import
Code: Select all
        if key[pygame.K_s]:
           
            pygame.image.save(screen,"screenshot.jpg")
            import crop
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1111
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with Pygame!

Postby ChristianCareaga » Mon Jun 24, 2013 8:46 pm

metulburr wrote:I am unable to see the pointer, untill i start drawing

os.path.join() paths together such as:
Code: Select all
background = pygame.image.load('game/background.png')

in linux it's fine, but if you run it in windows the path is game\\background.png breaking the game

flooding the namespace with:
Code: Select all
from globals import *


for example:
Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from Tkinter import *
>>> dir()
['ACTIVE', 'ALL', 'ANCHOR', 'ARC', 'At', 'AtEnd', 'AtInsert', 'AtSelFirst', 'AtSelLast', 'BASELINE', 'BEVEL', 'BOTH', 'BOTTOM', 'BROWSE', 'BUTT', 'BaseWidget', 'BitmapImage', 'BooleanType', 'BooleanVar', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'Button', 'CASCADE', 'CENTER', 'CHAR', 'CHECKBUTTON', 'CHORD', 'COMMAND', 'CURRENT', 'CallWrapper', 'Canvas', 'Checkbutton', 'ClassType', 'CodeType', 'ComplexType', 'DISABLED', 'DOTBOX', 'DictProxyType', 'DictType', 'DictionaryType', 'DoubleVar', 'E', 'END', 'EW', 'EXCEPTION', 'EXTENDED', 'EllipsisType', 'Entry', 'Event', 'FALSE', 'FIRST', 'FLAT', 'FileType', 'FloatType', 'Frame', 'FrameType', 'FunctionType', 'GROOVE', 'GeneratorType', 'GetSetDescriptorType', 'Grid', 'HIDDEN', 'HORIZONTAL', 'INSERT', 'INSIDE', 'Image', 'InstanceType', 'IntType', 'IntVar', 'LAST', 'LEFT', 'Label', 'LabelFrame', 'LambdaType', 'ListType', 'Listbox', 'LongType', 'MITER', 'MOVETO', 'MULTIPLE', 'MemberDescriptorType', 'Menu', 'Menubutton', 'Message', 'MethodType', 'Misc', 'ModuleType', 'N', 'NE', 'NO', 'NONE', 'NORMAL', 'NS', 'NSEW', 'NUMERIC', 'NW', 'NoDefaultRoot', 'NoneType', 'NotImplementedType', 'OFF', 'ON', 'OUTSIDE', 'ObjectType', 'OptionMenu', 'PAGES', 'PIESLICE', 'PROJECTING', 'Pack', 'PanedWindow', 'PhotoImage', 'Place', 'RADIOBUTTON', 'RAISED', 'READABLE', 'RIDGE', 'RIGHT', 'ROUND', 'Radiobutton', 'S', 'SCROLL', 'SE', 'SEL', 'SEL_FIRST', 'SEL_LAST', 'SEPARATOR', 'SINGLE', 'SOLID', 'SUNKEN', 'SW', 'Scale', 'Scrollbar', 'SliceType', 'Spinbox', 'StringType', 'StringTypes', 'StringVar', 'Studbutton', 'TOP', 'TRUE', 'Tcl', 'TclError', 'TclVersion', 'Text', 'Tk', 'TkVersion', 'Toplevel', 'TracebackType', 'Tributton', 'TupleType', 'TypeType', 'UNDERLINE', 'UNITS', 'UnboundMethodType', 'UnicodeType', 'VERTICAL', 'Variable', 'W', 'WORD', 'WRITABLE', 'Widget', 'Wm', 'X', 'XRangeType', 'XView', 'Y', 'YES', 'YView', '__builtins__', '__doc__', '__name__', '__package__', 'getboolean', 'getdouble', 'getint', 'image_names', 'image_types', 'mainloop', 're', 'sys', 'tkinter', 'wantobjects']
>>>


its better to do:
Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import Tkinter
>>> dir()
['Tkinter', '__builtins__', '__doc__', '__name__', '__package__']
>>>


at that point you address those variables and methods in Tkinter as Tkinter.LEFT (for example). It may seem more convenient to do otherwise, but its one of those things that you should just get use to. And as you progress it becomes more convenient to just do import Tkinter. Of course replace Tkinter with any other module.

Its more pythonic to instead of:
Code: Select all
        self.state = 1
        self.draw = True
       
        while 1:

to do:
Code: Select all
        self.state = True
        self.draw = True
       
        while True:



You should reload the module instead of import
Code: Select all
        if key[pygame.K_s]:
           
            pygame.image.save(screen,"screenshot.jpg")
            import crop


Thanks so much for the feedback its highly appreciated ill fix all that stuff Thanks

(also thanks for the follow on github!)
ChristianCareaga
 
Posts: 45
Joined: Sat Jun 22, 2013 9:54 am

Re: Help with Pygame!

Postby Mekire » Tue Jun 25, 2013 1:41 am

metulburr wrote:os.path.join() paths together such as:
[codebackground = pygame.image.load('game/background.png')[/code]in linux it's fine, but if you run it in windows the path is game\\background.png breaking the game

Actually forward slashes will work in windows. That said, yes, best practice is to always use os.path.join().

-Mek
User avatar
Mekire
 
Posts: 820
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help with Pygame!

Postby ChristianCareaga » Tue Jun 25, 2013 1:55 am

Mekire wrote:
metulburr wrote:os.path.join() paths together such as:
[codebackground = pygame.image.load('game/background.png')[/code]in linux it's fine, but if you run it in windows the path is game\\background.png breaking the game

Actually forward slashes will work in windows. That said, yes, best practice is to always use os.path.join().

-Mek


does the code look better than my last game so far ??
ChristianCareaga
 
Posts: 45
Joined: Sat Jun 22, 2013 9:54 am

Re: Help with Pygame!

Postby Mekire » Tue Jun 25, 2013 2:13 am

ChristianCareaga wrote:
Code: Select all
def Loop(self):
    # main game loop
    self.eventLoop()
    self.mouse_pos = pygame.mouse.get_pos()

    if self.state == 1:
        if pygame.time.get_ticks() - self.last_tick > 20:
            self.main_screen()

    if self.state == 2:
        if pygame.time.get_ticks() - self.last_tick > 60:
            self.Draw.update(screen)


    pygame.display.update()
As python code should be self documenting where possible, it would be much better to use meaningful strings for your states instead of arbitrary numbers.

IE:
Code: Select all
def Loop(self):
    # main game loop
    self.eventLoop()
    self.mouse_pos = pygame.mouse.get_pos()

    if self.state == "TITLE":
        if pygame.time.get_ticks() - self.last_tick > 20:
            self.main_screen()

    elif self.state == "DRAW":
        if pygame.time.get_ticks() - self.last_tick > 60:
            self.Draw.update(screen)
    pygame.display.update()
Even more ideally all your states should be separate classes (separate from your control flow class) and each should have a function called update. Then you can update via polymorphism and change state using a dictionary (see the previous rewrite of your other program for an example of this).

ChristianCareaga wrote:does the code look better than my last game so far ??
It looks like you are on the right track. Look into making a control class that does nothing except pass events to the current game state and update the current state. Also look into using pygame.Clock.tick(fps) to control your framerate. In most cases you will use pygame.Clock.tick(fps) to control the global framerate of the game and use pygame.time.get_ticks() to control the framerates of individual things like animations. You should also make yourself intimately familiar with how to use the Rect class in pygame. As Metul said and I said previously, stop the star imports now. It will be much harder to stop later when you truly realize why it's terrible.

-Mek
User avatar
Mekire
 
Posts: 820
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help with Pygame!

Postby metulburr » Tue Jun 25, 2013 8:26 am

Actually forward slashes will work in windows. That said, yes, best practice is to always use os.path.join().

oh so its only the other way around. Good to know
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1111
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with Pygame!

Postby ChristianCareaga » Tue Jun 25, 2013 9:55 am

Mekire wrote:
ChristianCareaga wrote:
Code: Select all
def Loop(self):
    # main game loop
    self.eventLoop()
    self.mouse_pos = pygame.mouse.get_pos()

    if self.state == 1:
        if pygame.time.get_ticks() - self.last_tick > 20:
            self.main_screen()

    if self.state == 2:
        if pygame.time.get_ticks() - self.last_tick > 60:
            self.Draw.update(screen)


    pygame.display.update()
As python code should be self documenting where possible, it would be much better to use meaningful strings for your states instead of arbitrary numbers.

IE:
Code: Select all
def Loop(self):
    # main game loop
    self.eventLoop()
    self.mouse_pos = pygame.mouse.get_pos()

    if self.state == "TITLE":
        if pygame.time.get_ticks() - self.last_tick > 20:
            self.main_screen()

    elif self.state == "DRAW":
        if pygame.time.get_ticks() - self.last_tick > 60:
            self.Draw.update(screen)
    pygame.display.update()
Even more ideally all your states should be separate classes (separate from your control flow class) and each should have a function called update. Then you can update via polymorphism and change state using a dictionary (see the previous rewrite of your other program for an example of this).

ChristianCareaga wrote:does the code look better than my last game so far ??
It looks like you are on the right track. Look into making a control class that does nothing except pass events to the current game state and update the current state. Also look into using pygame.Clock.tick(fps) to control your framerate. In most cases you will use pygame.Clock.tick(fps) to control the global framerate of the game and use pygame.time.get_ticks() to control the framerates of individual things like animations. You should also make yourself intimately familiar with how to use the Rect class in pygame. As Metul said and I said previously, stop the star imports now. It will be much harder to stop later when you truly realize why it's terrible.

-Mek


Yeah I saw the control class in the rewrite I think I'll use. That and thanks for all the help!
ChristianCareaga
 
Posts: 45
Joined: Sat Jun 22, 2013 9:54 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 0 guests