Lecture 움직이는 공들

Lecture • Views 310 • Comments 0 • Last Updated at 1 year ago  
  • 파이게임
  • 파이게임

움직이는 공

하나의 공이 움직이는 프로그램.

# ball_03.py

import pygame
import time
import numpy as np
import random 

pygame.init()

win = pygame.display.set_mode((500,400)) 
 
pygame.display.set_caption('두번째 예제') 

x = 50
y = 50
radius = 30
width = 40
height = 60
vx = 5
vy = 5
dx = 1
dy = 1

clock = pygame.time.Clock()

run = True
move = False

while run:
    t1 = time.time_ns()
    clock.tick(25)

    t2 = time.time_ns()
    s = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            move = True
    if move:
        x = x + dx * vx
        y = y + dy * vy
    if x >= 500 - radius:
        dx = -1
    if x <= radius:
        dx = 1
    if y >= 400 - radius:
        dy = -1
    if y <= radius:
        dy = 1
        
    win.fill((0,0,0)) 
    pygame.draw.circle(win, (255,0,0),(x,y),radius) 
    pygame.display.update() 
    t3 = time.time_ns()
    
pygame.quit()  

두개의 공이 움직이는 프로그램

# ball_04.py

import pygame
import time
import numpy as np
import random 

pygame.init()

win = pygame.display.set_mode((500,400)) 
 
pygame.display.set_caption('두번째 예제') 

x1, y1, r1 = 50, 50, 30

x2, y2, r2 = random.randint(150, 250), random.randint(150, 250), random.randint(10, 30)

width = 40
height = 60
vx = 5
vy = 5
dx1, dy1 = 1, 1
dx2, dy2 = 1, 1
vel = 0
clock = pygame.time.Clock()

run = True
move = False

while run:
    t1 = time.time_ns()
    clock.tick(25)

    t2 = time.time_ns()
    s = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            move = True
    if move:
        x1 = x1 + dx1 * vx
        y1 = y1 + dy1 * vy
        x2 = x2 + dx2 * vx
        y2 = y2 + dy2 * vy
        
    if x1 >= 500 - r1:
        dx1 = -1
    if x1 <= r1:
        dx1 = 1
    if y1 >= 400 - r1:
        dy1 = -1
    if y1 <= r1:
        dy1 = 1

    if x2 >= 500 - r1:
        dx2 = -1
    if x2 <= r1:
        dx2= 1
    if y2 >= 400 - r1:
        dy2 = -1
    if y2 <= r1:
        dy2 = 1
        
      
    win.fill((0,0,0)) 
    pygame.draw.circle(win, (255,0,0),(x1,y1),r1) 
    pygame.draw.circle(win, (0,255,0),(x2,y2),r2) 
    pygame.display.update() 
    t3 = time.time_ns()
    
pygame.quit()  

클래스를 활용한 프로그램

# ball_03_class.py

import pygame
import time
import numpy as np
import random 

class Ball():
    def __init__(self,x,y,radius,vx,vy,dx,dy):
        self.x = x
        self.y = y
        self.radius = radius
        self.vx = vx
        self.vy = vy
        self.dx = dx
        self.dy = dy
        
    def draw(self,color):
        self.img = pygame.draw.circle(win, color, (self.x,self.y), self.radius)
    def show(self):
        pygame.display.flip(self.img, (self.x, self.y))
    def move(self):
        self.x = self.x + self.dx * self.vx
        self.y = self.y + self.dy * self.vy
        if self.x >= 500 - self.radius:
            self.dx = -1
        if self.x <= self.radius:
            self.dx = 1
        if self.y >= 400 - self.radius:
            self.dy = -1
        if self.y <= self.radius:
            self.dy = 1     
            
pygame.init()

win = pygame.display.set_mode((500,400)) 
 
pygame.display.set_caption('두번째 예제') 

x = 50
y = 50
radius = 30
width = 40
height = 60
vx = 5
vy = 5
dx = 1
dy = 1

ball = Ball(x,y,radius,vx,vy,dx,dy)

clock = pygame.time.Clock()

run = True
move = False

while run:
    t1 = time.time_ns()
    clock.tick(25)

    t2 = time.time_ns()
    s = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            move = True
    if move:
        ball.move()
        
    win.fill((0,0,0)) 
    ball.draw((255,0,0))
    #pygame.draw.circle(win, (255,0,0),(x,y),radius) 
    pygame.display.update() 
    t3 = time.time_ns()
    
pygame.quit()  

세개의 공, 네개의 공, 다섯개의 공 .... 을 만들기 위해서는 위의 방법은 문제가 있음. 클래스 방법을 도입.

# ball_class_01.py

import pygame
import time
import numpy as np
import random 

pygame.init()

win = pygame.display.set_mode((500,400)) 
 
pygame.display.set_caption('두번째 예제') 
COLOR = [(255,0,0),(0,255,0),(0,0,255),(255,255,0),(0,255,255),(255,0,255)]
#COLOR = ['Red'   , 'Green' , 'Blue'  , 'Yellow'  , 'Cyan'    , 'Magenta']

class obj():
    def __init__(self,x,y,radius,vx,vy,dx,dy):
        self.x = x
        self.y = y
        self.radius = radius
        self.vx = vx
        self.vy = vy
        self.dx = dx
        self.dy = dy
        
    def draw(self,color):
        self.img = pygame.draw.circle(win, color, (self.x,self.y), self.radius)
    def show(self):
        pygame.display.flip(self.img, (self.x, self.y))
    def move(self):
        self.x = self.x + self.dx * self.vx
        self.y = self.y + self.dy * self.vy
        if self.x >= 500 - self.radius:
            self.dx = -1
        if self.x <= self.radius:
            self.dx = 1
        if self.y >= 400 - self.radius:
            self.dy = -1
        if self.y <= self.radius:
            self.dy = 1     

clock = pygame.time.Clock()

run = True
move = False
balls = []
n = 0

while run:
    t1 = time.time_ns()
    #pygame.time.delay(40)
    clock.tick(25)

    t2 = time.time_ns()
    s = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                x,y = pygame.mouse.get_pos()
                vx, vy, radius = random.randint(1, 10), random.randint(1, 10), random.randint(5, 10)
                ball = obj(x, y, radius, vx, vy, 1, 1)
                balls.append(ball)    
                #print(ball.x,ball.y,ball.radius,ball.dx, ball.dy)
            elif event.button == 3:
                move = True
                
    if move:
        for ball in balls:
            ball.move()
      
    win.fill((0,0,0))
    for ball in balls:
        r,g,b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

        ball.draw(COLOR[n%6])
        n=n+1
    #pygame.display.flip()
    pygame.display.update() 
    t3 = time.time_ns()
    #print((t3-t1)/1000000)
    
pygame.quit()
previous article
next article
Comments
Feel free to ask a question, answer or comment, etc.