[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Function for keyboard movement



Hello Bob,

 

So it seems that Blitz Basic has a different way to handle collisions. Youâre saying to the ifImagesCollide function, âHere, Iâm going to give you a long list of these attributes, and you figure it all out.â

 

The Pygame way is different because you just give the equivalent collision function merely two Pygame sprites, and it tells you if youâve collided:

 

from pygame import *

...

did_we_collide = collide_rect( sprite_a, sprite_b ) # (Collision documentation)

 

But to get to this point, you need to create two sprite objects, which have class member functions to update and draw themselves. (Sprite documentation)

 

 

In Pygame Zero, which I mentioned in my previous post, sprite creation and keyboard checking is simplified.

 

For the keyboard, you have an optional update() function, and if you use it, then you would code your keyboard polling like this:

 

def update():

    if keyboard.left:
        alien.x -= 1
    elif keyboard.right:
        alien.x += 1


Alternatively, you could code it like this:

 

keyboard[keys.A] # True if the 'A' key is pressed

keyboard[keys.SPACE]  # True if the space bar is pressed
In each case, the check is just one line. (Documentation)
Sprites are wrapped up and called Actors, and itâs easier to understand how to use them than to explain Python inheritance and method member functions. 
In Pygame Zero, itâs:
alien = Actor('alien', (50, 50))
And then there are a small number of well-defined functions where you can put your code, such as having one draw function and one update function.
def draw():
    screen.clear()
    alien.draw()
This is all Python and Pygame under the hood, but it cleans up the path so itâs easier for newcomers to understand.
Hope this gives you a different perspective.

~ Michael

 


On Wed : Oct 7, 2015 2:41:24 PM you wrote:

> Hi guys,

>

> Thanks for all the ideas..... I apologize if I wasn't clear enough in my OP.

>

> Since we found that collisions and rects are a stretch for beginning

> programmers, my colleague came up with the following code:

>

> def imagesCollide(pic1,p1x,p1y,pic2,p2x,p2y):

> aRect = pic1.get_rect(center=[p1x,p1y])

> bRect = pic2.get_rect(center=[p2x,p2y])

>

> if aRect.colliderect(bRect):

> return True;

> else:

> return False;

>

>

> So that allows us to replicate what we used last year in Blitz Basic, which

> was

>

> ifImagesCollide(pic1,pic1x,pic1y, pic2, pic2x, pic2y)

>

> One line of code! We (the other teacher and I understand rects but feel

> that the time spent explaining them was not commensurate with the value the

> students get at this level.

>

> As for keyboard movement, in BlitzBasic it's

>

> if keydown(right) then....

>

> One line again! So no need to introduce the for event loop, etc.

>

> We are making the switch to Python/Pygame because no one has ever heard of

> Blitz Basic, and we thought it was perhaps time to update, since most of

> the world is moving to Python for introducing text-based coding.

>

> However, some of these things make us go hmmm....

>

> Thanks in advance,

> Bob Irving

> Porter-Gaud School

> Charleston, SC

>

>

>

> On Wed, Oct 7, 2015 at 1:37 PM, <rockachu2@xxxxxxxxx> wrote:

>

> > Could you give us an example of what you have in basic?

> >

> >

> >

> >

> > On Oct 6, 2015, at 13:12, Bob Irving <bobirv@xxxxxxxxx> wrote:

> >

> > Hi folks,

> >

> > We're introducing Python for 9th grade this year and moving from Blitz

> > Basic for game programming. Just wondering if anyone has come up with a way

> > to hide the complexity for keyboard movement..... it's one of those things

> > that makes sense to more advanced programmers but not to beginners. We've

> > experimented with making a few functions to detect collisions, mouse

> > clicks....

> >

> > TIA,

> > Bob Irving

> > Porter-Gaud School

> > Charleston, SC

> >

> > --

> > Twitter: @birv2

> > www.bob-irving.com

> > http://www.scoop.it/t/on-the-digital-frontier

> >

> >

>

>

>