Hướng dẫn shooting game using python
10. Tutorial: Shooting gameIn this chapter we will build a shooting game together, step by step. The Python we will use is: conditionals, loops, lists and functions. Show
10.1. Step 1: Decide what Actors you will needOur game will need these Actors, so we must create images for all of them and save them as
The Program 10.1 Shooter game part 1 of 4
10.2. Step 2: Draw your ActorsEvery Pygame game needs an Program 10.2 Shooter game part 2 of 4
10.3. Step 3: Move your ActorsEvery Pygame game needs an Program 10.3 Shooter game part 3 of 4
Exercise Create the png image files ( 10.4. Step 4: Define your functionsPython cannot call a
function that has not yet been defined. Therefore we must at least provide empty, dummy versions of our functions that don’t do anything so we can fill them in later. However Python cannot define a completely empty function - it must contain at least one line. Therefore we use the Program 10.4 Shooter game part 4 of 4
Exercise Add listing Program 10.4 to the end of the file. Verify the game now runs and you can see the player at the bottom of the screen. (He can’t move yet.) 10.5. Create enemiesAdd this new function to the end of the program, and then call it immediately. It uses a loop within a loop to create enemy Actors and put them in the def create_enemies(): for x in range(0, 600, 60): for y in range(0, 200, 60): enemy = Actor("enemy", (x, y)) enemy.vx = level * 2 enemies.append(enemy) create_enemies() 10.6. Move the playerReplace the def move_player(): if keyboard.right: player.x = player.x + 5 if keyboard.left: player.x = player.x - 5 if player.x > WIDTH: player.x = WIDTH if player.x < 0: player.x = 0 10.7. Move the enemiesReplace the def move_enemies(): global score for enemy in enemies: enemy.x = enemy.x + enemy.vx if enemy.x > WIDTH or enemy.x < 0: enemy.vx = -enemy.vx animate(enemy, duration=0.1, y=enemy.y + 60) for bullet in bullets: if bullet.colliderect(enemy): enemies.remove(enemy) bullets.remove(bullet) score = score + 1 if enemy.colliderect(player): exit() 10.8. Draw text on the screenReplace the def draw_text(): screen.draw.text("Level " + str(level), (0, 0), color="red") screen.draw.text("Score " + str(score), (100, 0), color="red") screen.draw.text("Lives " + str(lives), (200, 0), color="red") 10.9. Player bulletsAdd this new function to the end of the program. Pygame will call it for us automatically when a key is pressed. def on_key_down(key): if key == keys.SPACE and len(bullets) < MAX_BULLETS: bullet = Actor("bullet", pos=(player.x, player.y)) bullets.append(bullet) Replace the def move_bullets(): for bullet in bullets: bullet.y = bullet.y - 6 if bullet.y < 0: bullets.remove(bullet) 10.10. Enemy bombsReplace the def create_bombs(): if random.randint(0, 100 - level * 6) == 0: enemy = random.choice(enemies) bomb = Actor("bomb", enemy.pos) bombs.append(bomb) Replace the def move_bombs(): global lives for bomb in bombs: bomb.y = bomb.y + 10 if bomb.colliderect(player): bombs.remove(bomb) lives = lives - 1 if lives == 0: exit() 10.11. Check for end of levelReplace the def check_for_end_of_level(): global level if len(enemies) == 0: level = level + 1 create_enemies() 10.12. Ideas for extension
|