강의노트 움직이는 공과 미사일

강의노트 • 조회수 344 • 댓글 0 • 수정 11개월 전  
  • 파이게임
  • 파이게임

게임

  • 움직이는 베이스에서 미사일을 발사하여 움직이는 공을 터트리는 게임이다.
import pygame
import random

pygame.init()
pygame.mixer.init()

screen_width = 500
screen_height = 600

screen = pygame.display.set_mode((screen_width,screen_height))
font = pygame.font.SysFont('한컴말랑말랑', 30)
text = font.render('아무키나 누르세요', True, (255,255,255))

bg = pygame.image.load("D:/전기전자/terraria.jpg")
#r = get_rect(bg)
bg = pygame.transform.scale(bg, (500,600))
clock = pygame.time.Clock()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
blue = (0,0,255)



x = 50
y = 380
r = 50
music3 = 'bomb_01.mp3'

def crash(m,b):
    if b.y - b.r < m.y < b.y + b.r :
        if b.x - b.r < m.x < b.x + b.r:
            return True
        if b.x - b.r < m.x + 5 < b.x + b.r:
            return True
    if b.y - b.r < m.y - 10 < b.y + b.r:
        if b.x - b.r < m.x < b.x + b.r:
            return True
        if b.x - b.r < m.x + 5 < b.x + b.r:
            return True

class Base:
    def __init__(self,x,y,width,color):
        self.x = x
        self.y = y
        self.width = width
        self.color = color
    
class Ball:
    c = [1, -1]
    def __init__(self,x,y,r):
        self.x = x
        self.y = y
        self.r = r
        self.dx = random.choice(self.c)
        self.dy = random.choice(self.c)
        self.vx = random.randint(1, 5)
        self.vy = random.randint(1, 5)
        self.color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))

class Missile:
    c = [1, -1]
    def __init__(self,x,w,y):
        self.x = x + int(w/2)
        self.y = y
        self.v = 15
        self.color = red


ball = []
missile = []
score = 0
base = Base(50,screen_height - 20,50,black)

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        #print(event)
        if event.type == pygame.QUIT:
            #break
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
                running = False    
    screen.fill(black)
    screen.blit(text, (100,200))
    pygame.display.update()

running = True

while running:
    clock.tick(60)  #fps
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        #if event.type == pygame.MOUSEBUTTONDOWN:
    if random.random() > 0.98:
        bx = random.randint(100, 400)
        by = random.randint(100, 300)
        br = random.randint(5, 30)
        ball.append(Ball(bx,by,br))

    k = pygame.key.get_pressed()
    if k[pygame.K_RIGHT]:
        base.x = base.x + 5 #random.randint(1, 50)
        if base.x > (screen_width - base.width):
            base.x= (screen_width - base.width)
    if k[pygame.K_LEFT]:
        base.x = base.x - 5
        if base.x < 0:
            base.x = 0
    if k[pygame.K_SPACE]:
        missile.append(Missile(base.x,base.width, base.y))

        
    for b in ball:
        if b.x + b.r > screen_width:
            b.dx = -1
        if b.x - b.r < 0:
            b.dx = 1
        if b.y + b.r > screen_height:
            b.dy = -1
        if b.y - b.r < 0:
            b.dy = 1
        b.x = b.x + b.dx*b.vx
        b.y = b.y + b.dy*b.vy
    i = 0
    m_d = []
    for m in missile:
        m.y = m.y - m.v
        if m.y < 0:
            m_d.append(i)
        i +=1
    m_d.reverse()
    for i in m_d:
        missile.pop(i)
    
    
    m_d = []
    b_d = []
    i = 0
    for b in ball:
        j = 0
        for m in missile:
            if crash(m, b):
                b_d.append(i)
                m_d.append(j)
            j += 1
        i += 1
    if len(b_d) > 0:
        b_d = list(set(b_d))
    if len(m_d) > 0:
        m_d = list(set(m_d))
    b_d.reverse()
    m_d.reverse()
    for i in m_d:
        missile.pop(i)
    for i in b_d:
        ball.pop(i)
        pygame.mixer.music.load(music3)
        pygame.mixer.music.play()
        score += 1
        
    b_d = []
    i = 0
    for b in ball:
        if crash(base,b):
            running = False
            #b_d.append(i)
        i += 1
        
    score_text = font.render(f'score : {score}', True, (255,0,0))

    screen.fill(white)
    screen.blit(bg, (0,0))
    for b in ball:
        pygame.draw.circle(screen, b.color, [b.x,b.y],b.r,0)
    for m in missile:
        pygame.draw.rect(screen, m.color, [m.x, m.y, 5,10],0)
    
    pygame.draw.rect(screen, black, [base.x,base.y,50,10],0)
    screen.blit(score_text, (100,10))
    pygame.display.flip()
    #print(len(missile))

running = True
text = font.render('Game Over', True, (255,0,0))

while running:
    clock.tick(50)
    for event in pygame.event.get():
        #print(event)
        if event.type == pygame.QUIT:
            #break
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
                running = False    
    screen.fill(white)
    screen.blit(bg, (0,0))
    screen.blit(score_text, (100,50))
    screen.blit(text, (100,300))
    pygame.display.update()
    
pygame.quit()
이전 글
다음 글
댓글
댓글로 소통하세요.