0% found this document useful (0 votes)
18 views3 pages

Arkanoid - Part3

A code thing

Uploaded by

Jennifer Cahyadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Arkanoid - Part3

A code thing

Uploaded by

Jennifer Cahyadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

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)

bola = Picture(200, 180, 50, 50, back, 'ball.png')


platform = Picture(200, 330, 99, 23, back, 'platform.png')

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

# inisiasi variabel game_over


game_over = False
while not game_over:
mw.fill(back)

for enemy in enemies:


enemy.draw()
if enemy.colliderect(bola.rect):
enemy.fill()
enemies.remove(enemy)
speed_y *= -1

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

# pergerakan bola otomatis


bola.rect.x += speed_x
bola.rect.y += speed_y

# sentukan bola dan platform


if bola.colliderect(platform.rect) or bola.rect.y < 0:
speed_y *= -1

# sentuhan bola dan batas


if bola.rect.x > 450 or bola.rect.x < 0:
speed_x *= -1

# 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)

You might also like