The continuing sage of moving a square around the screen using a state variable with Python

This is fun just finding different ways to do the same simple problem.  After getting pygame working (3 hours?  Again, I am not bright, just stubborn. ) the code was 60 minutes.  I had to look up how to use pygame and found some good examples.  (blit?  Who came up with blit?)  I had to chase down the usual weird things in the code but no big deal.  Being the stone-cold idiot I am I figured if I can get this working with pygame.py I should learn how to do it with graphics.py.  It took me a while (really stubborn) to figure out to copy the graphics class to a text file, rename it .py then drop it into the Lib>site-packages folder in the Python folder.  Nowhere does any clever soul give that little detail.  The directions just say “put this where Python can see it.”  Not good.  Anyway once I got the messy setup details figured out the code with the graphics.py was 30 minutes.  Again I found a nice example.  It still has a minor drawing issue and gets slower with each lap.  Tonight’s entertainment.  I cancelled cable TV so this is my replacement.  (I have to get cable TV back, this stuff is going to make me go blind.)  Dan Schellenberg, where I got the square idea in the first place, picked up on the “let’s do it in Python” idea and has three variations.  The third one listed here is one of his.

Now I stare at these and think “how anal do I really want to get with this?”  I look at the code in those 4 if statements.  They are all the same except for the parameters.  I think I could make that a procedure and pass parameters …  Hummmm.

I really need to do more of these.  All my Python is learned on the job.  For that matter all my coding is learned on the job (except for the 2 FORTRAN courses in ‘71 and ‘80, and one intro Java course in ‘90).  This leads to a rather sketchy foundation and almost no breadth.  I can do what is in the book I am using and only to the chapters I have had time to learn.  Projects like this have no chapter and have no solution determined by the chapter you are reading at the time.

I now have 5 good solutions to this one problem.  Two mine and 3 Dan’s.  Anybody have any more that are simple or have refinement suggestions I could learn from?

 

With pygame.py.

  1. def main():
  2.     import pygame
  3.     pygame.init()
  4.     width = 500
  5.     height = 500
  6.     white = (255,255,255)
  7.     state = 0
  8.     right = 0
  9.     down = 0
  10.     left = width
  11.     up = height
  12.     step = .5
  13.     main_surface = pygame.display.set_mode((width, height))
  14.     redSquare = pygame.image.load(“redSquare.png”)
  15.     while True:
  16.         ev = pygame.event.poll() # Look for any event
  17.         if ev.type == pygame.QUIT: # Window close button clicked?
  18.             break # … leave game loop
  19.         main_surface.fill(white)
  20.         if state == 0:
  21.             right = right + step
  22.             main_surface.blit(redSquare, (right,0))
  23.             if right >= width – 20:
  24.                 right = 0
  25.                 state = 1
  26.         elif state == 1:
  27.             down = down + step
  28.             main_surface.blit(redSquare, (width – 20, down))
  29.             if down >= height – 20:
  30.                 down = 0
  31.                 state = 2
  32.         elif state == 2:
  33.             left = left – step
  34.             main_surface.blit(redSquare, (left – 20, height – 20))
  35.             if left <= 20:
  36.                 left = width
  37.                 state = 3
  38.         elif state == 3:
  39.             up = up – step
  40.             main_surface.blit(redSquare, (0, up – 20))
  41.             if up <= 20:
  42.                 up = height
  43.                 state = 0
  44.         pygame.display.flip()
  45.     pygame.quit() # Once we leave the loop, close the window.
  46. main()

With graphics.py.

  1. from graphics import *
  2. win = GraphWin(‘Screen’, 500,500)
  3. def main():
  4.     width = 500
  5.     height = 500
  6.     state = 0
  7.     right = 0
  8.     down = 0
  9.     left = width
  10.     up = height
  11.     step = .5
  12.     while True:
  13.         if state == 0:
  14.             right = right + step
  15.             rect = Rectangle(Point(right,0),Point(right+20,20))
  16.             rect.setFill(“red”)
  17.             rect.draw(win)
  18.             if right >= width – 20:
  19.                 right = 0
  20.                 state = 1
  21.         elif state == 1:
  22.             down = down + step
  23.             rect = Rectangle(Point(width – 20,down),Point(width,down + 20))
  24.             rect.setFill(“red”)
  25.             rect.draw(win)
  26.             if down >= height – 20:
  27.                 down = 0
  28.                 state = 2
  29.         elif state == 2:
  30.             left = left – step
  31.             rect = Rectangle(Point(left – 20,height – 20),Point(left,width))
  32.             rect.setFill(“red”)
  33.             rect.draw(win)
  34.             if left <= 20:
  35.                 left = width
  36.                 state = 3
  37.         elif state == 3:
  38.             up = up – step
  39.             rect = Rectangle(Point(0, up – 20),Point(20,up))
  40.             rect.setFill(“red”)
  41.             rect.draw(win)
  42.             if up <= 20:
  43.                 up = height
  44.                 state = 0
  45. main()

With just the native turtle.  (Thanks to Dan Schellenberg.)

  1. import turtle
  2. theWindow = turtle.Screen()          #theWindow.setup(width=400, height=400)
  3. bob = turtle.Turtle()
  4. bob.shape(“square”)
  5. bob.penup()
  6. bob.speed(0)
  7. #determine edges
  8. edgeBuffer = 20
  9. rightEdge = theWindow.window_width()/2 – edgeBuffer
  10. leftEdge = theWindow.window_width()/-2 + edgeBuffer – 10  #the 10 is due to the way that python turtles
  11. topEdge = theWindow.window_height()/2 – edgeBuffer + 10   #draw the square shape…
  12. bottomEdge = theWindow.window_height()/-2 + edgeBuffer
  13. #move to starting location and set original state
  14. bob.setpos(leftEdge,topEdge)
  15. bob.setheading(0)                          #facing east
  16. speed = 5
  17. state = 0
  18. while True:
  19.   if state == 0:
  20.     bob.setx( bob.xcor() + speed )
  21.     if bob.xcor() >= rightEdge:
  22.       state = 1
  23.   elif state == 1:
  24.     bob.sety( bob.ycor() – speed )
  25.     if bob.ycor() <= bottomEdge:
  26.       state = 2
  27.   elif state == 2:
  28.     bob.setx( bob.xcor() – speed )
  29.     if bob.xcor() <= leftEdge:
  30.       state = 3
  31.   elif state == 3:
  32.     bob.sety( bob.ycor() + speed )
  33.     if bob.ycor() >= topEdge:
  34.       state = 0
  35. theWindow.mainloop()
Advertisements

2 Responses to “The continuing sage of moving a square around the screen using a state variable with Python”

  1. gasstationwithoutpumps Says:

    I’d be tempted to do a more physics-like simulation, with
    velocity = (delta_x, 0)
    position = [init_x, init_y]

    while True:
    position = [position[0]+velocity[0], position[1]+velocity[1]]
    if velocity[0]>0 and position[0]>right_edge:
    position = [right_edge, top_edge]
    velocity= (0, -delta_x)
    elif …

    Of course, it is even easier if you include a package that has vectors so you can just do
    position += speed*delta_t

    I like the Vpython package for physics-like simulations. (see http://vpython.org )

  2. gflint Says:

    VPython does look interesting. I actually found directions on how to get it to work with Python 3.4 but it is not a simple process. These different versions of Python are just a pain.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: