Friday, March 18, 2011

Mar 18

from gasp import *

number = random_between(1, 1000)
guesses = 1
guess = input("Guess the number between 1 and 1000: ")

while guess != number:
if guess > number:
print "Too high!"
else:
print "Too low!"
guess = input("Guess the number between 1 and 1000: ")
guess += 1

print "\n\nCongratulations, you get it in %d guesses!\n\n" % guesses
When I inputed the above program i had to guess a few times in order to find the number that was randomly picked, which i got 719 as my number between 1 - 1000. 
from gasp import *

number = random_between(1, 1000)
guesses = 0

while True:
guess = input("Guess the number between 1 and 1000: ")
guesses += 1
if guess > number:
print "Too high!"
elif guess < number:
print "Too low!"
else:
print "\n\nCongratulations, you got it in %d guesses!\n\n" % guesses
break
This is an update of the first program that will give the accurate results of how many guesses it took you to guess the right number. This I say is a very cool program cause it functions as a person as to picking a random number and asking you to find the right number and pops out the amount of times you had too guess in order to get the right number. 
from gasp import *

def distance(x1, y1, x2, y2):
return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
begin_graphics(800, 600, title="Catch", background=color.RED)
set_speed(120)

ball1_x = 10
ball1_y = 300
ball1 = Circle((ball1_x, ball1_y), 10, filled=True)
ball1_dx = 4

ball2_x = 790
ball2_y = 300
ball2 = Circle((ball2_x, ball2_y), 10)
ball2_dx = -4

while ball1_x < 810:
ball1_x += ball1_dx
ball2_x += ball2_dx
move_to(ball1, (ball1_x, ball1_y))
move_to(ball2, (ball2_x, ball2_y))
if distance(ball1_x, ball1_y, ball2_x, ball2_y) <= 20:
remove_from_screen(ball1)
remove_from_screen(ball2)
break
update_when('next_tick')
sleep(1)
end_graphics()
The game being created from this program I'm starting to see is starting to get more and more complicated. This simple game is pretty easy to understand. 
from gasp import *

def distance(x1, y1, x2, y2):
return((x2 - x1)**2 + (y2 - y1)**2)**0.5
begin_graphics(800, 600, title="Catch", background=color.RED)
set_speed(120)

ball_x = 10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4
dy = random_between(-4, 4)

mitt_x = 780
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 20)

while True:
# move the ball
if ball_y >= 590 or ball_y <= 10:
dy *= -1
ball_x += dx
if ball_x > 810:     # the ball has gone off the screen
break
ball_y += dy
move_to(ball, (ball_x, ball_y))
# check on the mitt
if key_pressed('k') and mitt_y <= 580:
mitt_y += 5
elif key_pressed('j') and mitt_y >= 20:
mitt_y -= 5
if key_pressed('escape'):
break 
move_to(mitt, (mitt_x, mitt_y))
if distance(ball_x, ball_y, mitt_x, mitt_y) <= 30:   # ball is caught
remove_from_screen(ball)
break 
update_when('next_tick')
end_graphics()
When running the above program, I got the following error:
Exception in thread QueueFeederThread (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
  File "/usr/lib/python2.6/threading.py", line 484, in run
  File "/usr/lib/python2.6/multiprocessing/queues.py", line 242, in _feed <class 'cPickle.PicklingError'>: Can't pickle gasp.api.Circle: import of module gasp.api failed
I believe this is a error off of gasp but I can't tell. 

1 comment:

  1. Assignment for the beginning of this week:

    1. Create a dropbox account for yourself, if you don't already have one.

    2. Complete the pong.py project at the end of Chapter 8.

    3. Copy your solution to the public folder inside your dropbox.

    Put a link to it, together with a narrative describing what you did to produce it, in your technical journal (blog).

    ReplyDelete