Thomas | 1 Feb 2011 08:51
Picon

Re: PyGame Windows Key Behaviour

Hi there, thanks for fast responses

 <at>  Julian: Equal means exakt the same function with the same
playerspeed variable - but under other conditions:
it compares the position of the cpuplayer and the ball and rearranges
the cpuplayer
def move_player_a(ball_y):
    global a_y
    if a_y + 31 > ball_y and a_y > 0:
        #up
        a_y += seconds * playerspeed * -1
    elif a_y + 31 < ball_y and a_y + 95 < 360:
        #down
        a_y += seconds * playerspeed * 1

i will have to take a look at framerates
thanks for that hint

 <at>  Ian: i will also check pygame.key.get_pressed(). thx

 <at>  Brian: The complete code is here http://pastebin.com/ic44uvfU
code was growing with my pygame knowledgement, so it isn't as nice as
i would like to have it ;)
but as additional info, pictures where used as background and as bars

Brian Fisher | 1 Feb 2011 09:58
Favicon

Re: Re: PyGame Windows Key Behaviour

Hey Thomas,
   Ian Mallet's guess was correct and his solution will fix it.

You are moving the paddle based on key down events coming in, It looks like you were counting on:
    pygame.key.set_repeat(1,1)
to provide you with key down events every frame, but it doesn't work out that way. I added prints to your code to see how the objects were moving, and both paddles move by the same amount each time they move, but sometimes the player paddle moves only 2 out of 3 frames (i.e. the player paddle moves slower), hence the speed difference you were seeing.

What you want is to move the paddle when the appropriate key is held down - namely when you've had a down event without an up event yet. key_pressed() is a shortcut way to get that info.


On Mon, Jan 31, 2011 at 11:51 PM, Thomas <pummer.thomas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi there, thanks for fast responses

<at> Julian: Equal means exakt the same function with the same
playerspeed variable - but under other conditions:
it compares the position of the cpuplayer and the ball and rearranges
the cpuplayer
def move_player_a(ball_y):
   global a_y
   if a_y + 31 > ball_y and a_y > 0:
       #up
       a_y += seconds * playerspeed * -1
   elif a_y + 31 < ball_y and a_y + 95 < 360:
       #down
       a_y += seconds * playerspeed * 1

i will have to take a look at framerates
thanks for that hint

<at> Ian: i will also check pygame.key.get_pressed(). thx

<at> Brian: The complete code is here http://pastebin.com/ic44uvfU
code was growing with my pygame knowledgement, so it isn't as nice as
i would like to have it ;)
but as additional info, pictures where used as background and as bars

Thomas Pummer | 1 Feb 2011 16:58
Picon

Re: PyGame Windows Key Behaviour

Thanks, it's scrolling fine now on different machines.

Greets
Thomas

On 1 Feb., 09:58, Brian Fisher <br...@...> wrote:
> Hey Thomas,
>    Ian Mallet's guess was correct and his solution will fix it.
>
> You are moving the paddle based on key down events coming in, It looks like
> you were counting on:
>     pygame.key.set_repeat(1,1)
> to provide you with key down events every frame, but it doesn't work out
> that way. I added prints to your code to see how the objects were moving,
> and both paddles move by the same amount each time they move, but sometimes
> the player paddle moves only 2 out of 3 frames (i.e. the player paddle moves
> slower), hence the speed difference you were seeing.
>
> What you want is to move the paddle when the appropriate key is held down -
> namely when you've had a down event without an up event yet. key_pressed()
> is a shortcut way to get that info.
>
> On Mon, Jan 31, 2011 at 11:51 PM, Thomas <pummer.tho...@...> wrote:
> > Hi there, thanks for fast responses
>
> >  <at>  Julian: Equal means exakt the same function with the same
> > playerspeed variable - but under other conditions:
> > it compares the position of the cpuplayer and the ball and rearranges
> > the cpuplayer
> > def move_player_a(ball_y):
> >    global a_y
> >    if a_y + 31 > ball_y and a_y > 0:
> >        #up
> >        a_y += seconds * playerspeed * -1
> >    elif a_y + 31 < ball_y and a_y + 95 < 360:
> >        #down
> >        a_y += seconds * playerspeed * 1
>
> > i will have to take a look at framerates
> > thanks for that hint
>
> >  <at>  Ian: i will also check pygame.key.get_pressed(). thx
>
> >  <at>  Brian: The complete code is herehttp://pastebin.com/ic44uvfU
> > code was growing with my pygame knowledgement, so it isn't as nice as
> > i would like to have it ;)
> > but as additional info, pictures where used as background and as bars

Sam Bull | 5 Feb 2011 13:57

Re: GSoC

Hi guys,

I'm looking for some input into widget events, and what people think is
the best way to implement this.

