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 [2023/10/03 06:38] (current) – appledog | ||
---|---|---|---|
Line 67: | Line 67: | ||
In one version I used GAME OVER instead of a shrug smiley, but I thought this way was a bit funnier. | In one version I used GAME OVER instead of a shrug smiley, but I thought this way was a bit funnier. | ||
+ | |||
+ | === Adding Robots | ||
+ | Adding robots is a bit trickier because they have to move intelligently on their own. But overall it is also a simple idea. | ||
+ | |||
+ | First let's add a robot. Here is what it looks like: Place this code after adding the rocks: | ||
+ | |||
+ | < | ||
+ | # Add Robot | ||
+ | self.gameMap[15][12] = ' | ||
+ | </ | ||
+ | |||
+ | This code looks deceptively simple but the idea behind it is very powerful. Consider briefly we could also place the player on the map in this way as well. Why don't we do this? It could be a good question for after the game is finished. A) Keep special track off-map, or B) keep track of it on the map itself. Which method is better in which situations? | ||
+ | |||
+ | === Moving the Robots | ||
+ | Moving the robots is easy. The robots always move after the player moves. Therefore, we modify checkEvents() like this: | ||
+ | |||
+ | < | ||
+ | if event.key == pygame.K_LEFT: | ||
+ | self.movePlayer(self.px-1, | ||
+ | self.moveRobots() | ||
+ | |||
+ | elif event.key == pygame.K_RIGHT: | ||
+ | self.movePlayer(self.px+1, | ||
+ | self.moveRobots() | ||
+ | |||
+ | elif event.key == pygame.K_UP: | ||
+ | self.movePlayer(self.px, | ||
+ | self.moveRobots() | ||
+ | |||
+ | elif event.key == pygame.K_DOWN: | ||
+ | self.movePlayer(self.px, | ||
+ | self.moveRobots() | ||
+ | </ | ||
+ | |||
+ | As you can see, all we did is "move the robots after the player moves" | ||
+ | |||
+ | Okay so now let's write moveRobots()! | ||
+ | |||
+ | === moveRobots() | ||
+ | Since we are keeping track of the robots on the map, we have to find the robots on the map and move them. Watch carefully how this is done: | ||
+ | |||
+ | < | ||
+ | def moveRobots(self): | ||
+ | # 1. Find and move every robot. | ||
+ | for x in range(self.gameW): | ||
+ | for y in range(self.gameH): | ||
+ | if self.gameMap[y][x] == ' | ||
+ | self.moveRobot(x, | ||
+ | </ | ||
+ | |||
+ | That seems simple enough! Find a robot on the map, and move it! | ||
+ | |||
+ | < | ||
+ | def moveRobot(self, | ||
+ | # The robot wants to move towards the player. | ||
+ | dx = self.px - x | ||
+ | dy = self.py - y | ||
+ | |||
+ | if dx != 0 and dy != 0: | ||
+ | xory = random.randint(1, | ||
+ | if xory == 1: | ||
+ | dx = 0 | ||
+ | else: | ||
+ | dy = 0 | ||
+ | |||
+ | if dx > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y][x+1] = ' | ||
+ | |||
+ | if dx < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y][x-1] = ' | ||
+ | |||
+ | if dy > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y+1][x] = ' | ||
+ | |||
+ | if dy < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y-1][x] = ' | ||
+ | </ | ||
+ | |||
+ | The logic is simple. Find the distance between the robot and the player. But since the player can only move up, down, left and right we want to make it so the robots cannot move diagonally. The meaning of " | ||
+ | |||
+ | The logic of moving it on the map is simple too. All we do is erase the old robot and draw a new one on the map at the new position. | ||
+ | |||
+ | //But there is a big problem with this!// | ||
+ | |||
+ | If we move the robot down (or to the right) then after the robot moves, the for loops in moveRobots() will detect the //new// robot at the //new// position and move him again! | ||
+ | |||
+ | This is a serious problem, but the solution is very simple. Just write a large R to the map for the new robot' | ||
+ | |||
+ | < | ||
+ | def moveRobot(self, | ||
+ | # The robot wants to move towards the player. | ||
+ | dx = self.px - x | ||
+ | dy = self.py - y | ||
+ | |||
+ | if dx != 0 and dy != 0: | ||
+ | xory = random.randint(1, | ||
+ | if xory == 1: | ||
+ | dx = 0 | ||
+ | else: | ||
+ | dy = 0 | ||
+ | |||
+ | if dx > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y][x+1] = ' | ||
+ | |||
+ | if dx < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y][x-1] = ' | ||
+ | |||
+ | if dy > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y+1][x] = ' | ||
+ | |||
+ | if dy < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | self.gameMap[y-1][x] = ' | ||
+ | </ | ||
+ | |||
+ | There. Now the robots will only be moved once. We now have to repair this after we have moved all the robots: | ||
+ | |||
+ | < | ||
+ | def moveRobots(self): | ||
+ | # 1. Find and move every robot. | ||
+ | for x in range(self.gameW): | ||
+ | for y in range(self.gameH): | ||
+ | if self.gameMap[y][x] == ' | ||
+ | self.moveRobot(x, | ||
+ | |||
+ | # 2. repair the map | ||
+ | for x in range(self.gameW): | ||
+ | for y in range(self.gameH): | ||
+ | if self.gameMap[y][x] == ' | ||
+ | self.gameMap[y][x] = ' | ||
+ | </ | ||
+ | |||
+ | There! Now, after the robots have been moved, all the large R is changed back to small R. The idea here is to flag a robot in a special way. Since we cannot add a ' | ||
+ | |||
+ | === Robots dying to rocks | ||
+ | There has to be a way to escape the robots. Therefore let us make the robots die when they hit a rock, just like the player. | ||
+ | |||
+ | We will change moveRobots() like this: | ||
+ | |||
+ | < | ||
+ | if dx > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | if self.gameMap[y][x+1] != ' | ||
+ | self.gameMap[y][x+1] = ' | ||
+ | |||
+ | if dx < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | if self.gameMap[y][x-1] != ' | ||
+ | self.gameMap[y][x-1] = ' | ||
+ | |||
+ | if dy > 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | if self.gameMap[y+1][x] != ' | ||
+ | self.gameMap[y+1][x] = ' | ||
+ | |||
+ | if dy < 0: | ||
+ | self.gameMap[y][x] = ' ' | ||
+ | if self.gameMap[y-1][x] != ' | ||
+ | self.gameMap[y-1][x] = ' | ||
+ | </ | ||
+ | |||
+ | Now, if there is a rock at the position which the robot wants to move to, it will ' | ||
+ | |||
+ | Finally, since the player has a way to fight the robots, there should also be a danger; if the player touches a robot, he dies. Here, add this kill condition at the start of moveRobot(): | ||
+ | |||
+ | < | ||
+ | def moveRobot(self, | ||
+ | # The robot wants to move towards the player. | ||
+ | dx = self.px - x | ||
+ | dy = self.py - y | ||
+ | |||
+ | # If a robot touches a player, the player dies. | ||
+ | if dx == 0 and dy == 0: | ||
+ | self.killPlayer() | ||
+ | |||
+ | # (The rest of the code goes here) | ||
+ | </ | ||
+ | |||
+ | Now, we have a game! The robot can die, the player can die. In part 3 we will define a win condition and polish up the game a bit by adding some more special features! | ||
+ | |||
+ | == Next | ||
+ | * [[robots-3|Part 3]] |
robots-2.1695775161.txt.gz · Last modified: 2023/09/27 00:39 by appledog