0% found this document useful (0 votes)
0 views2 pages

Code Python

This document is a Python script that uses Pygame to create a simple game called 'Block Blast'. The game features a movable block that can be controlled with the left and right arrow keys, and it initializes a game window with specific dimensions and colors. The main game loop handles events, updates the block's position, and refreshes the display at 60 frames per second.

Uploaded by

percobaan.login1
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)
0 views2 pages

Code Python

This document is a Python script that uses Pygame to create a simple game called 'Block Blast'. The game features a movable block that can be controlled with the left and right arrow keys, and it initializes a game window with specific dimensions and colors. The main game loop handles events, updates the block's position, and refreshes the display at 60 frames per second.

Uploaded by

percobaan.login1
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/ 2

import pygame

import random

# Inisialisasi Pygame
pygame.init()

# Ukuran layar
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
BLOCK_SIZE = 40

# Warna
COLORS = [(255, 0, 0), (0, 0, 255), (255, 165, 0)] # Merah, Biru, Oranye
BACKGROUND_COLOR = (30, 30, 30)

# Buat layar
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Block Blast")

# Kelas untuk blok


class Block:
def __init__(self, color):
self.color = color
self.x = SCREEN_WIDTH // 2 // BLOCK_SIZE * BLOCK_SIZE
self.y = SCREEN_HEIGHT - BLOCK_SIZE

def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, BLOCK_SIZE,
BLOCK_SIZE))

def move_left(self):
if self.x > 0:
self.x -= BLOCK_SIZE

def move_right(self):
if self.x < SCREEN_WIDTH - BLOCK_SIZE:
self.x += BLOCK_SIZE

# Fungsi utama game


def main():
clock = pygame.time.Clock()
block = Block(random.choice(COLORS))

running = True
while running:
screen.fill(BACKGROUND_COLOR)
block.draw()

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
block.move_left()
if keys[pygame.K_RIGHT]:
block.move_right()

pygame.display.flip()
clock.tick(60)

pygame.quit()

# Jalankan game
if __name__ == "__main__":
main()

You might also like