Skip to main content
Format as per https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks
Source Link

import pygame import random

Inisialisasi Pygame

pygame.init()

Konstanta

SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 30 WHITE = (255, 255, 255) BLUE = (135, 206, 235) GREEN = (34, 139, 34) FISH_SPEED = 2 HOOK_SPEED = 5 BOBBER_SPEED = 2

Membuat layar

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Memancing Sederhana")

Load aset (gambar bisa diganti sesuai keinginan)

try: angler_img = pygame.image.load("angler.png").convert_alpha() fish_img = pygame.image.load("fish.png").convert_alpha() hook_img = pygame.image.load("hook.png").convert_alpha() bobber_img = pygame.image.load("bobber.png").convert_alpha() except pygame.error as e: print(f"Error loading image: {e}") pygame.quit() exit()

angler_rect = angler_img.get_rect(center=(SCREEN_WIDTH // 4, SCREEN_HEIGHT - 50)) fish_rect = fish_img.get_rect() hook_rect = hook_img.get_rect(midtop=(angler_rect.midtop)) bobber_rect = bobber_img.get_rect(midbottom=hook_rect.midbottom)

Posisi awal ikan secara acak

fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width()) fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2) fish_direction = random.choice([-1, 1]) # -1 untuk kiri, 1 untuk kanan

Status memancing

casting = False reeling = False fish_caught = False

Skor

score = 0 font = pygame.font.Font(None, 36)

def draw_environment(): screen.fill(BLUE) pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)) # Tanah screen.blit(angler_img, angler_rect)

def draw_fish(): screen.blit(fish_img, fish_rect)

def draw_hook_and_bobber(): if casting or reeling: screen.blit(hook_img, hook_rect) screen.blit(bobber_img, bobber_rect)

def display_score(): score_text = font.render(f"Skor: {score}", True, WHITE) screen.blit(score_text, (10, 10))

Game loop

running = True clock = pygame.time.Clock()

while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not casting and not reeling and not fish_caught: casting = True hook_rect.midtop = angler_rect.midtop bobber_rect.midbottom = hook_rect.midbottom elif event.key == pygame.K_SPACE and casting and not reeling: reeling = True

import pygame
import random

# Inisialisasi Pygame
pygame.init()

# Konstanta
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 30
WHITE = (255, 255, 255)
BLUE = (135, 206, 235)
GREEN = (34, 139, 34)
FISH_SPEED = 2
HOOK_SPEED = 5
BOBBER_SPEED = 2

# Membuat layar
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Game Memancing Sederhana")

# Load aset (gambar bisa diganti sesuai keinginan)
try:
    angler_img = pygame.image.load("angler.png").convert_alpha()
    fish_img = pygame.image.load("fish.png").convert_alpha()
    hook_img = pygame.image.load("hook.png").convert_alpha()
    bobber_img = pygame.image.load("bobber.png").convert_alpha()
except pygame.error as e:
    print(f"Error loading image: {e}")
    pygame.quit()
    exit()

angler_rect = angler_img.get_rect(center=(SCREEN_WIDTH // 4, SCREEN_HEIGHT - 50))
fish_rect = fish_img.get_rect()
hook_rect = hook_img.get_rect(midtop=(angler_rect.midtop))
bobber_rect = bobber_img.get_rect(midbottom=hook_rect.midbottom)

# Posisi awal ikan secara acak
fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
fish_direction = random.choice([-1, 1]) # -1 untuk kiri, 1 untuk kanan

# Status memancing
casting = False
reeling = False
fish_caught = False

# Skor
score = 0
font = pygame.font.Font(None, 36)

def draw_environment():
    screen.fill(BLUE)
    pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)) # Tanah
    screen.blit(angler_img, angler_rect)

def draw_fish():
    screen.blit(fish_img, fish_rect)

def draw_hook_and_bobber():
    if casting or reeling:
        screen.blit(hook_img, hook_rect)
        screen.blit(bobber_img, bobber_rect)

