script game ular

Run Settings
LanguagePython
Language Version
Run Command
import pygame import time import random # Inisialisasi pygame pygame.init() # Warna white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) # Dimensi layar width = 800 height = 600 # Membuat layar screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Snake Game") # Kecepatan ular clock = pygame.time.Clock() snake_speed = 15 # Ukuran blok block_size = 20 # Font untuk pesan font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35) def your_score(score): """Menampilkan skor pada layar.""" value = score_font.render("Your Score: " + str(score), True, yellow) screen.blit(value, [10, 10]) def our_snake(block_size, snake_list): """Menggambar ular di layar.""" for block in snake_list: pygame.draw.rect(screen, green, [block[0], block[1], block_size, block_size]) def message(msg, color): """Menampilkan pesan pada layar.""" mesg = font_style.render(msg, True, color) screen.blit(mesg, [width / 6, height / 3]) def gameLoop(): """Fungsi utama untuk menjalankan game.""" game_over = False game_close = False # Posisi awal ular x1 = width / 2 y1 = height / 2 # Pergerakan awal x1_change = 0 y1_change = 0 # Panjang ular snake_list = [] length_of_snake = 1 # Posisi makanan foodx = round(random.randrange(0, width - block_size) / block_size) * block_size foody = round(random.randrange(0, height - block_size) / block_size) * block_size while not game_over: while game_close: screen.fill(blue) message("You lost! Press Q-Quit or C-Play Again", red) your_score(length_of_snake - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() # Mengontrol pergerakan ular for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -block_size y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = block_size y1_change = 0 elif event.key == pygame.K_UP: y1_change = -block_size x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = block_size x1_change = 0 # Memastikan ular tetap berada di layar if x1 >= width or x1 < 0 or y1 >= height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(black) # Gambar makanan pygame.draw.rect(screen, red, [foodx, foody, block_size, block_size]) # Menambah posisi ular snake_head = [] snake_head.append(x1) snake_head.append(y1) snake_list.append(snake_head) if len(snake_list) > length_of_snake: del snake_list[0] # Game over jika ular menabrak dirinya sendiri for block in snake_list[:-1]: if block == snake_head: game_close = True # Gambar ular our_snake(block_size, snake_list) your_score(length_of_snake - 1) pygame.display.update() # Makanan dimakan if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, width - block_size) / block_size) * block_size foody = round(random.randrange(0, height - block_size) / block_size) * block_size length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() # Jalankan game gameLoop()
Editor Settings
Theme
Key bindings
Full width
Lines