robots
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| robots [2023/09/27 00:19] – appledog | robots [2023/09/27 00:31] (current) – appledog | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| = Robots | = Robots | ||
| Part one of this class was taught to [[Roger]] on Sep. 19th 2023. | Part one of this class was taught to [[Roger]] on Sep. 19th 2023. | ||
| + | |||
| + | This discusses the basic addition of map and player. In part two we add rocks and robots, and in part 3 we finish the game. | ||
| + | * [[robots-2|Part 2]] | ||
| + | * [[robots-3|Part 3]] | ||
| == PyGame Console Starter | == PyGame Console Starter | ||
| Line 10: | Line 14: | ||
| The first step is to define the world. We start small, with baby steps. We want a map and a player which can move around. Add the following code after " | The first step is to define the world. We start small, with baby steps. We want a map and a player which can move around. Add the following code after " | ||
| - | === Setting it up in Class Game | + | === Initializing the Map |
| < | < | ||
| Line 35: | Line 39: | ||
| The two for loops will draw walls at the edges of the map. | The two for loops will draw walls at the edges of the map. | ||
| + | === Drawing the Map | ||
| + | Inside Class Game's drawMap() method we have the following code: | ||
| + | |||
| + | < | ||
| + | def drawGame(self): | ||
| + | # 1. draw map | ||
| + | for x in range(self.gameW): | ||
| + | for y in range(self.gameH): | ||
| + | self.drawText(x, | ||
| + | |||
| + | # 2. draw player | ||
| + | self.drawText(self.px, | ||
| + | </ | ||
| + | |||
| + | Simply put, we draw whatever is in the gameMap to the screen, and then we draw the player. This is a really simple idea; we just draw the state of the game as-is. | ||
| + | |||
| + | === Player Movement | ||
| + | First we have the checkEvents method, which is given hooks to move the player: | ||
| + | |||
| + | < | ||
| + | def checkEvents(self): | ||
| + | for event in pygame.event.get(): | ||
| + | if event.type == pygame.QUIT: | ||
| + | self.running = False | ||
| + | return | ||
| + | |||
| + | if event.type == pygame.KEYDOWN: | ||
| + | # key down event, process keys. | ||
| + | |||
| + | if event.key == pygame.K_LEFT: | ||
| + | self.movePlayer(self.px-1, | ||
| + | |||
| + | elif event.key == pygame.K_RIGHT: | ||
| + | self.movePlayer(self.px+1, | ||
| + | |||
| + | elif event.key == pygame.K_UP: | ||
| + | self.movePlayer(self.px, | ||
| + | |||
| + | elif event.key == pygame.K_DOWN: | ||
| + | self.movePlayer(self.px, | ||
| + | |||
| + | else: | ||
| + | pass | ||
| + | </ | ||
| + | |||
| + | Again, keeping things light and simple. This is a feature of 'rapid prototyping', | ||
| + | |||
| + | The next thing we need to do is write movePlayer(). | ||
| + | |||
| + | < | ||
| + | def movePlayer(self, | ||
| + | # 1. Check map bounds. | ||
| + | if to_x < 0: to_x = self.gameW-1 | ||
| + | if to_y < 0: to_y = self.gameH - 1 | ||
| + | if to_x >= self.gameW: | ||
| + | if to_y >= self.gameH: | ||
| + | |||
| + | # 2. Don't allow player to move onto a (blocking) wall. | ||
| + | if self.gameMap[to_y][to_x] == '#': | ||
| + | return # do nothing | ||
| + | | ||
| + | # 3. Fall-through condition: move the player. | ||
| + | self.px = to_x | ||
| + | self.py = to_y | ||
| + | </ | ||
| + | |||
| + | Note that #3 could be written using else: as well; but then later if we want to introduce more checks it will be difficult to modify the code. Not difficult, just more difficult than this way. The fall-throuh condition style of coding is considered lower-level than using if-elif-else, | ||
| + | At this point you should have a working game which allows the player to move around, but there are some problems with the game. Yet, this so far is good enough for a small 1.5 hours class. | ||
robots.1695773943.txt.gz · Last modified: 2023/09/27 00:19 by appledog
