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.
- def main():
- import pygame
- pygame.init()
- width = 500
- height = 500
- white = (255,255,255)
- state = 0
- right = 0
- down = 0
- left = width
- up = height
- step = .5
- main_surface = pygame.display.set_mode((width, height))
- redSquare = pygame.image.load(“redSquare.png”)
- while True:
- ev = pygame.event.poll() # Look for any event
- if ev.type == pygame.QUIT: # Window close button clicked?
- break # … leave game loop
- main_surface.fill(white)
- if state == 0:
- right = right + step
- main_surface.blit(redSquare, (right,0))
- if right >= width – 20:
- right = 0
- state = 1
- elif state == 1:
- down = down + step
- main_surface.blit(redSquare, (width – 20, down))
- if down >= height – 20:
- down = 0
- state = 2
- elif state == 2:
- left = left – step
- main_surface.blit(redSquare, (left – 20, height – 20))
- if left <= 20:
- left = width
- state = 3
- elif state == 3:
- up = up – step
- main_surface.blit(redSquare, (0, up – 20))
- if up <= 20:
- up = height
- state = 0
- pygame.display.flip()
- pygame.quit() # Once we leave the loop, close the window.
- main()
With graphics.py.
- from graphics import *
- win = GraphWin(‘Screen’, 500,500)
- def main():
- width = 500
- height = 500
- state = 0
- right = 0
- down = 0
- left = width
- up = height
- step = .5
- while True:
- if state == 0:
- right = right + step
- rect = Rectangle(Point(right,0),Point(right+20,20))
- rect.setFill(“red”)
- rect.draw(win)
- if right >= width – 20:
- right = 0
- state = 1
- elif state == 1:
- down = down + step
- rect = Rectangle(Point(width – 20,down),Point(width,down + 20))
- rect.setFill(“red”)
- rect.draw(win)
- if down >= height – 20:
- down = 0
- state = 2
- elif state == 2:
- left = left – step
- rect = Rectangle(Point(left – 20,height – 20),Point(left,width))
- rect.setFill(“red”)
- rect.draw(win)
- if left <= 20:
- left = width
- state = 3
- elif state == 3:
- up = up – step
- rect = Rectangle(Point(0, up – 20),Point(20,up))
- rect.setFill(“red”)
- rect.draw(win)
- if up <= 20:
- up = height
- state = 0
- main()
With just the native turtle. (Thanks to Dan Schellenberg.)
- import turtle
- theWindow = turtle.Screen() #theWindow.setup(width=400, height=400)
- bob = turtle.Turtle()
- bob.shape(“square”)
- bob.penup()
- bob.speed(0)
- #determine edges
- edgeBuffer = 20
- rightEdge = theWindow.window_width()/2 – edgeBuffer
- leftEdge = theWindow.window_width()/-2 + edgeBuffer – 10 #the 10 is due to the way that python turtles
- topEdge = theWindow.window_height()/2 – edgeBuffer + 10 #draw the square shape…
- bottomEdge = theWindow.window_height()/-2 + edgeBuffer
- #move to starting location and set original state
- bob.setpos(leftEdge,topEdge)
- bob.setheading(0) #facing east
- speed = 5
- state = 0
- while True:
- if state == 0:
- bob.setx( bob.xcor() + speed )
- if bob.xcor() >= rightEdge:
- state = 1
- elif state == 1:
- bob.sety( bob.ycor() – speed )
- if bob.ycor() <= bottomEdge:
- state = 2
- elif state == 2:
- bob.setx( bob.xcor() – speed )
- if bob.xcor() <= leftEdge:
- state = 3
- elif state == 3:
- bob.sety( bob.ycor() + speed )
- if bob.ycor() >= topEdge:
- state = 0
- theWindow.mainloop()
March 24, 2016 at 4:39 am |
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 )
March 24, 2016 at 1:49 pm |
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.