def display_score():
    score_text = font.render(f"Skor: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

# Game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not casting and not reeling and not fish_caught:
                casting = True
                hook_rect.midtop = angler_rect.midtop
                bobber_rect.midbottom = hook_rect.midbottom
            elif event.key == pygame.K_SPACE and casting and not reeling:
                reeling = True

    # Pergerakan ikan
    if not fish_caught:
        fish_rect.x += FISH_SPEED * fish_direction
        if fish_rect.left < SCREEN_WIDTH // 2 or fish_rect.right > SCREEN_WIDTH:
            fish_direction *= -1
            fish_rect.y += random.randint(-20, 20)
            if fish_rect.top < 50:
                fish_rect.top = 50
            elif fish_rect.bottom > SCREEN_HEIGHT // 2:
                fish_rect.bottom = SCREEN_HEIGHT // 2

    # Mekanika melempar kail
    if casting:
        hook_rect.y -= HOOK_SPEED
        bobber_rect.midbottom = hook_rect.midbottom
        if hook_rect.top < 0:
            casting = False
            hook_rect.midtop = angler_rect.midtop
            bobber_rect.midbottom = hook_rect.midbottom

    # Mekanika menarik kail
    if reeling:
        hook_rect.y += BOBBER_SPEED
        bobber_rect.midbottom = hook_rect.midbottom
        if hook_rect.bottom > angler_rect.top:
            reeling = False
            hook_rect.midtop = angler_rect.midtop
            bobber_rect.midbottom = hook_rect.midbottom
            if fish_caught:
                fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
                fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
                fish_direction = random.choice([-1, 1])
                fish_caught = False

    # Deteksi tabrakan antara kail dan ikan
    if casting and hook_rect.colliderect(fish_rect):
        casting = False
        reeling = True
        fish_caught = True
        score += 1

    # Jika ikan tertangkap, posisikan di atas kail
    if fish_caught:
        fish_rect.midbottom = hook_rect.midtop

    # Gambar elemen game
    draw_environment()
    draw_fish()
    draw_hook_and_bobber()
    display_score()

    # Update layar
    pygame.display.flip()

    # Batasi kecepatan frame
    clock.tick(FPS) 

# Keluar dari Pygame
pygame.quit()

Keluar dari Pygame

pygame.quit()

import pygame import random

Inisialisasi Pygame

pygame.init()

Konstanta

SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 30 WHITE = (255, 255, 255) BLUE = (135, 206, 235) GREEN = (34, 139, 34) FISH_SPEED = 2 HOOK_SPEED = 5 BOBBER_SPEED = 2

Membuat layar

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Memancing Sederhana")

Load aset (gambar bisa diganti sesuai keinginan)

try: angler_img = pygame.image.load("angler.png").convert_alpha() fish_img = pygame.image.load("fish.png").convert_alpha() hook_img = pygame.image.load("hook.png").convert_alpha() bobber_img = pygame.image.load("bobber.png").convert_alpha() except pygame.error as e: print(f"Error loading image: {e}") pygame.quit() exit()

angler_rect = angler_img.get_rect(center=(SCREEN_WIDTH // 4, SCREEN_HEIGHT - 50)) fish_rect = fish_img.get_rect() hook_rect = hook_img.get_rect(midtop=(angler_rect.midtop)) bobber_rect = bobber_img.get_rect(midbottom=hook_rect.midbottom)

Posisi awal ikan secara acak

fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width()) fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2) fish_direction = random.choice([-1, 1]) # -1 untuk kiri, 1 untuk kanan

Status memancing

casting = False reeling = False fish_caught = False

Skor

score = 0 font = pygame.font.Font(None, 36)

def draw_environment(): screen.fill(BLUE) pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)) # Tanah screen.blit(angler_img, angler_rect)

def draw_fish(): screen.blit(fish_img, fish_rect)

def draw_hook_and_bobber(): if casting or reeling: screen.blit(hook_img, hook_rect) screen.blit(bobber_img, bobber_rect)

def display_score(): score_text = font.render(f"Skor: {score}", True, WHITE) screen.blit(score_text, (10, 10))

Game loop

running = True clock = pygame.time.Clock()

while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not casting and not reeling and not fish_caught: casting = True hook_rect.midtop = angler_rect.midtop bobber_rect.midbottom = hook_rect.midbottom elif event.key == pygame.K_SPACE and casting and not reeling: reeling = True

# Pergerakan ikan
if not fish_caught:
    fish_rect.x += FISH_SPEED * fish_direction
    if fish_rect.left < SCREEN_WIDTH // 2 or fish_rect.right > SCREEN_WIDTH:
        fish_direction *= -1
        fish_rect.y += random.randint(-20, 20)
        if fish_rect.top < 50:
            fish_rect.top = 50
        elif fish_rect.bottom > SCREEN_HEIGHT // 2:
            fish_rect.bottom = SCREEN_HEIGHT // 2

# Mekanika melempar kail
if casting:
    hook_rect.y -= HOOK_SPEED
    bobber_rect.midbottom = hook_rect.midbottom
    if hook_rect.top < 0:
        casting = False
        hook_rect.midtop = angler_rect.midtop
        bobber_rect.midbottom = hook_rect.midbottom

