Sphynx
Asistan
- Katılım
- 21 Nisan 2020
- Mesajlar
- 209
- Reaksiyon puanı
- 150
- Puanları
- 43
Python:
from pyglet.window import key
import pyglet
import random
# Oyuncu karakteri
class Player:
def __init__(self):
self.image = pyglet.image.load("player.png")
self.sprite = pyglet.sprite.Sprite(self.image, x=200, y=50)
self.speed = 200
self.bullet_list = []
self.keys = dict(left=False, right=False, up=False, down=False, space=False)
def on_key_press(self, symbol, modifiers):
if symbol == key.LEFT:
self.keys['left'] = True
elif symbol == key.RIGHT:
self.keys['right'] = True
elif symbol == key.UP:
self.keys['up'] = True
elif symbol == key.DOWN:
self.keys['down'] = True
elif symbol == key.SPACE:
self.keys['space'] = True
def on_key_release(self, symbol, modifiers):
if symbol == key.LEFT:
self.keys['left'] = False
elif symbol == key.RIGHT:
self.keys['right'] = False
elif symbol == key.UP:
self.keys['up'] = False
elif symbol == key.DOWN:
self.keys['down'] = False
elif symbol == key.SPACE:
self.keys['space'] = False
def update(self, dt, keys):
# Oyuncu hareketleri
if self.keys[pyglet.window.key.LEFT]:
self.sprite.x -= self.speed * dt
if self.keys[pyglet.window.key.RIGHT]:
self.sprite.x += self.speed * dt
if self.keys[pyglet.window.key.UP]:
self.sprite.y += self.speed * dt
if self.keys[pyglet.window.key.DOWN]:
self.sprite.y -= self.speed * dt
if self.keys[pyglet.window.key.SPACE]:
self.fire()
# Uzay aracı ekran sınırları
if self.sprite.x < 0:
self.sprite.x = 0
if self.sprite.x > 380:
self.sprite.x = 380
if self.sprite.y < 0:
self.sprite.y = 0
if self.sprite.y > 280:
self.sprite.y = 280
def fire(self):
bullet = Bullet(self.sprite.x + 25, self.sprite.y + 50)
self.bullet_list.append(bullet)
# Düşman karakteri
class Enemy:
def __init__(self):
self.image = pyglet.image.load("enemy.png")
x_pos = random.randint(0, 1600 - self.image.width)
y_pos = random.randint(300, 1000)
self.sprite = pyglet.sprite.Sprite(self.image, x=x_pos, y=y_pos)
def update(self, dt):
# Düşman hareketleri
speed = random.randint(50, 150)
self.sprite.y -= speed * dt
# Mermi karakteri
class Bullet:
def __init__(self, x_pos, y_pos):
self.image = pyglet.image.load("bullet.png")
self.sprite = pyglet.sprite.Sprite(self.image, x=x_pos - 10, y=y_pos + 30)
def update(self, dt):
# Mermi hareketleri
speed = 500
self.sprite.y += speed * dt
# Pencere oluşturma ve oyun döngüsü
window = pyglet.window.Window(width=1200,height=750)
@window.event
def on_key_press(symbol, modifiers):
player.keys = key.KeyStateHandler()
player.keys[symbol] = True
@window.event
def on_key_release(symbol, modifiers):
player.keys[symbol] = False
player = Player()
bullet_list = []
enemy_list = []
for i in range(20):
enemy = Enemy()
enemy_list.append(enemy)
keys = key.KeyStateHandler()
window.push_handlers(keys)
@window.event
def on_draw():
window.clear()
player.sprite.draw()
for enemy in enemy_list:
enemy.sprite.draw()
for bullet in player.bullet_list:
bullet.sprite.draw()
def update(dt):
keys = pyglet.window.key.KeyStateHandler()
window.push_handlers(keys)
player.update(dt, player.keys)
for enemy in enemy_list:
enemy.update(dt)
if enemy.sprite.y < -enemy.sprite.height:
enemy_list.remove(enemy)
for bullet in player.bullet_list:
bullet.update(dt)
if bullet.sprite.y > window.height:
player.bullet_list.remove(bullet)
time_elapsed = 0
time_elapsed += dt
if time_elapsed > 3:
enemy = Enemy()
enemy_list.append(enemy)
time_elapsed = 0
pyglet.clock.schedule_interval(update, 1/60.0)
pyglet.app.run()