forked from Idea-Fighters/Food-Drop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_ContinuousMovement
More file actions
56 lines (39 loc) · 1.56 KB
/
Copy pathcode_ContinuousMovement
File metadata and controls
56 lines (39 loc) · 1.56 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
53
54
55
56
# 앞선 코드(code)를 변형하여 작성된 코드입니다.
# 방향키를 반복적으로 누르지 않고 키를 놓을 때까지 해당 방향으로 움직이는 코드입니다.
!pip install pygame
pip install --upgrade pip
import pygame
pygame.init()
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))
def runGame():
global done, airplane
x = 20
y = size[1] - 45
speed = 5
while not done:
clock.tick(30) # 숫자가 커질수록 화면이 자주 업데이트되기 때문에 좀 더 속도감있고 매끄러워짐
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
# 수정된 부분
keys = pygame.key.get_pressed() # 키보드의 모든 키의 상태 가져오기
# 왼쪽 또는 오른쪽 키가 눌려 있는지 확인
if keys[pygame.K_LEFT]: # 왼쪽 키가 눌려 있으면
x -= speed # 왼쪽으로 이동
if keys[pygame.K_RIGHT]: # 오른쪽 키가 눌려 있으면
x += speed # 오른쪽으로 이동
if x < 0:
x = 0
elif x > size[0] - 60:
x = size[0] - 60
screen.blit(airplane, (x, y))
pygame.display.update()
runGame()
pygame.quit()