There seems to be two main methods, either a widget can have functions
that are called on these events, or the widget can emit a Pygame event
that will then be handled in the main game loop.
	The advantage of the first method is that it is perhaps a little more
OO, in that the functions are encapsulated within the widget's class.
But, from a conversation with someone else about this, it was suggested
that this could complicate the game code, and it would be better to use
events in order to keep the game code and GUI code separated.
	After this conversation, I have come up with a potential solution:
On a widget event, such as clicking on a button or hitting the enter key
in an input box, the widget will check for an appropriate function (e.g.
self.on_click()), and run it. This will be useful for printlining or
running some quick GUI code, e.g. clicking a button that should
add/remove a menu can be easily done this way.
	If the function is not present, then the widget will emit a Pygame
event, that the developers game code can then handle.

What do you think about the best way to do this?

Also, if you like the proposed idea, would it be better to check if the
function exists, or perhaps check if the function returns None.
	The latter may be advantageous, as I can have empty functions in the
base class to simplify documentation. It would also allow a developer to
use the function and return None, so that an event is still emitted and
both methods could be used simultaneously.

Thanks,
Sam Bull

> On Sat, Jan 15, 2011 at 7:46 AM, Sam Bull <sam.hacking@...>
> wrote:
>         Hi,
>         
>         I was wondering if there was any chance of creating a new GUI
>         toolkit in
>         Pygame, for Google Summer of Code. I know this isn't working
>         directly on
>         Pygame, but I think an easier to use, more detailed toolkit
>         may lower
>         the entry barrier for new developers, and provide a smoother
>         experience
>         to the players that choose to play these games, thus improving
>         the
>         overall quality of the Pygame project.
>         
>         This toolkit will focus more on the widgets than packing
>         features,
>         giving full control to the developer as to where the widgets
>         should be
>         positioned. Each widget should act as a user would expect,
>         with similar
>         behaviour to GTK+ widgets, a lot of attention has gone into
>         the small
>         details.
>         
>         With it's current design, to start using the toolkit it is as
>         simple as:
>         from sgc import *
>         from sgc.locals import * #Import the modules
>         # Create a widget
>         example_widget = widgets.Input_Box(args...) #Create an input
>         box widget
>         example_widget.pos = (x,y) #Set the widget's position
>         example_widget.add() #Add widget to the active widgets
>         Then just add into your event handling:
>         widgets.event(event)
>         And somewhere else in your game loop:
>         widgets.update(time)
>         
>         It should be complete without graphics to keep the total size
>         low and
>         unbloated, all graphics should thus be produced through code
>         only.
>         Although, it will be themeable so that developers can use
>         custom images
>         to change the appearance of the widgets.
>         
>         During this GSoC project I would also like to make this
>         toolkit work in
>         OpenGL under Pygame, with identical behaviour, so developers
>         can have a
>         more seamless transition between 2D and 3D game development.
>         
>         I have started creating a spec for this project as part of my
>         college
>         coursework. Most of the stuff in the spec has already been
>         completed, so
>         if there is a chance I can do this for GSoC then I will expand
>         this spec
>         to show what will be completed during this timeframe. You can
>         download
>         the spec from:
>         http://sambull.org/spec.pdf
>         
>         If you also want to see the current progress of the project,
>         you can
>         download the source from:
>         https://launchpad.net/simplegc
>         Just download the code from the link, then run the run.py file
>         in Python
>         to launch the example program. The widgets should behave as
>         you would
>         expect them to on your desktop, use the "f" key to toggle the
>         FPS
>         counter.
>         The dialog windows and menu are not complete, but the few
>         other widgets
>         are reasonably feature complete.
>         
>         Thank you for your time,
>         Sam Bull

amdlintuxo | 5 Feb 2011 15:52
Picon

[pyopengl] glActiveTexture(GL_TEXTURE1) cause failure in my code, can't get the reason why

Hi all.

I was trying to play with GLSL multi-texturing and as usual the
problems don't want stay far away from me.
Now i get problem with Texture units.

glActiveTexture(GL_TEXTURE1) cause failure, i can't get the reason
why.
If i use only glActiveTexture(GL_TEXTURE0) then it's is OK, but in
this case i am able to transfer only one texture to fragment shader
with is not enough for multi-texturing.

glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB) returns me 32,
also i checked this in
Also i found that all these works good on http://www.pygame.org/project-glLib+Reloaded-1326-.html
glLibShader.py, glLibTexturing.py, parallaxmapping.py[Tutorial]

