# Scene

## Initialization

You can get started with Scenes by creating a new class that extends the base Scene class from pyDraw:

```python
import * from pydraw

class MyScene(Scene):
    # you should declare your variables outside of any of the methods (static!)
    some_variable = 34
    
    some_shape = None  # set pyDraw objects or uninitiated variables to None
    to_set_later = None
    
    # now we can define a "start" method which will run when the scene is displayed
    def start(self):
        self.some_shape = Rectangle(self.screen(), 10, 10, 75, 50, Color('red'))
        self.to_set_later = self.some_shape.width() / 2
    
    # you can setup the Scene's input hooks by defining the methods in the class
    def keydown(self, key):
        if key == 'x':
            self.to_set_later += 1
        else:
            self.some_shape.move(x=3)
    
    def keyup(self, key):
        if key == 'x':
            self.to_set_later += self.to_set_later % 3
    
    def mousedown(self, location):
        print(location)
        
    # you won't even need to call screen.listen(), it happens automatically!
    
    # next up we setup a "run" method which happens after our input methods!
    def run():
        # as you can see, it's just like a normal pyDraw program!
    
        running = True
        fps = 30
        while running:
            self.some_shape.color(Color.random())
        
            self.screen().update()
            self.screen().sleep(1 / fps)
```

## Methods

### Start

Should instantiate your variables and prepare everything that the input hooks will use (unless you have checks in your hooks)

### Run

Should contain your actual loop and main program logic.

### Screen

Get the Screen that the Scene is tied to! (can throw errors if methods are being called without being attached to a Screen)

### Input Methods

Supports all the same input methods that any other pyDraw program supports:

* `mousedown` | When the mouse is pressed or is held, this will be called
* `mouseup`     | When the mouse is released this is called
* `mousedrag` | When the mouse is dragged, this is called anytime the mouse moves and is held.
* `mousemove` | When the mouse moves, this method is called.
* `keydown`     | When a key is pressed or is currently being held, this is called
* `keyup`         | When a key is released this is called
* `exit`           | Called when the Screen is abruptly closed or [`screen.exit()`](/quick-reference/screen.md) is called.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pydraw.graphics/quick-reference/scene.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
