'전체 글'에 해당되는 글 514건

  1. 2023.09.13 [Pygame] Bouncing Ball 벽에 튕기는 공 만들기
반응형

사방이 벽으로 둘러쌓인 공간에서 이리저리 움직이는 공을 만들어 보자.

 

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
import pygame
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
 
position = pygame.math.Vector2(screen.get_width()/2, screen.get_height()/2)
velocity = pygame.math.Vector2(11).normalize() * 5
 
running = True
 
while 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
 
    screen.fill("black")
 
    if position.y > screen.get_height():
        velocity.reflect_ip(pygame.math.Vector2(01))
        # 천장이나 바닥에 부딪히면 (0, 1)의 법선 백터를 갖는 평면에 반사.
    elif position.y < 0:
        velocity.reflect_ip(pygame.math.Vector2(01))
    elif position.x > screen.get_width():
        velocity.reflect_ip(pygame.math.Vector2(10))
        # 오른쪽이나 왼쪽에 부딪히면 (1, 0)의 법선 백터를 갖는 평면에 반사.
    elif position.x < 0:
        velocity.reflect_ip(pygame.math.Vector2(10))  
 
    position = position + velocity
    pygame.draw.circle(screen, "red", position, 20)
    pygame.display.flip()
 
    clock.tick(60)
 
pygame.quit()
 

 

 

 

반응형
Posted by J-sean
: