ผลต่างระหว่างรุ่นของ "Prg2/arcade6 dotrun"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 124: แถว 124:
  
 
{{gitcomment|Don't forget to commit your code.}}
 
{{gitcomment|Don't forget to commit your code.}}
 +
 +
=== Exercise: jumping dot ===
 +
 +
We will make the dot jumps, basically by having attributes <tt>vy</tt> and <tt>is_jump</tt>.  The dot will also keep <tt>base_y</tt>.
 +
 +
{{synfile|models.py}}
 +
<syntaxhighlight lang="python">
 +
class Dot(Model):
 +
    def __init__(self, world, x, y, base_y):
 +
        super().__init__(world, x, y, 0)
 +
        self.vx = 0
 +
        self.vy = 0
 +
        self.is_jump = False
 +
 +
        self.base_y = base_y
 +
</syntaxhighlight>
 +
 +
When the dot lands, it will stop falling when its y co-ordinate is at most base_y.
  
 
== Additional steps ==
 
== Additional steps ==

รุ่นแก้ไขเมื่อ 05:57, 21 กุมภาพันธ์ 2562

This is part of the course Programming 2.

In this tutorial we will sketch rough steps and new techniques for building the Dot Run game.

Arcade-dot-run.png

Steps

These are the steps we will take to implement the game.

  • Shows Dot
  • Dot moves to the right
  • Dot moves and jump on the floor
  • Viewport
  • Shows platforms
  • Jumps on top of platforms
  • Recycles platforms
  • Shows and collects coins
  • Shows score

New techniques

Viewport

Drawing primitives

Player's states for jumping off and onto a platform

Let's get started

Create a new project and set up a Git repository

Create a directory to keep the project. Create a git repository there.

We will start with a very simple dot moving game.

File: dotrun.py
import arcade
from models import World

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400

class ModelSprite(arcade.Sprite):
    def __init__(self, *args, **kwargs):
        self.model = kwargs.pop('model', None)
 
        super().__init__(*args, **kwargs)
 
    def sync_with_model(self):
        if self.model:
            self.set_position(self.model.x, self.model.y)
 
    def draw(self):
        self.sync_with_model()
        super().draw()
        

class DotRunWindow(arcade.Window):
    def __init__(self, width, height):
        super().__init__(width, height)
 
        arcade.set_background_color(arcade.color.GRAY)

        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
        
        self.dot_sprite = ModelSprite('images/dot.png',
                                      model=self.world.dot)
 
    def update(self, delta):
        self.world.update(delta)


    def on_draw(self):
        arcade.start_render()
        self.dot_sprite.draw()
 
if __name__ == '__main__':
    window = DotRunWindow(SCREEN_WIDTH, SCREEN_HEIGHT)
    arcade.run()
File: models.py
GRAVITY = -1
MAX_VX = 10
ACCX = 1

class Model:
    def __init__(self, world, x, y, angle):
        self.world = world
        self.x = x
        self.y = y
        self.angle = 0

        
class Dot(Model):
    def __init__(self, world, x, y):
        super().__init__(world, x, y, 0)
        self.vx = 0
        self.vy = 0
    
    def update(self, delta):
        if self.vx < MAX_VX:
            self.vx += ACCX

        self.x += self.vx

        
class World:
    def __init__(self, width, height):
        self.width = width
        self.height = height

        self.dot = Dot(self, 0, 100)


    def update(self, delta):
        self.dot.update(delta)

You can use the dot image file here: dot.png. Save it to images/dot.png.

Try to run the game to see if there is a dot moving. Then, commit the code.

Gitmark.png Don't forget to commit your code.

Exercise: jumping dot

We will make the dot jumps, basically by having attributes vy and is_jump. The dot will also keep base_y.

File: models.py
class Dot(Model):
    def __init__(self, world, x, y, base_y):
        super().__init__(world, x, y, 0)
        self.vx = 0
        self.vy = 0
        self.is_jump = False

        self.base_y = base_y

When the dot lands, it will stop falling when its y co-ordinate is at most base_y.

Additional steps