robots
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
robots [2023/09/20 01:42] – created 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 | ||
We begin with the standard PyGame console starter program: | We begin with the standard PyGame console starter program: | ||
- | * [[Portable BConsole Starter Program]] | + | * [[PyGame Terminal]] |
+ | |||
+ | == Defining the World | ||
+ | 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 " | ||
+ | |||
+ | === Initializing the Map | ||
+ | |||
+ | < | ||
+ | # create map | ||
+ | self.gameW = 60 | ||
+ | self.gameH = 21 | ||
+ | self.gameMap = [[' ' for _ in range(self.gameW)] for _ in range(self.gameH)] | ||
+ | |||
+ | # add walls | ||
+ | for x in range(self.gameW): | ||
+ | self.gameMap[0][x] = '#' | ||
+ | self.gameMap[self.gameH-1][x] = '#' | ||
+ | |||
+ | for y in range(self.gameH): | ||
+ | self.gameMap[y][0] = '#' | ||
+ | self.gameMap[y][self.gameW-1] = '#' | ||
+ | |||
+ | self.px = 3 # | ||
+ | self.py = 3 # | ||
+ | </ | ||
+ | |||
+ | I left code inside for a random position, but you can also just make the player start wherever you want for testing purposes. You will also have to set gameW and gameH based on your screen size and other factors. The numbers shown work well on my screen but yours may be different. | ||
+ | |||
+ | 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.1695174144.txt.gz · Last modified: 2023/09/20 01:42 by appledog