[Pygame] Pygame Gravity 파이게임 중력
Python 2024. 2. 3. 00:08 |중력이 적용된 캐릭터를 만들어 보자.
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
import pygame
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640, 480), vsync=1)
clock = pygame.time.Clock()
FPS = 60
running = True
# 플레이어 클래스 with Gravity
class Player(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.direction = -1
self.speed = 4
self.image = pygame.image.load("player.png").convert()
self.image.set_colorkey(self.image.get_at((0, 0)))
self.size = (self.image.get_width()*1.5, self.image.get_height()*1.5)
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect(center=position)
self.isGround = False
self.jumpForce = 0.0
self.gravity = 5.0
def flip_image(self):
self.image = pygame.transform.flip(self.image, True, False)
def update(self):
global running
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if self.direction > 0:
self.flip_image()
self.direction = -1
self.rect.move_ip(-self.speed, 0)
if keys[pygame.K_RIGHT]:
if self.direction < 0:
self.flip_image()
self.direction = 1
self.rect.move_ip(self.speed, 0)
if keys[pygame.K_UP]:
if self.isGround:
self.jumpForce = -17
if not self.isGround:
self.rect.centery += self.gravity + self.jumpForce
self.gravity += 0.3
else:
self.rect.centery += self.jumpForce
self.gravity = 5.0
if self.jumpForce < 0:
self.jumpForce += 0.10
elif self.jumpForce >= 0:
self.jumpForce = 0.0
# 버블 클래스
class Bubble(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("bubble.png").convert()
self.image.set_colorkey(self.image.get_at((0, 0)))
self.size = (self.image.get_width()*6, self.image.get_height()*6)
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect(center=position)
self.collided = False
def update(self):
if self.collided == True:
self.rect.top -= 1
# 그라운드 클래스
class Ground(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("ground.png").convert()
self.rect = self.image.get_rect(bottomleft=position)
def update(self):
pass
def main():
player = Player((screen.get_width()/2, screen.get_height()/2))
player_sprite = pygame.sprite.Group(player)
# 플레이어 스프라이트 그룹
bubbles = [
Bubble((40, screen.get_height()/2)),
Bubble((160, screen.get_height()/2)),
Bubble((480, screen.get_height()/2)),
Bubble((600, screen.get_height()/2))]
bubble_sprites = pygame.sprite.Group(bubbles)
# 버블 스프라이트 그룹
grounds = [
Ground((0, 480)),
Ground((440, 480))]
ground_sprites = pygame.sprite.Group(grounds)
all_sprites = pygame.sprite.Group()
all_sprites.add(player_sprite)
all_sprites.add(bubble_sprites)
all_sprites.add(ground_sprites)
while running:
#player_sprite.update()
#bubble_sprites.update()
all_sprites.update()
collision = pygame.sprite.spritecollide(player, bubble_sprites, False)
for bubble in collision:
bubble.collided = True
# 플레이어와 버블의 충돌을 감지하고 충돌한 버블의 collided 값을 True로 바꾼다.
collision = pygame.sprite.spritecollide(player, ground_sprites, False)
if len(collision) != 0:
player.isGround = True
player.jumpForce = 0.0
player.rect.bottom = collision[0].rect.top
else:
player.isGround = False
screen.fill("black")
#player_sprite.draw(screen)
#bubble_sprites.draw(screen)
#ground_sprites.draw(screen)
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == '__main__':
main()
|
코드를 입력하고 실행한다.
'Python' 카테고리의 다른 글
[Pygame] Sorting Algorithms in Python 파이썬 정렬 알고리즘 2 (0) | 2024.03.22 |
---|---|
[Pygame] Sorting Algorithms in Python 파이썬 정렬 알고리즘 1 (0) | 2024.03.09 |
[Pygame] Pygame Simple Camera 파이게임 간단한 카메라 (0) | 2024.02.02 |
[Pygame] Pygame GUI 파이게임 그래픽 유저 인터페이스 (0) | 2024.01.29 |
[Pygame] Box2D 파이게임 물리 라이브러리 (0) | 2024.01.28 |