00_base.py

import pyxel

class WorkshopGame:
    def __init__(self):
        pyxel.init(128, 128, title="Pixel Art Workshop")
        
        # Make sure they have a file named this or comment it out to use the editor's bank
        pyxel.load("my_resource.pyxres")
        
        # --- POSITION & PHYSICS ---
        self.x = 64
        self.y = 100         # Starting floor height
        self.floor_y = 100   # The "ground" level
        self.dy = 0          # Vertical velocity (delta Y)
        self.gravity = 0.8   # Strength of gravity
        self.jump_force = -6 # Initial upward boost
        
        # --- ANIMATION SETTINGS ---
        self.sprite_size = 8
        self.ROW_IDLE = 0
        self.ROW_WALK = 1
        self.ROW_JUMP = 2
        
        self.current_row = self.ROW_IDLE
        self.frame_count = 4
        self.anim_speed = 5
        self.facing_right = True
        
        pyxel.run(self.update, self.draw)

    def update(self):
        # 1. HORIZONTAL MOVEMENT
        is_moving = False
        if pyxel.btn(pyxel.KEY_RIGHT):
            self.x += 2
            self.facing_right = True
            is_moving = True
        elif pyxel.btn(pyxel.KEY_LEFT):
            self.x -= 2
            self.facing_right = False
            is_moving = True
            
        self.x %= 128

        # 2. JUMP LOGIC
        # Only allow jump if the character is on the floor
        if pyxel.btnp(pyxel.KEY_SPACE) and self.y >= self.floor_y:
            self.dy = self.jump_force

        # 3. APPLY PHYSICS
        self.y += self.dy          # Move character by current velocity
        
        if self.y < self.floor_y:  # Character is in the air
            self.dy += self.gravity # Pull them down
            self.current_row = self.ROW_JUMP
            self.frame_count = 2
        else:                      # Character is on/below the ground
            self.y = self.floor_y  # Snap to floor
            self.dy = 0            # Stop falling
            
            # Switch between Walk and Idle only when on ground
            if is_moving:
                self.current_row = self.ROW_WALK
                self.frame_count = 4
            else:
                self.current_row = self.ROW_IDLE
                self.frame_count = 4

    def draw(self):
        pyxel.cls(0)
        
        # Draw a simple floor line
        pyxel.line(0, self.floor_y + (self.sprite_size // 2), 128, self.floor_y + (self.sprite_size // 2), 13)
        
        # Animation frame calculation
        # Use pyxel.frame_count for continuous loop
        frame_x = (pyxel.frame_count // self.anim_speed) % self.frame_count
        
        facing_sign = 1 if self.facing_right else -1
        
        pyxel.blt(
            self.x - (self.sprite_size // 2), 
            self.y - (self.sprite_size // 2), 
            0, 
            frame_x * self.sprite_size, 
            self.current_row * self.sprite_size, 
            self.sprite_size * facing_sign, 
            self.sprite_size, 
            0
        )

        pyxel.text(5, 5, "ARROWS: Move", 7)
        pyxel.text(5, 12, "SPACE: Jump", 7)

WorkshopGame()