I tried to compare and identify the difference in
glActiveTexture(GL_TEXTURE1 call and functions surround it , no luck,
probably the difference exist in some stage of init, but it is hard to
identify what exactly i missed.

Also code for quick look can be found here:
http://bpaste.net/show/13480/

The log of fault
[code]
Fragment shader(s) linked, vertex shader(s) linked.

1 -1
32
Traceback (most recent call last):
  File "GLSL_uniform_multitexturing_TMP.py", line 18, in
glLibTestErrors
    function()
  File "GLSL_uniform_multitexturing_TMP.py", line 201, in main
    glPopMatrix()
  File "/usr/lib/pymodules/python2.6/OpenGL/error.py", line 208, in
glCheckError
    baseOperation = baseOperation,
GLError: GLError(
        err = 1282,
        description = 'invalid operation',
        baseOperation = glPopMatrix,
        cArguments = ()
)
[/code]

Thank for support in advance!

amdlintuxo | 5 Feb 2011 16:14
Picon

Re: [pyopengl] glActiveTexture(GL_TEXTURE1) cause failure in my code, can't get the reason why

Sorry, problem was solved by excluding GLUT and it's call.

Cheers, Alex

Ian Mallett | 5 Feb 2011 16:39
Picon

Re: Re: [pyopengl] glActiveTexture(GL_TEXTURE1) cause failure in my code, can't get the reason why

Hi,

Have a look at the output; the minus one indicates an invalid index, invalid because the variable it points to does not exist in the shader.  Although it doesn't on my machine, perhaps trying to pass the uniform to it causes some error. 

The reason "Texture_1" doesn't exist in the shader is that GLSL optimized it out, because it wasn't used.  If you uncomment the mix command in the fragment shader, you'll see that the index goes away (and for me a nice multitextured teapot starts rotating).

Also note: glGetUniformLocation doesn't have very well-defined operation unless the shader is bound.  Perhaps when you print the locations on line 157, you're inadvertently causing an error later.

Ian

#avg_ls_inline_popup{position: absolute;z-index: 9999;padding: 0px 0px;margin-left: 0px;margin-top: 0px;overflow: hidden;word-wrap: break-word;color: black;font-size: 10px;text-align: left;line-height: 130%;}
amdlintuxo | 5 Feb 2011 18:07
Picon

Re: [pyopengl] glActiveTexture(GL_TEXTURE1) cause failure in my code, can't get the reason why

On Feb 5, 7:39 pm, Ian Mallett <geometr...@...> wrote:
> Hi,
>
> Have a look at the output; the minus one indicates an invalid index, invalid
> because the variable it points to does not exist in the shader.  Although it
> doesn't on my machine, perhaps trying to pass the uniform to it causes some
> error.
>
> The reason "Texture_1" doesn't exist in the shader is that GLSL optimized it
> out, because it wasn't used.  If you uncomment the mix command in the
> fragment shader, you'll see that the index goes away (and for me a nice
> multitextured teapot starts rotating).
Hi Ian.
Yes i got this point as you explained me in the previous topic about
compiler optimization for unused variables.
The reason i have commented mix, i change manually in the shader
texel0 and texel1 to see what is going on here.

> Also note: glGetUniformLocation doesn't have very well-defined operation
> unless the shader is bound.  Perhaps when you print the locations on line
> 157, you're inadvertently causing an error later.
I have commented the prints, but this is does not resolve the problem,
problem is the same.
I sure that is in something with my GLUT implementation or setup,
because if commented only 3 strings:
#from OpenGL.GLUT import *
#glutInit(sys.argv)
#glutSolidTeapot(1.0)
My program runs well without a fault described below. Then i just
added manually quad vertexes and put textures in there by GLSL,
everything works good now for me.

Cheers, Alex

Greg Ewing | 6 Feb 2011 01:00
Picon
Picon
Favicon

Re: GSoC

Sam Bull wrote:

> But, from a conversation with someone else about this, it was suggested
> that this could complicate the game code, and it would be better to use
> events in order to keep the game code and GUI code separated.

It's not necessary to use pygame events to achieve that. Techniques
for keeping GUI and application code decoupled are well known. Google
"MVC" for more information.

The only good reason I can think of for using events this way is if
for some reason you need to handle the event asynchronously, i.e. you
need to ensure that all processing for the current event is completed
before carrying out the action. Otherwise, using an event is probably
just needless complication.

--

-- 
Greg

David Burton | 6 Feb 2011 05:01
Picon

Re: GSoC

Any GUI-widget toolkit needs to send information back to the application about the events that happen: button clicks, menu items picked, text entered, etc..  There are two obvious ways to do that in pygame: callbacks and pygame events.

Pygame events are by far the better approach.

Here's how I do it in my code.  When initializing a GUI-widget, it is passed an identifying data value (an ID), to distinguish it from other GUI-widgets.  The ID value is stored in the GUI-widget's instance data.  Then, when the widget needs to send information back to the program, it simply posts a pygame event with the ID value stored in the pygame event instance.  (I currently use a single event type number, default USEREVENT, for all GUI-widget-generated events.)

