<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Hello, <br><br>I changed a lot because of your suggestions.<br><br>But one thing is still a puzzle.<br><br>The robots don't move anymore.<br><br>What I have is this :<br><br>#<br># robots.py<br>#<br>from gasp import *<br><br>SCREEN_WIDTH = 640<br>SCREEN_HEIGHT = 480<br>GRID_WIDTH = SCREEN_WIDTH/10 - 1<br>GRID_HEIGHT = SCREEN_HEIGHT/10 - 1<br><br><br>def place_player():<br> # x = random.randint(0, GRID_WIDTH)<br> # y = random.randint(0, GRID_HEIGHT)<br> x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2<br> return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, 'y': y}<br><br>def place_robot(x, y, junk=False):<br> return {'shape': Box((10*x, 10*y), 10, 10, filled = junk), 'x': x, 'y': y}<br><br><br>def place_robots(numbots):<br> robots=[]<br> # for i in range(numbots):<br> # x = random.randint(0, GRID_WIDTH)<br> # y = random.randint(0, GRID_HEIGHT)<br> # robots.append(place_robot(x, y))<br> robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, junk = False))<br> robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, junk = False))<br> return robots<br><br>def move_player(player):<br> update_when('key_pressed')<br> if key_pressed('escape'):<br> return True<br> elif key_pressed('4'):<br> if player['x'] > 0: player['x'] -= 1<br> elif key_pressed('7'):<br> if player['x'] > 0: player['x'] -= 1<br> if player['y'] < GRID_HEIGHT: player['y'] += 1<br> elif key_pressed('8'):<br> if player['y'] < GRID_HEIGHT: player['y'] += 1<br> elif key_pressed('9'):<br> if player['x'] < GRID_WIDTH: player['x'] += 1<br> if player['y'] < GRID_HEIGHT: player['y'] += 1<br> elif key_pressed('6'):<br> if player['x'] < GRID_WIDTH: player['x'] += 1<br> elif key_pressed('3'):<br> if player['x'] < GRID_WIDTH: player['x'] += 1<br> if player['y'] > 0: player['y'] -= 1<br> elif key_pressed('2'):<br> if player['y'] > 0: player['y'] -= 1<br> elif key_pressed('1'):<br> if player['x'] > 0: player['x'] -= 1<br> if player['y'] > 0: player['y'] -= 1<br> elif key_pressed('0'):<br> player['x'] = random.randint(0, GRID_WIDTH)<br> player['y'] = random.randint(0, GRID_HEIGHT)<br> else:<br> return False<br><br> move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))<br><br> return False<br><br>def collided(thing1, thing2):<br> return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']<br><br>def check_collisions(robots, junk, player):<br> # check whether player has collided with anything<br> for thing in robots + junk:<br> if collided(thing, player):<br> return True<br> return False<br><br><br><br>def move_robot(robot, player):<br> if robot['x'] < player['x']: robot['x'] += 1<br> elif robot['x'] > player['x']: robot['x'] -= 1<br><br> if robot['y'] < player['y']: robot['y'] += 1<br> elif robot['y'] > player['y']: robot['y'] -= 1<br><br> move_to(robot['shape'], (10*robot['x'], 10*robot['y']))<br><br>def move_robots(robots, player):<br> for robot in robots:<br> move_robot(robot, player)<br><br><br>def play_game():<br> begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)<br> player = place_player()<br> robots = []<br> place_robots(4)<br> junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk=True )]<br> defeated = False<br><br> while not defeated:<br> quit = move_player(player)<br> if quit:<br> break<br> move_robots(robots, player)<br> defeated = check_collisions(robots, junk, player)<br><br> if defeated:<br> remove_from_screen(player['shape'])<br> for thing in robots + junk:<br> remove_from_screen(thing['shape'])<br> Text("They got you!", (240, 240), size=32)<br> sleep(3)<br><br> end_graphics()<br><br><br><br>if __name__ == '__main__':<br> play_game()<br><br>Roelof<br><br><br>> To: tutor@python.org<br>> From: alan.gauld@btinternet.com<br>> Date: Fri, 17 Sep 2010 01:14:32 +0100<br>> Subject: Re: [Tutor] robots question<br>> <br>> <br>> "Roelof Wobben" <rwobben@hotmail.com> wrote<br>> <br>> <br>> #<br>> # robots.py<br>> <br>> This is pretty weird code, there are several odd things in it.<br>> <br>> def place_player():<br>> # x = random.randint(0, GRID_WIDTH)<br>> # y = random.randint(0, GRID_HEIGHT)<br>> x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2<br>> return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, <br>> 'y': y}<br>> <br>> So this returns a dictionary which always contains the same data.<br>> <br>> def place_robot(x,y, junk):<br>> x = random.randint(0, GRID_WIDTH)<br>> y = random.randint(0, GRID_HEIGHT)<br>> return {'shape': Box((10*x, 10*y), 10, 10), 'x': x, 'y': y}<br>> <br>> This returns a similar dict but with random data.<br>> It ignores the values of x and y passed in and does not use junk at <br>> all.<br>> <br>> def place_robots(numbots):<br>> robots = []<br>> # for i in range(numbots):<br>> # x = random.randint(0, GRID_WIDTH)<br>> # y = random.randint(0, GRID_HEIGHT)<br>> # robots.append(place_robot(x, y))<br>> robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, <br>> junk= False))<br>> robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, <br>> junk = False))<br>> print type(robots)<br>> return robots<br>> <br>> This returns a list of 2 dictionaries. The x,y parameters are ignored <br>> by the function.<br>> <br>> <br>> def move_player(player):<br>> update_when('key_pressed')<br>> if key_pressed('escape'):<br>> return True<br>> elif key_pressed('4'): ...<br>> else:<br>> return False<br>> move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))<br>> return False<br>> <br>> This seems OK, it returns True for escape otherwise False.<br>> <br>> def collided(thing1, thing2):<br>> return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']<br>> <br>> This returns a boolean<br>> <br>> <br>> def check_collisions(robots, junk, player):<br>> # check whether player has collided with anything<br>> for thing in robots + junk:<br>> if collided(thing, player):<br>> return True<br>> return False<br>> <br>> Could be simplified to just<br>> <br>> for thing in robots + junk:<br>> return collided(thing, player)<br>> <br>> It requires that robots and junk are capable of being added together<br>> and the result being iterable.<br>> <br>> def move_robot(robot, player):<br>> if robot['x'] < player['x']: robot['x'] += 1<br>> elif robot['x'] > player['x']: robot['x'] -= 1<br>> <br>> if robot['y'] < player['y']: robot['y'] += 1<br>> elif robot['y'] > player['y']: robot['y'] -= 1<br>> <br>> move_to(robot['shape'], (10*robot['x'], 10*robot['y']))<br>> <br>> I don't see move_to so assume its part of the module you imported?<br>> <br>> def move_robots(robots, player):<br>> for robot in robots:<br>> move_robot(robot, player)<br>> <br>> ok<br>> <br>> <br>> def play_game():<br>> begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)<br>> player = place_player()<br>> robot = place_robots(4)<br>> junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]<br>> robots = []<br>> defeated = False<br>> <br>> So at this point<br>> player is a dict<br>> robot is a list of 2 dicts<br>> junk is a list of one dict<br>> robots is an empty list<br>> <br>> <br>> while not defeated:<br>> quit = move_player(player)<br>> if quit:<br>> break<br>> move_robots(robots, player)<br>> print "type robots", type(robots)<br>> print "type junk", type(junk)<br>> print "type player", type(player)<br>> defeated = check_collisions(robots, player, junk)<br>> <br>> You now call check_collisions passing an empty list and a dict and a <br>> list of a dict<br>> The order in the definition is:<br>> <br>> def check_collisions(robots, junk, player):<br>> <br>> so it looks like you swapped the last two arguments<br>> <br>> <br>> And now Im getting this message :<br>> <br>> ** Message: pygobject_register_sinkfunc is deprecated (GtkWindow)<br>> ** Message: pygobject_register_sinkfunc is deprecated (GtkInvisible)<br>> ** Message: pygobject_register_sinkfunc is deprecated (GtkObject)<br>> <type 'list'><br>> <br>> Not sure where that lot came from...<br>> <br>> type robotsTraceback (most recent call last):<br>> <type 'list'><br>> type junk <type 'list'><br>> type player <type 'dict'><br>> File "/root/workspace/test2/src/test.py", line 125, in <module><br>> play_game()<br>> File "/root/workspace/test2/src/test.py", line 111, in play_game<br>> defeated = check_collisions(robots, player, junk)<br>> File "/root/workspace/test2/src/test.py", line 74, in <br>> check_collisions<br>> for thing in robots + junk:<br>> TypeError: can only concatenate list (not "dict") to list<br>> <br>> But this is valid because of the swapped arguments.<br>> <br>> > So far I can see the problem is that player is a dict and the rest <br>> > is a list.<br>> > Is this the correct conclusion ?<br>> <br>> Yes, but you missed the fact that you changed the order of the <br>> arguments.<br>> When you get type errors check the types at your interfaces(functions, <br>> classes etc)<br>> match the definitions.<br>> <br>> -- <br>> Alan Gauld<br>> Author of the Learn to Program web site<br>> http://www.alan-g.me.uk/<br>> <br>> <br>> _______________________________________________<br>> Tutor maillist - Tutor@python.org<br>> To unsubscribe or change subscription options:<br>> http://mail.python.org/mailman/listinfo/tutor<br>                                            </body>
</html>