# Mekanika menarik kail
if reeling:
    hook_rect.y += BOBBER_SPEED
    bobber_rect.midbottom = hook_rect.midbottom
    if hook_rect.bottom > angler_rect.top:
        reeling = False
        hook_rect.midtop = angler_rect.midtop
        bobber_rect.midbottom = hook_rect.midbottom
        if fish_caught:
            fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
            fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
            fish_direction = random.choice([-1, 1])
            fish_caught = False

# Deteksi tabrakan antara kail dan ikan
if casting and hook_rect.colliderect(fish_rect):
    casting = False
    reeling = True
    fish_caught = True
    score += 1

# Jika ikan tertangkap, posisikan di atas kail
if fish_caught:
    fish_rect.midbottom = hook_rect.midtop

# Gambar elemen game
draw_environment()
draw_fish()
draw_hook_and_bobber()
display_score()

# Update layar
pygame.display.flip()

# Batasi kecepatan frame
clock.tick(FPS)

Keluar dari Pygame

pygame.quit()

import pygame
import random

# Inisialisasi Pygame
pygame.init()

# Konstanta
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 30
WHITE = (255, 255, 255)
BLUE = (135, 206, 235)
GREEN = (34, 139, 34)
FISH_SPEED = 2
HOOK_SPEED = 5
BOBBER_SPEED = 2

# Membuat layar
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Game Memancing Sederhana")

# Load aset (gambar bisa diganti sesuai keinginan)
try:
    angler_img = pygame.image.load("angler.png").convert_alpha()
    fish_img = pygame.image.load("fish.png").convert_alpha()
    hook_img = pygame.image.load("hook.png").convert_alpha()
    bobber_img = pygame.image.load("bobber.png").convert_alpha()
except pygame.error as e:
    print(f"Error loading image: {e}")
    pygame.quit()
    exit()

angler_rect = angler_img.get_rect(center=(SCREEN_WIDTH // 4, SCREEN_HEIGHT - 50))
fish_rect = fish_img.get_rect()
hook_rect = hook_img.get_rect(midtop=(angler_rect.midtop))
bobber_rect = bobber_img.get_rect(midbottom=hook_rect.midbottom)

# Posisi awal ikan secara acak
fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
fish_direction = random.choice([-1, 1]) # -1 untuk kiri, 1 untuk kanan

# Status memancing
casting = False
reeling = False
fish_caught = False

# Skor
score = 0
font = pygame.font.Font(None, 36)

def draw_environment():
    screen.fill(BLUE)
    pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)) # Tanah
    screen.blit(angler_img, angler_rect)

def draw_fish():
    screen.blit(fish_img, fish_rect)

def draw_hook_and_bobber():
    if casting or reeling:
        screen.blit(hook_img, hook_rect)
        screen.blit(bobber_img, bobber_rect)

