Meta Description" name="description" />
# Oyun Ayarları
GENISLIK = 800
YUKSEKLIK = 400
FPS = 60
# Renkler
BEYAZ = (255, 255, 255)
SIYAH = (0, 0, 0)
KIRMIZI = (255, 0, 0)
MAVI = (0, 0, 255)
pygame.init()
ekran = pygame.display.set_mode((GENISLIK, YUKSEKLIK))
pygame.display.set_caption("Engellerden Kaçış")
saat = pygame.time.Clock()
# Oyuncu Sınıfı
class Oyuncu(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((40, 60))
self.image.fill(MAVI)
self.rect = self.image.get_rect()
self.rect.bottom = YUKSEKLIK - 10
self.rect.left = 50
self.hiz_y = 0
self.zipliyor = False
def zipla(self):
if not self.zipliyor:
self.hiz_y = -15
self.zipliyor = True
def update(self):
# Yerçekimi
self.hiz_y += 0.8
self.rect.y += self.hiz_y
# Yere basma kontrolü
if self.rect.bottom >= YUKSEKLIK - 10:
self.rect.bottom = YUKSEKLIK - 10
self.hiz_y = 0
self.zipliyor = False
# Engel Sınıfı
class Engel(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((30, 50))
self.image.fill(KIRMIZI)
self.rect = self.image.get_rect()
self.rect.x = GENISLIK + random.randint(0, 100)
self.rect.bottom = YUKSEKLIK - 10
def update(self):
self.rect.x -= 7 # Engelin hızı
if self.rect.right < 0:
self.kill() # Ekrandan çıkan engeli sil
# Gruplar
tum_spriteler = pygame.sprite.Group()
engeller = pygame.sprite.Group()
oyuncu = Oyuncu()
tum_spriteler.add(oyuncu)
# Engel oluşturma zamanlayıcısı
engel_zamani = pygame.USEREVENT + 1
pygame.time.set_timer(engel_zamani, 1500)
skor = 0
oyun_devam = True
# Oyun Döngüsü
while oyun_devam:
ekran.fill(BEYAZ)
for event in pygame.event.get():
if event.type == pygame.QUIT:
oyun_devam = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
oyuncu.zipla()
if event.type == engel_zamani:
yeni_engel = Engel()
tum_spriteler.add(yeni_engel)
engeller.add(yeni_engel)
# Güncelleme
tum_spriteler.update()
# Çarpışma Kontrolü
if pygame.sprite.spritecollide(oyuncu, engeller, False):
print(f"Oyun Bitti! Skorun: {int(skor)}")
oyun_devam = False
# Skoru artır
skor += 0.1
# Çizim
tum_spriteler.draw(ekran)
pygame.display.flip()
saat.tick(FPS)
pygame.quit()
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Birleşen Meyve Oyunu</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
<style>
body { margin: 0; overflow: hidden; background: #f0f0f0; display: flex; justify-content: center; align-items: center; height: 100vh; }
canvas { border: 5px solid #333; background: #fff; }
</style>
</head>
<body>
<script>
const { Engine, Render, Runner, Bodies, Composite, Events, Body } = Matter;
// Oyun Ayarları
const engine = Engine.create();
const render = Render.create({
element: document.body,
engine: engine,
options: { width: 400, height: 600, wireframes: false, background: '#fff' }
});
// Duvarlar
const ground = Bodies.rectangle(200, 610, 410, 60, { isStatic: true, render: { fillStyle: '#333' } });
const leftWall = Bodies.rectangle(-10, 300, 20, 600, { isStatic: true });
const rightWall = Bodies.rectangle(410, 300, 20, 600, { isStatic: true });
Composite.add(engine.world, [ground, leftWall, rightWall]);
// Meyve Türleri (Boyut, Renk, Seviye)
const FRUIT_TYPES = [
{ radius: 15, color: '#ff4d4d', label: 'Elma', level: 1 },
{ radius: 25, color: '#ffcc00', label: 'Portakal', level: 2 },
{ radius: 35, color: '#9933ff', label: 'Üzüm', level: 3 },
{ radius: 50, color: '#33cc33', label: 'Karpuz', level: 4 }
];
let currentFruit = null;
// Yeni Meyve Hazırla
function createFruit(x, y, typeIndex, isStatic = false) {
const type = FRUIT_TYPES[typeIndex];
const fruit = Bodies.circle(x, y, type.radius, {
restitution: 0.5,
friction: 0.1,
isStatic: isStatic,
label: "fruit_" + typeIndex,
render: { fillStyle: type.color }
});
fruit.level = typeIndex;
return fruit;
}
// İlk meyveyi oluştur
function spawnNewFruit() {
currentFruit = createFruit(200, 50, 0, true);
Composite.add(engine.world, currentFruit);
}
spawnNewFruit();
// Fare Hareket Ettirme
window.addEventListener('mousemove', (e) => {
if (currentFruit && currentFruit.isStatic) {
const canvasRect = render.canvas.getBoundingClientRect();
let newX = e.clientX - canvasRect.left;
newX = Math.max(20, Math.min(380, newX));
Body.setPosition(currentFruit, { x: newX, y: 50 });
}
});
// Tıklayınca Bırakma
window.addEventListener('mousedown', () => {
if (currentFruit && currentFruit.isStatic) {
Body.setStatic(currentFruit, false);
const fruitToRelease = currentFruit;
currentFruit = null;
setTimeout(spawnNewFruit, 1000);
}
});
// Çarpışma ve Birleşme Kontrolü
Events.on(engine, 'collisionStart', (event) => {
event.pairs.forEach((pair) => {
const { bodyA, bodyB } = pair;
if (bodyA.label.startsWith('fruit_') && bodyB.label.startsWith('fruit_')) {
if (bodyA.level === bodyB.level && bodyA.level < FRUIT_TYPES.length - 1) {
// Pozisyonu hesapla (iki meyvenin ortası)
const newX = (bodyA.position.x + bodyB.position.x) / 2;
const newY = (bodyA.position.y + bodyB.position.y) / 2;
// Eski meyveleri sil
Composite.remove(engine.world, [bodyA, bodyB]);
// Yeni (bir üst seviye) meyveyi ekle
const nextFruit = createFruit(newX, newY, bodyA.level + 1);
Composite.add(engine.world, nextFruit);
}
}
});
});
Render.run(render);
const runner = Runner.create();
Runner.run(runner, engine);
</script>
</body>
</html>
2
2
29KB
85KB
201.0ms
136.0ms
201.0ms