forked from Idea-Fighters/Food-Drop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
49 lines (38 loc) · 1.33 KB
/
Copy pathcode
File metadata and controls
49 lines (38 loc) · 1.33 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
# 키보드 왼쪽 오른쪽 누르면 그 방향대로 움직이는 코드입니다
# !pip install pygame
import pygame # 1. pygame 선언
pygame.init() # 2. pygame 초기화
# 3. pygame에 사용되는 전역변수 선언
WHITE = (255,255,255)
size = (400,300)
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
airplane = pygame.image.load('images/plane.png')
airplane = pygame.transform.scale(airplane, (60, 45))
# 4. pygame 무한루프
def runGame():
global done, airplane
x = 20
y = size[1] - 45 # 화면 아래쪽에서 시작하도록 y 좌표 조정
speed = 10
while not done:
clock.tick(30)
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= speed
elif event.key == pygame.K_RIGHT:
x += speed
# 비행기가 화면을 벗어나지 않도록 x 좌표 제한
if x < 0:
x = 0
elif x > size[0] - 60: # 비행기의 너비가 60이므로 화면 오른쪽 끝에서 60을 뺍니다.
x = size[0] - 60
screen.blit(airplane, (x, y))
pygame.display.update()
runGame()
pygame.quit()