-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.py
More file actions
52 lines (42 loc) ยท 2.83 KB
/
ship.py
File metadata and controls
52 lines (42 loc) ยท 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pygame
from pygame.sprite import Sprite
class Ship(Sprite): # Ship ํด๋์ค์ Sprite ํด๋์ค๋ฅผ ์์ํจ.
def __init__(self, invasion_settings, screen):
"""Initialize the ship and set its starting position."""
super(Ship, self).__init__() # ์์ ์์ํด๋์ค(Sprite)์ ์์ฑ์ ํธ์ถํ๋ super() ๋ฉ์๋๋ฅผ ํตํด __init__() ์๋ฐ๋ก ์น๋ฉด ์์ฑ์ ํธ์ถ. .
self.screen = screen
self.invasion_settings = invasion_settings
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Store a decimal value for the ship's center.
self.center = float(self.rect.centerx)
# Movement flags (์์ง์ ๊ธฐ๋ณธ๊ฐ-์ ์ง)
self.moving_right = False # ship ๊ฐ์ฒด์ ์์ง์์ ๊ฐ์งํ์ง ์์ ๋(False)๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋
self.moving_left = False
def update(self):
"""Update the ship's position based on the movement flags."""
# Update the ship's center value, not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
# ship ๊ฐ์ฒด์ ์์ง์์ด ๊ฐ์ง๋๋ฉด ์๋์ ๊ฐ์ด ์ค์ฌ์ ์์นํ ship์ ์ค๋ฅธ์ชฝ์ผ๋ก 1.5์ฉ ์ด๋ <<Refactoring>>๋์ด์ ์ฝ๋๋ผ์ธX
# (invasion.py์tj ship_speed_factor ์์ฑ์ Ship ํด๋์ค์ ํ์ ํ๋ผ๋ฏธํฐ๋ก setting ๋ชจ๋์ ์ง์ ํ ์คํผ๋๊ฐ ๊ฐ์ ธ์ด.
# ์คํผ๋๊ฐ ์ธํ
๋ชจ๋์ ์ผ์ํ => Refactoring)
self.center += self.invasion_settings.ship_speed_factor
# elif๋ก ๋ถ๊ธฐ๋์ง ์๊ณ if ๋ธ๋ก์ผ๋ก ์ ์ด๋ฅผ ํ๋ ์ด์ ๋ ์ข์ฐ ๋ฐฉํฅํค๋ฅผ ๋์์ ๋๋ ์ ๋์ ์กฐ๊ฑด(->๋ฉ์ถค) ๊ฐ์ง์ ์ค๋ฅธ์ชฝ์ผ๋ก
# ์์ง์ด๋ ๊ฒ์ ์ฐ์ ์์์ผ๋ก ๋์ง ์๊ธฐ ์ํจ.(p252 ๊ฐ์ด๋ฐ ์ฐธ์กฐ).
# Update rect object from self.center
# if self.moving_left and self.rect.left < self.screen_rect.left: ํ๋ฉด ์ต์ข์ธก์ ํด๋นํ๋ left ์์ฑ์ ์์. <<๋ฆฌํํฐ๋ง>>
if self.moving_left and self.rect.left > 0: # ๊ทธ๋์ ๋ง์ฝ ์ฌ๊ฐํ(ship)์ ์ข์ธก๊ฐ์ด 0๋ณด๋ค ํฌ๋ค๋ ์กฐ๊ฑดํ์ ship์ ํ๋ฉด์ ์ข์ธก
self.center -= self.invasion_settings.ship_speed_factor # ์ผ๋ก ๋์ด๊ฐ์ง ์์ ๊ฑฐ์.
# Update rect object from self.center.
self.rect.centerx = self.center
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
def center_ship(self):
"""Center the ship on the screen."""
self.center = self.screen_rect.centerx