ผลต่างระหว่างรุ่นของ "Prg2/space (applying design patterns)"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 15: แถว 15:
 
* Enemy (in <tt>elements.py</tt>)
 
* Enemy (in <tt>elements.py</tt>)
 
* Bomb
 
* Bomb
 +
 +
=== Changes in gamelib ===
 +
 +
Since we will have a lot of sprites flying around, it would cause the game huge delays if we keep out-of-scene sprites in the game.  We add attribute <tt>to_be_deleted</tt> and method <tt>delete</tt> to tell the game loop that a sprite can be discarded.  We also provide methods for stoping the game <tt>stop_animation</tt> and resuming the game <tt>resume_animation</tt>.  Method <tt>animate</tt> becomes:
 +
 +
<syntaxhighlight lang="python">
 +
    def animate(self):
 +
        if not self.is_stopped:
 +
            self.pre_update()
 +
 +
            remaining_elements = []
 +
            for element in self.elements:
 +
                element.update()
 +
                element.render()
 +
 +
                if element.to_be_deleted:
 +
                    element.delete()
 +
                else:
 +
                    remaining_elements.append(element)
 +
 +
            self.elements = remaining_elements
 +
 +
            self.post_update()
 +
 +
        self.after(self.update_delay, self.animate)
 +
</syntaxhighlight>
  
 
=== SpaceGame ===
 
=== SpaceGame ===

รุ่นแก้ไขเมื่อ 22:54, 24 มีนาคม 2564

This is part of Programming 2 2563

Overview

In this assignment, you will apply design patterns to the provided Space Fighter code. In this game you can turn the ship with Left and Right keys. You can fire bullets with Spacebar and applying bombs with the Z key. You have 30 bullets at a time and the bomb needs cool-down period (to recharge its power to 100%).

Prg2-space-fighter.png

Understanding the current code

There are 5 main classes:

  • SpaceGame
  • Ship (in elements.py)
  • Bullet (in elements.py)
  • Enemy (in elements.py)
  • Bomb

Changes in gamelib

Since we will have a lot of sprites flying around, it would cause the game huge delays if we keep out-of-scene sprites in the game. We add attribute to_be_deleted and method delete to tell the game loop that a sprite can be discarded. We also provide methods for stoping the game stop_animation and resuming the game resume_animation. Method animate becomes:

    def animate(self):
        if not self.is_stopped:
            self.pre_update()

            remaining_elements = []
            for element in self.elements:
                element.update()
                element.render()

                if element.to_be_deleted:
                    element.delete()
                else:
                    remaining_elements.append(element)

            self.elements = remaining_elements

            self.post_update()

        self.after(self.update_delay, self.animate)

SpaceGame

SpaceGame is the main class. It maintains a Ship, a list of Enemies, and a list of Bullets.

  • It keeps bullets and enemies in self.bullets and self.enemies. The reason these objects are kept outside the typical self.elements is to improve performance when performing collision detection.
  • There are 2 mechanisms to generate enemies.
    def create_enemy_star(self):
        # ...

    def create_enemy_from_edges(self):
        # ...

    def create_enemies(self):
        if random() < 0.2:
            enemies = self.create_enemy_star()
        else:
            enemies = self.create_enemy_from_edges()

        for e in enemies:
            self.add_enemy(e)
  • It runs collision detection to detect bullet-enemy collisions and enemy-ship collisions.
  • It handles keyboard events (both key-pressed and key-released events).
  • It also deals with bomb capacity of the ship: detonates the bomb, maintains bomb cool-down periods, and shows/hides bomb radius after it is detonated.

Ship

Bullet and Enemy

Bomb

Getting started

This is an individual assignment, but you should still use branches in git for managing your work. You should use the following template as a starter code.

Since this is the second time you work on design patterns, the instructions would be less specific and you can use your judgement more freely to improve the code.

Patterns

Additional improvements