Arkanoid - Part3
Arkanoid - Part3
class Area():
def __init__(self, x=0, y=0, width=10, height=10, color=None):
self.rect = pygame.Rect(x, y, width, height)
self.fill_color = color
def color(self, new_color):
self.fill_color = new_color
def fill(self):
pygame.draw.rect(mw, self.fill_color, self.rect)
def outline(self, frame_color, thickness):
pygame.draw.rect(mw, frame_color, self.rect, thickness)
def collidepoint(self, x, y):
return self.rect.collidepoint(x, y)
def colliderect(self, rect):
return self.rect.colliderect(rect)
class Picture(Area):
def __init__(self, x, y, width, height, color, filename):
super().__init__(x, y, width, height, color)
self.image = pygame.image.load(filename)
def draw(self):
self.fill()
mw.blit(self.image, (self.rect.x, self.rect.y))
class Label(Area):
def set_text(self, text, fsize=12, text_color=(0, 0, 0)):
self.image = pygame.font.SysFont('verdana', fsize).render(text, True,
text_color)
def draw(self, shift_x=0, shift_y=0):
self.fill()
mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
pygame.init()
back = (200, 255, 255) #(R, G, B)
mw = pygame.display.set_mode((500, 500)) # (width, height)
mw.fill(back)
enemies = []
x = 18
y = 10
for j in range(3): # 0, 1, 2 (baris)
for i in range(9 - j): # 0-9 (kolom)
enemies.append(Picture(x + (27 * j), y, 49, 45, back, 'enemy.png'))
x += 51
y += 51
x = 18
jam = pygame.time.Clock()
FPS = 40
kanan = False
kiri = False
# kecepatan bola
speed_x = 3
speed_y = 3
bola.draw()
platform.draw()
# kontrol platform
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_a and platform.rect.x > 0:
kiri = True
if e.key == pygame.K_d and platform.rect.x < 401:
kanan = True
if e.type == pygame.KEYUP:
if e.key == pygame.K_a:
kiri = False
if e.key == pygame.K_d:
kanan = False
if platform.rect.x < 0:
kiri = False
if platform.rect.x > 401:
kanan = False
if kiri:
platform.rect.x -= 3
if kanan:
platform.rect.x += 3
# kondisi kalah
if bola.rect.y > platform.rect.y:
game_over = True
lose = Label(150, 200, 100, 70, back)
lose.set_text('YOU LOSE!', 60, (255, 0, 0))
lose.draw()
# kondisi menang
if len(enemies) == 0:
game_over = True
win = Label(150, 200, 100, 70, back)
win.set_text('YOU WIN!', 60, (0, 255, 0))
win.draw()
pygame.display.update()
jam.tick(FPS)