---
# System prepended metadata

title: Pygame

---

# Pygame
`main.py`
``` python
import controller

def main():
    main_window = controller.Controller()
    main_window.mainLoop()
main()
```

`controller.py`
``` python
import sys
import pygame

import Thing

class Controller:
    def __init__(self, width=640, height=480):
        pygame.init()
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.background = pygame.Surface(self.screen.get_size()).convert()
        pygame.font.init()

        self.tree = Thing.Thing(100, 100, "tree.png")


        self.display_group = pygame.sprite.Group((self.tree,))



    def mainLoop(self):
        '''
        start the game
        '''
        while True:
            self.gameLoop()

    def gameLoop(self):
        '''
        play the game
        '''
        pygame.key.set_repeat(1,50)

        while True:
            self.background.fill((50, 250, 250))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()



            

            self.screen.blit(self.background, (0, 0))

            self.display_group.draw(self.screen)
            
            pygame.display.flip()

```
`tree.py`
``` python
import pygame

class Thing(pygame.sprite.Sprite):
    def __init__(self, x, y, img_file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(img_file).convert_alpha()

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

```


![](https://i.imgur.com/GC83FI6.png)
