Lecture 움직이는 공들 2
Lecture
• Views 217
• Comments 0
• Last Updated at 1 year ago
- 파이게임
보드 생성
공하나와 베이스를 하나만 생성하는 프로그램. crash함수는 두 개가 접촉하였는지 확인한다.
# ball_03_base.py
import pygame
import time
import numpy as np
import random
def crash(base,ball):
if (base.x <= ball.x) and (base.x + base.width >= ball.x):
if (base.y <= ball.y + ball.radius):
return True
else:
return False
elif (base.x > ball.x) and (base.x < ball.x + ball.radius):
if ((base.y - ball.y)**2 + (base.x - ball.x)**2) <= ball.radius**2:
return True
else:
return False
elif (base.x + base.width < ball.x) and (base.x + base.width > ball.x - ball.radius):
if ((base.y - ball.y)**2 + (base.x - ball.x)**2) <= ball.radius**2:
return True
else:
return False
else:
return False
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
class Base():
def __init__(self,x,y,width,height,vel):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = vel
def draw(self,color):
self.img = pygame.draw.rect(win, color, (self.x, self.y, self.width, self.height))
def show(self):
pygame.display.flip(self.img, (self.x, self.y))
pygame.init()
win = pygame.display.set_mode((500,400))
pygame.display.set_caption('두번째 예제')
x, y, radius, vx, vy, dx, dy = 50, 50, 30, 5, 5, 1, 1
bx, by, width, height, vel = 50, 380, 80, 10, 5
clock = pygame.time.Clock()
run = True
move = False
ball = Ball(x,y,radius,vx,vy,dx,dy)
base = Base(bx, by, width, height,vel)
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
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
base.x = base.x - base.vel
elif event.key == pygame.K_RIGHT:
base.x = base.x + base.vel
elif event.key == pygame.K_SPACE:
print('key 2 is pressed')
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
base.x = base.x - base.vel
if keys[pygame.K_RIGHT]:
base.x = base.x + base.vel
if keys[pygame.K_SPACE]:
print('space is pressed')
if move:
ball.x = ball.x + ball.dx * ball.vx
ball.y = ball.y + ball.dy * ball.vy
if ball.x >= 500 - ball.radius:
ball.dx = -1
if ball.x <= ball.radius:
ball.dx = 1
if ball.y >= 400 - ball.radius:
ball.dy = -1
if ball.y <= ball.radius:
ball.dy = 1
if crash(base,ball):
print('Crashed')
win.fill((0,0,0))
ball.draw((255,0,0))
base.draw((255,0,0))
#pygame.draw.circle(win, (255,0,0),(x,y),radius)
#pygame.draw.rect(win, (255,0,0), (bx, by, width, height))
pygame.display.update()
t3 = time.time_ns()
pygame.quit()
previous article
next article
Login to write a comment.