---
# System prepended metadata

title: Pygame 服務學習學習筆記

---

## image
```python=

# remove background
img = pygame.image.load('image.png').convert_alpha()

# get image height and width
imgHeight = img.get_height()
imgWidth = img.get_width()

# transform.flip(img, 左右顛倒, 上下顛倒)
img = pygame.transform.flip(img, True, True)

# change the size of image
img = pygame.transform.scale(img, (imgHeight/4, imgWidth/4))

# transform.rotate(img, degree) 
# rotate clockwisely
img = pygame.transform.rotate(img, 90)

```

## sprite
```python=
# create a subclass of sprite
class Player(pygame.sprite.Sprite):
  
  # consturctor
  def __init__(self, thing: pygame.surface.Surface) -> None:
    self.image = thing
    self.rect = thing.get_rect()
  
  # method of sprite, when calling group.update(), the method would be executed
  def update(self) -> None:
    self.rect.x += 2
    self.rect.y += 2
# create a sprite group
group = pygame.sprite.Group()

player1 = Player(img)
group.add(player1)

# show the sprites on screen
group.draw(screen)
# calling group update
group.update()

```

## keyboard inputs
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:    #read 'a'

## sprite collide
all_rocks = pygame.sprite.Group()   *#contains all rocks*
all_bullets = pygame.sprite.Group() *#contains all bullets*
player = Player()

hits = pygame.sprite.groupcollide(all_rocks, all_bullets, True, True)  *#delete that rock? delete that bullet?*
for hit in hits:
    r = Rock()
    all_sprites.add(r)
    all_rocks.add(r)

hits = pygame.sprite.spritecollide(player, all_rocks, True, pygame.sprite.collide_circle)    *#Delete Rock. Circle as hitboxes*
*#when using collide_circle, the constructor function of the class must contain self.radius*
if hits:
    running = False    #game ends
    print("You died")
