Re: [Tutor] FW: robots question

2010-09-16 Thread Peter Otten
Roelof Wobben wrote:

> I change everything to this :

> def check_collisions(robots, junk, player):

> defeated = check_collisions(robots, player, junk)

Do you see the problem?

> print "type robots", type(robots)
> print "type junk", type(junk)
> print "type player", type(player)

Adding print statements for debugging purposes is a good approach.

> And now Im getting this message :
> 
> ** Message: pygobject_register_sinkfunc is deprecated (GtkWindow)
> ** Message: pygobject_register_sinkfunc is deprecated (GtkInvisible)
> ** Message: pygobject_register_sinkfunc is deprecated (GtkObject)
> 
> type robotsTraceback (most recent call last):
>  
> type junk 
> type player 
>   File "/root/workspace/test2/src/test.py", line 125, in 
> play_game()
>   File "/root/workspace/test2/src/test.py", line 111, in play_game
> defeated = check_collisions(robots, player, junk)
>   File "/root/workspace/test2/src/test.py", line 74, in check_collisions
> for thing in robots + junk:
> TypeError: can only concatenate list (not "dict") to list
> 
> So far I can see the problem is that player is a dict and the rest is a
> list.
> Is this the correct conclusion ?

It may be correct but it's a low-level view. A more appropriate description 
would be that you are trying to concatenate a list of robots with a player.

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: robots question

2010-09-16 Thread Roelof Wobben



From: rwob...@hotmail.com
To: __pete...@web.de
Subject: RE: [Tutor] robots question
Date: Thu, 16 Sep 2010 17:43:41 +








Hello , 

I change everything to this :

#
# robots.py
#
from gasp import *

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
GRID_WIDTH = SCREEN_WIDTH/10 - 1
GRID_HEIGHT = SCREEN_HEIGHT/10 - 1


def place_player():
# x = random.randint(0, GRID_WIDTH)
# y = random.randint(0, GRID_HEIGHT)
x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2
return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, 'y': y}

def place_robot(x,y, junk):
x = random.randint(0, GRID_WIDTH)
y = random.randint(0, GRID_HEIGHT)
return {'shape': Box((10*x, 10*y), 10, 10), 'x': x, 'y': y}


def place_robots(numbots):
robots = []
# for i in range(numbots):
#x = random.randint(0, GRID_WIDTH)
#y = random.randint(0, GRID_HEIGHT)
#robots.append(place_robot(x, y))
robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, junk= False))
robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, junk = 
False))
print type(robots)
return robots

def move_player(player):
update_when('key_pressed')
if key_pressed('escape'):
return True
elif key_pressed('4'):
if player['x'] > 0: player['x'] -= 1
elif key_pressed('7'):
if player['x'] > 0: player['x'] -= 1
if player['y'] < GRID_HEIGHT: player['y'] += 1
elif key_pressed('8'):
if player['y'] < GRID_HEIGHT: player['y'] += 1
elif key_pressed('9'):
if player['x'] < GRID_WIDTH: player['x'] += 1
if player['y'] < GRID_HEIGHT: player['y'] += 1
elif key_pressed('6'):
if player['x'] < GRID_WIDTH: player['x'] += 1
elif key_pressed('3'):
if player['x'] < GRID_WIDTH: player['x'] += 1
if player['y'] > 0: player['y'] -= 1
elif key_pressed('2'):
if player['y'] > 0: player['y'] -= 1
elif key_pressed('1'):
if player['x'] > 0: player['x'] -= 1
if player['y'] > 0: player['y'] -= 1
elif key_pressed('0'):
   player['x'] = random.randint(0, GRID_WIDTH)
   player['y'] = random.randint(0, GRID_HEIGHT)
else:
return False

move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))

return False

def collided(thing1, thing2):
return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']

def check_collisions(robots, junk, player):
# check whether player has collided with anything
for thing in robots + junk:
if collided(thing, player):
return True
return False



def move_robot(robot, player):
if robot['x'] < player['x']: robot['x'] += 1
elif robot['x'] > player['x']: robot['x'] -= 1

if robot['y'] < player['y']: robot['y'] += 1
elif robot['y'] > player['y']: robot['y'] -= 1

move_to(robot['shape'], (10*robot['x'], 10*robot['y']))

def move_robots(robots, player):
for robot in robots:
move_robot(robot, player)


def play_game():
begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)
player = place_player()
robot = place_robots(4)
junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]
robots = []
defeated = False

while not defeated:
quit =  move_player(player)
if quit:
break
move_robots(robots, player)
print "type robots", type(robots)
print "type junk", type(junk)
print "type player", type(player)
defeated = check_collisions(robots, player, junk)

if defeated:
remove_from_screen(player['shape'])
for thing in robots + junk:
remove_from_screen(thing['shape'])
Text("They got you!", (240, 240), size=32)
sleep(3)

end_graphics()



if __name__ == '__main__':
play_game()


And now Im getting this message :

** Message: pygobject_register_sinkfunc is deprecated (GtkWindow)
** Message: pygobject_register_sinkfunc is deprecated (GtkInvisible)
** Message: pygobject_register_sinkfunc is deprecated (GtkObject)

type robotsTraceback (most recent call last):
 
type junk 
type player 
  File "/root/workspace/test2/src/test.py", line 125, in 
play_game()
  File "/root/workspace/test2/src/test.py", line 111, in play_game
defeated = check_collisions(robots, player, junk)
  File "/root/workspace/test2/src/test.py", line 74, in check_collisions
for thing in robots + junk:
TypeError: can only concatenate list (not "dict") to list

So far I can see the problem is that player is a dict and the rest is a list.

Is this the correct conclusion ?

Roelof


> To: tutor@python.org
> From: __pete...@web.de
> Date: Thu, 16 Sep 2010 18:10:13 +0200
> Subject: Re: [Tutor] robots question
> 
> Roelof Wobben wrote:
> 
> > As a exercise from this book ( Thinking like a computer scientist ) I have
> > to make this programm on this
> > page(http://openbookproject.net/thinkcs/python/english2e/ch12.html)
> > Exercise 11
> 
> > def check_collisions(robots, junk, pl