This is super-simple for the application to handle, much easier than callbacks.  In the main program, you just have another elif clause in the event loop, to receive GUI-widget events: button clicks, menu clicks, user-entered-text-and-pressed [Enter], etc.

Using the event queue like that is just plain Better for the structure of the program than using callbacks.

If you use callbacks to call user functions whenever a button or menu click happens, then much of the time all the callbacks do is set some global flags, store some data, and then return, so that you can unwrap the call stack and wind your way back out of the innards of the GUI code, before actually doing whatever the button or menu click requires.  (That's because, if you are an experienced programmer, you have learned [probably the hard way] that you really don't want to start anything complicated in response to a click if you're deep in the bowels of the GUI code [which has called your callback function], since the complicated thing you start might want to use that same GUI code, and get button clicks and menu choices of its own, leading to ever deeper recursion in the [hopefully reentrant!] GUI code, and mind-bending complexity and bugs.)

If you use the event queue, instead of callbacks, to pass back information from the GUI, all that complexity goes away.  A button click, a menu selection, etc. is just another event to be handled in the main event loop.

Python/pygame makes that easy, since you can attach as much additional info as you want to the event object.  I think the reason so many other GUI frameworks use callbacks is that their event queues are so limited.  All they contain is integer event numbers, so it isn't feasible to pass back (for example) the string of text from a text entry field.  So they work around this limitation with callbacks to pass back the additional information.  Programmers get used to this cumbersome approach, and do it that way even when they don't have to, because of the Toolbox Principle: "if all you have is a hammer, then everything looks like a nail."

Additionally, you can make any GUI-widget modal, by wrapping it in a simple function that contains a temporary event loop.  Here's mine:

def modal_popup(widget):
    '''Usually, widget sprites are displayed and active along with everything
    else in the application, but it is also possible to display a widget sprite
    "modally," that is, with everything else suspended.  To do so, just pass
    the widget to this function.  It won't return until the widget generates
    a result event.

    A result event is any event of type WIDGETEVENT which has an attribute
    .internal=False.

    Note: don't use this with widget sprites which can't generate events (like
    LabelSprites), lest it never return!  (Humming Kingston Trio tune...)
    '''
    group = WidgetGroup(widget)  # a temporary group, for duration of this popup
    keep_running = True
    while keep_running:
        # Update and Draw the widget
        group.update()
        group.draw(screen)
        pygame.display.update()
        # get next event:
        event = pygame.event.wait()
        # If event is QUIT then they closed the applicaiton, so quit
        if event.type == QUIT:
            keep_running = False
            pygame.quit()
            sys.exit(0)
        # Give widget sprite a look at the event
        group.notify(event)
        # We loop until we get a result event from the widget!
        if event.type == WIDGETEVENT:
            if hasattr(event,'internal') and not event.internal:
                group.remove(widget)
                keep_running = False
    return event

However, I have noticed that pygame events are somewhat weird.  They are objects, but they don't always behave like proper Python objects.  There's no __dict__ attribute, and dir() doesn't work, which I find annoying:

Python 3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
>>> ev = pygame.event.Event( pygame.USEREVENT, {'id':'test', 'internal':False} )
>>> dir(ev)
[]
>>> ev.__dict__
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    ev.__dict__
AttributeError: event member not defined
>>> ev.type
24
>>> ev.id
'test'
>>> ev.internal
False
>>> repr(ev)
"<Event(24-UserEvent {'internal': False, 'id': 'test'})>"
>>> 

Contrast that with how other Python objects behave:

>>> class junk:
fee = 1
fie = True
foe = 'giant'
fum = (1,2,3)

>>> j1 = junk()
>>> repr(j1)
'<__main__.junk object at 0x034FA870>'
>>> dir(j1)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'fee', 'fie', 'foe', 'fum']


See what I mean?  With other Python objects, it is easy to enumerate the attributes of an object.  But it is not obvious how to do that for pygame event objects (though repr() somehow manages it).

Does anyone know how repr() manages to find the attributes for the pygame event, since dir() doesn't work?

Dave


On Sat, Feb 5, 2011 at 7:00 PM, Greg Ewing <greg.ewing-F+z8Qja7x9Xokq/tPzqvJg@public.gmane.org> wrote:
Sam Bull wrote:

But, from a conversation with someone else about this, it was suggested
that this could complicate the game code, and it would be better to use
events in order to keep the game code and GUI code separated.

It's not necessary to use pygame events to achieve that. Techniques
for keeping GUI and application code decoupled are well known. Google
"MVC" for more information.

The only good reason I can think of for using events this way is if
for some reason you need to handle the event asynchronously, i.e. you
need to ensure that all processing for the current event is completed
before carrying out the action. Otherwise, using an event is probably
just needless complication.

--
Greg


Gmane