Google Dinozor oyununu Python'da Pygame ile yapmak için aşağıdaki adımları takip edebilirsiniz:
Gereksinimler:
Python 3.x
Pygame kütüphanesi (kurulum için pip install pygame komutunu kullanabilirsiniz)
Oyun Kodu:
python
import pygame
import sys
# Ekran boyutları
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 400
# Dinozor boyutları
DINO_WIDTH = 60
DINO_HEIGHT = 60
# Zemin yüksekliği
GROUND_HEIGHT = 50
# Dinozorun başlangıç konumu
DINO_X = 100
DINO_Y = SCREEN_HEIGHT - GROUND_HEIGHT - DINO_HEIGHT
# Dinozorun hızları
DINO_SPEED_X = 5
DINO_SPEED_Y = 10
# Engellerin boyutları
OBSTACLE_WIDTH = 30
OBSTACLE_HEIGHT = 30
# Engellerin başlangıç konumu
OBSTACLE_X = SCREEN_WIDTH
OBSTACLE_Y = SCREEN_HEIGHT - GROUND_HEIGHT - OBSTACLE_HEIGHT
# Puan
SCORE = 0
# Pygame başlangıç
pygame.init()
# Ekran oluşturma
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Dinozor resmi
dino_image = pygame.Surface((DINO_WIDTH, DINO_HEIGHT))
dino_image.fill((255, 0, 0)) # Kırmızı renk
# Engeller resmi
obstacle_image = pygame.Surface((OBSTACLE_WIDTH, OBSTACLE_HEIGHT))
obstacle_image.fill((0, 0, 255)) # Mavi renk
# Zemin resmi
ground_image = pygame.Surface((SCREEN_WIDTH, GROUND_HEIGHT))
ground_image.fill((128, 128, 128)) # Gri renk
# Dinozorun konumu
dino_x = DINO_X
dino_y = DINO_Y
# Engellerin konumu
obstacle_x = OBSTACLE_X
obstacle_y = OBSTACLE_Y
# Dinozorun hızı
dino_speed_x = DINO_SPEED_X
dino_speed_y = DINO_SPEED_Y
# Oyun döngüsü
while True:
# Etkinlikler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
dino_speed_y = -DINO_SPEED_Y
# Dinozorun hareketi
dino_x += dino_speed_x
dino_y += dino_speed_y
# Dinozorun zeminle teması
if dino_y + DINO_HEIGHT > SCREEN_HEIGHT - GROUND_HEIGHT:
dino_y = SCREEN_HEIGHT - GROUND_HEIGHT - DINO_HEIGHT
dino_speed_y = 0
# Engellerin hareketi
obstacle_x -= 5
if obstacle_x < -OBSTACLE_WIDTH:
obstacle_x = SCREEN_WIDTH
SCORE += 1
# Çarpışma kontrolü
if (dino_x + DINO_WIDTH > obstacle_x and
dino_x < obstacle_x + OBSTACLE_WIDTH and
dino_y + DINO_HEIGHT > obstacle_y and
dino_y < obstacle_y + OBSTACLE_HEIGHT):
print("Oyun bitti! Puanınız:", SCORE)
pygame.quit()
sys.exit()
# Ekran güncelleme
screen.fill((255, 255, 255)) # Beyaz renk
screen.blit(dino_image, (dino_x, dino_y))
screen.blit(obstacle_image, (obstacle_x, obstacle_y))
screen.blit(ground_image, (0, SCREEN_HEIGHT - GROUND_HEIGHT))
pygame.display.flip()
# Zaman kontrolü
pygame.time.Clock().tick(60)
Oyunun Çalışması:
Kodu kopyalayın ve bir Python dosyasına yapıştırın (örneğin dino_game.py).
Terminal veya komut istemini açın ve python dino_game.py komutunu çalıştırın.
Oyun penceresi açılacaktır. Dinozoru hareket ettirmek için sağ ve sol ok tuşlarını kullanabilirsiniz. Engelleri atlamak için space tuşuna basabilirsiniz.
Oyun bittiğinde, puanınız ekranda görünecektir.
Not: Bu kod, Google Dinozor oyununun basit bir versiyonudur. Oyunun orijinal sürümü daha karmaşıktır ve daha fazla özellik içerir.