robots-2
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| robots-2 [2023/09/27 00:39] – created appledog | robots-2 [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | = Robots Part 2 | ||
| - | This part of the code was taught to Roger on September 26th. | ||
| - | == Rocks and Robots | ||
| - | The first thing we want to do is add one robot and one rock to make the game world feel less lonely and work on the rules of the game. If the game works with one, it can work with more. This is induction! | ||
| - | |||
| - | == Adding Rocks | ||
| - | After the code which creates the map in __init__() add this: | ||
| - | |||
| - | < | ||
| - | # Add rocks. | ||
| - | self.gameMap[7][7] = ' | ||
| - | </ | ||
| - | |||
| - | There, we have added a rock to the map. | ||
| - | |||
| - | If you want to get fancy you can add it randomly (or add a second, random rock!) as follows: | ||
| - | |||
| - | < | ||
| - | # Add rocks. | ||
| - | self.gameMap[7][7] = ' | ||
| - | | ||
| - | # Add a rock randomly | ||
| - | rx = random.randint(1, | ||
| - | ry = random.randint(1, | ||
| - | self.gameMap[ry][rx] = ' | ||
| - | </ | ||
| - | |||
| - | Next, we need to make sure that if the player hits the rock, he will die. | ||
| - | |||
| - | To do this we add an elif to movePlayer(). Add the following elif to the if condition in movePlayer: | ||
| - | |||
| - | < | ||
| - | if self.gameMap[to_y][to_x] == '#': | ||
| - | return | ||
| - | elif self.gameMap[to_y][to_x] == ' | ||
| - | self.killPlayer() | ||
| - | |||
| - | self.px = to_x | ||
| - | self.py = to_y | ||
| - | |||
| - | </ | ||
| - | |||
| - | For reference, here is a version that uses if-elif-else: | ||
| - | |||
| - | < | ||
| - | if self.gameMap[to_y][to_x] == '#': | ||
| - | pass | ||
| - | elif self.gameMap[to_y][to_x] == ' | ||
| - | self.killPlayer() | ||
| - | else: | ||
| - | self.px = to_x | ||
| - | self.py = to_y | ||
| - | </ | ||
| - | |||
| - | It doesn' | ||
| - | |||
| - | In any case you will notice the killPlayer() function. This is new. Here it is: | ||
| - | |||
| - | < | ||
| - | def killPlayer(self): | ||
| - | print(" | ||
| - | quit() | ||
| - | </ | ||
| - | |||
| - | There it is. Simple idea, simple execution. We just want something functional for now. | ||
| - | |||
| - | In one version I used GAME OVER instead of a shrug smiley, but I thought this way was a bit funnier. | ||
robots-2.1695775161.txt.gz · Last modified: by appledog
