Item collection w/ list walk-through

From WLCS

Item Collection without Lists

from gasp import *

def distance(x1, y1, x2, y2):
    return ((x2-x1)**2 + (y2-y1)**2)**0.5

begin_graphics(400, 300, title="Item Collection", background=color.WHITE)
set_speed(120)

#creates player
player_x = 200
player_y = 150
player = Circle((player_x, player_y), 20)

numItems = 3
numCollected = 0

item_x0 = random_between(0,400) 
item_y0 = random_between(0,300) 
item_shape0= Circle((item_x0, item_y0), 5, filled=True)

item_x1 = random_between(0,400)
item_y1 = random_between(0,300)
item_shape1= Circle((item_x1, item_y1), 5, filled=True)

item_x2 = random_between(0,400)
item_y2 = random_between(0,300)
item_shape2= Circle((item_x2, item_y2), 5, filled=True)

while True:
    if key_pressed('up'):
        player_y += 5
    if key_pressed('down'):
        player_y -= 5
    if key_pressed('right'):
        player_x += 5
    if key_pressed('left'):
        player_x -= 5

    if key_pressed('escape'):
        break
    
    if distance(player_x, player_y, item_x0, item_y0) < 25:
        item_x0 = 1000
        item_y0 = 1000
        remove_from_screen(item_shape0)
        numCollected += 1

    if distance(player_x, player_y, item_x1, item_y1) < 25:
        item_x1 = 1000
        item_y1 = 1000
        remove_from_screen(item_shape1)
        numCollected += 1
        
    if distance(player_x, player_y, item_x2, item_y2) < 25:
        item_x2 = 1000
        item_y2 = 1000
        remove_from_screen(item_shape2)
        numCollected += 1

    #check if you've collected all the items
    if numCollected == numItems:
        print "You win!"
        break

    move_to(player, (player_x, player_y))
    update_when('next_tick')

end_graphics()

Item Collection with Lists

from gasp import *

def distance(x1, y1, x2, y2):
    return ((x2-x1)**2 + (y2-y1)**2)**0.5

begin_graphics(400, 300, title="Item Collection", background=color.WHITE)
set_speed(120)

player_x = 200
player_y = 150
player = Circle((player_x, player_y), 20)

numItems = 3
numCollected = 0

item_x = [] #stores all x-coordinates
item_y = [] #stores all y-coordinates
item_shape = [] #stores all shapes

#loops through 3 times and appends numbers and 
#shapes to the lists
i = 0
while i < numItems:
    item_x.append(random_between(0,400))
    item_y.append(random_between(0,300))
    item_shape.append(Circle((item_x[i], item_y[i]), 5, filled=True))
    i = i + 1

while True:
    if key_pressed('up'):
        player_y += 5
    if key_pressed('down'):
        player_y -= 5
    if key_pressed('right'):
        player_x += 5
    if key_pressed('left'):
        player_x -= 5

    if key_pressed('escape'):
        break
    
    #loop through multiple times to test collision
    #of each of the items
    i = 0
    while i < len(item_shape):
        if distance(player_x, player_y, item_x[i], item_y[i]) < 25:
            item_x[i] = 1000
            item_y[i] = 1000
            remove_from_screen(item_shape[i])
            numCollected += 1
        i = i + 1

    #check if you've collected all the items
    if numCollected == numItems:
        print "You win!"
        break

    move_to(player, (player_x, player_y))
    update_when('next_tick')

end_graphics()