def display_score():
    score_text = font.render(f"Skor: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

# Game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not casting and not reeling and not fish_caught:
                casting = True
                hook_rect.midtop = angler_rect.midtop
                bobber_rect.midbottom = hook_rect.midbottom
            elif event.key == pygame.K_SPACE and casting and not reeling:
                reeling = True

    # Pergerakan ikan
    if not fish_caught:
        fish_rect.x += FISH_SPEED * fish_direction
        if fish_rect.left < SCREEN_WIDTH // 2 or fish_rect.right > SCREEN_WIDTH:
            fish_direction *= -1
            fish_rect.y += random.randint(-20, 20)
            if fish_rect.top < 50:
                fish_rect.top = 50
            elif fish_rect.bottom > SCREEN_HEIGHT // 2:
                fish_rect.bottom = SCREEN_HEIGHT // 2

    # Mekanika melempar kail
    if casting:
        hook_rect.y -= HOOK_SPEED
        bobber_rect.midbottom = hook_rect.midbottom
        if hook_rect.top < 0:
            casting = False
            hook_rect.midtop = angler_rect.midtop
            bobber_rect.midbottom = hook_rect.midbottom

    # Mekanika menarik kail
    if reeling:
        hook_rect.y += BOBBER_SPEED
        bobber_rect.midbottom = hook_rect.midbottom
        if hook_rect.bottom > angler_rect.top:
            reeling = False
            hook_rect.midtop = angler_rect.midtop
            bobber_rect.midbottom = hook_rect.midbottom
            if fish_caught:
                fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
                fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
                fish_direction = random.choice([-1, 1])
                fish_caught = False

    # Deteksi tabrakan antara kail dan ikan
    if casting and hook_rect.colliderect(fish_rect):
        casting = False
        reeling = True
        fish_caught = True
        score += 1

    # Jika ikan tertangkap, posisikan di atas kail
    if fish_caught:
        fish_rect.midbottom = hook_rect.midtop

    # Gambar elemen game
    draw_environment()
    draw_fish()
    draw_hook_and_bobber()
    display_score()

    # Update layar
    pygame.display.flip()

    # Batasi kecepatan frame
    clock.tick(FPS) 

# Keluar dari Pygame
pygame.quit()
Source Link

import pygame import random

Inisialisasi Pygame

pygame.init()

Konstanta

SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 30 WHITE = (255, 255, 255) BLUE = (135, 206, 235) GREEN = (34, 139, 34) FISH_SPEED = 2 HOOK_SPEED = 5 BOBBER_SPEED = 2

Membuat layar

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Memancing Sederhana")

Load aset (gambar bisa diganti sesuai keinginan)

try: angler_img = pygame.image.load("angler.png").convert_alpha() fish_img = pygame.image.load("fish.png").convert_alpha() hook_img = pygame.image.load("hook.png").convert_alpha() bobber_img = pygame.image.load("bobber.png").convert_alpha() except pygame.error as e: print(f"Error loading image: {e}") pygame.quit() exit()

angler_rect = angler_img.get_rect(center=(SCREEN_WIDTH // 4, SCREEN_HEIGHT - 50)) fish_rect = fish_img.get_rect() hook_rect = hook_img.get_rect(midtop=(angler_rect.midtop)) bobber_rect = bobber_img.get_rect(midbottom=hook_rect.midbottom)

Posisi awal ikan secara acak

fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width()) fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2) fish_direction = random.choice([-1, 1]) # -1 untuk kiri, 1 untuk kanan

Status memancing

casting = False reeling = False fish_caught = False

Skor

score = 0 font = pygame.font.Font(None, 36)

def draw_environment(): screen.fill(BLUE) pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)) # Tanah screen.blit(angler_img, angler_rect)

def draw_fish(): screen.blit(fish_img, fish_rect)

def draw_hook_and_bobber(): if casting or reeling: screen.blit(hook_img, hook_rect) screen.blit(bobber_img, bobber_rect)

def display_score(): score_text = font.render(f"Skor: {score}", True, WHITE) screen.blit(score_text, (10, 10))

Game loop

running = True clock = pygame.time.Clock()

while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not casting and not reeling and not fish_caught: casting = True hook_rect.midtop = angler_rect.midtop bobber_rect.midbottom = hook_rect.midbottom elif event.key == pygame.K_SPACE and casting and not reeling: reeling = True

# Pergerakan ikan
if not fish_caught:
    fish_rect.x += FISH_SPEED * fish_direction
    if fish_rect.left < SCREEN_WIDTH // 2 or fish_rect.right > SCREEN_WIDTH:
        fish_direction *= -1
        fish_rect.y += random.randint(-20, 20)
        if fish_rect.top < 50:
            fish_rect.top = 50
        elif fish_rect.bottom > SCREEN_HEIGHT // 2:
            fish_rect.bottom = SCREEN_HEIGHT // 2

# Mekanika melempar kail
if casting:
    hook_rect.y -= HOOK_SPEED
    bobber_rect.midbottom = hook_rect.midbottom
    if hook_rect.top < 0:
        casting = False
        hook_rect.midtop = angler_rect.midtop
        bobber_rect.midbottom = hook_rect.midbottom

# Mekanika menarik kail
if reeling:
    hook_rect.y += BOBBER_SPEED
    bobber_rect.midbottom = hook_rect.midbottom
    if hook_rect.bottom > angler_rect.top:
        reeling = False
        hook_rect.midtop = angler_rect.midtop
        bobber_rect.midbottom = hook_rect.midbottom
        if fish_caught:
            fish_rect.x = random.randint(SCREEN_WIDTH // 2, SCREEN_WIDTH - fish_img.get_width())
            fish_rect.y = random.randint(50, SCREEN_HEIGHT // 2)
            fish_direction = random.choice([-1, 1])
            fish_caught = False

# Deteksi tabrakan antara kail dan ikan
if casting and hook_rect.colliderect(fish_rect):
    casting = False
    reeling = True
    fish_caught = True
    score += 1

# Jika ikan tertangkap, posisikan di atas kail
if fish_caught:
    fish_rect.midbottom = hook_rect.midtop

# Gambar elemen game
draw_environment()
draw_fish()
draw_hook_and_bobber()
display_score()

# Update layar
pygame.display.flip()

# Batasi kecepatan frame
clock.tick(FPS)

Keluar dari Pygame

pygame.quit()