Pygame-nesne hareket etmesine rağmen sürekli ateşleme aynı yerde çalışıyor

Mermi ateşleyen bir kod yazıyorum, Python Oyun Geliştirme - Ateşleme Mekaniği - YouTube videosundan alıntılayarak yaptım bir çok şeyi. Gemi hareket etmesine rağmen mermileri sadece orta noktadan atıyor. Bunun sebebi ne olabilir?

import pygame
import time
pygame.init()
durum = True
gen = 900
yuk = 700
fps = 25
saat = pygame.time.Clock()
pencere = pygame.display.set_mode((gen,yuk))
class oyuncu(pygame.sprite.Sprite):
    def __init__(self,birinci_grup) :
        super().__init__()
        self.image = pygame.image.load('uzay_gemisi.png')
        self.rect = self.image.get_rect()
        self.rect.centerx = gen/2
        self.rect.centery = yuk-100
        self.birinci_grup = birinci_grup
        self.hiz = 12
    def update(self):
        klavye = pygame.key.get_pressed()
        if klavye[pygame.K_RIGHT] and self.rect.right <= 900:
            self.rect.centerx += self.hiz
        elif klavye[pygame.K_LEFT] and self.rect.left >= 0:
            self.rect.centerx -= self.hiz
    def atesle(self):
        if len(self.birinci_grup) < 2:
            mermi(self.birinci_grup,  self.rect.x,  self.rect.top)
class mermi(pygame.sprite.Sprite):
    def __init__(self, birinci_grup,x,y):
        super().__init__()
        self.image=pygame.image.load('line.png')
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.hiz = 12
        birinci_grup.add(self)
    def update(self):
        self.rect.y -= self.hiz
        if self.rect.bottom < 0:
            self.kill()
ikinci_grup = pygame.sprite.Group()
ucuncu_grup = pygame.sprite.Group()
oyuncu2 = oyuncu(ikinci_grup)
ucuncu_grup.add(oyuncu2)
while durum:
    for etk in pygame.event.get():
        if etk.type == pygame.QUIT:
            durum = False
        if etk.type == pygame.KEYDOWN:
            if etk.key == pygame.K_SPACE:
                oyuncu(ikinci_grup).atesle()
    pencere.fill((0,0,0))
    ikinci_grup.update()
    ikinci_grup.draw(pencere)
    ucuncu_grup.update()
    ucuncu_grup.draw(pencere)
    pygame.display.update()
    saat.tick(fps)
pygame.quit()  

Merhaba, verdiğiniz videodaki koda baktım.

Oradan sizin koda biraz benzettim. Değişkenleri, birinci grup, ikinci grup, üçüncü grup gibi tanımladığınızda takip etmekte zorlanıyorum.

Tahminim, sınıfları bir birine doğru tanıtmadığınız için uzay gemisi x pozisyonunu, mermiye aktaramıyorsunuz.

O kadar self ve class karmaşası var ki değişkenlerden dolayı, okumakta zorlandım.

Bu nedenle videodaki koda sadık kalarak kodunuzu oynadım.

Png dosylarını kendim uydurdum, kendi dosya ve dosya yollarınıza göre yeniden düzenlemeniz gerekebilir.

import pygame
import time
pygame.init()
durum = True
gen = 900
yuk = 700
fps = 25
saat = pygame.time.Clock()
pencere = pygame.display.set_mode((gen,yuk))

class Oyuncu(pygame.sprite.Sprite):
    def __init__(self,oyuncu_mermi_grup) :
        super().__init__()
        self.image = pygame.image.load('uzay_gemisi.png')
        self.rect = self.image.get_rect()
        self.rect.centerx = gen/2
        self.rect.centery = yuk-100
        self.oyuncu_mermi_grup = oyuncu_mermi_grup
        self.hiz = 12
    def update(self):
        klavye = pygame.key.get_pressed()
        if klavye[pygame.K_RIGHT] and self.rect.right <= 900:
            self.rect.centerx += self.hiz
            
        elif klavye[pygame.K_LEFT] and self.rect.left >= 0:
            self.rect.centerx -= self.hiz
            
    def atesle(self):
        if len(self.oyuncu_mermi_grup) < 2:
            #mermi(self.birinci_grup,  x,  self.rect.top)
            mermi(self.rect.centerx, self.rect.top, self.oyuncu_mermi_grup)
            

class mermi(pygame.sprite.Sprite):
    def __init__(self,x,y,oyuncu_mermi_grup):
        super().__init__()
        self.image=pygame.image.load('line.png')
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.hiz = 12
        oyuncu_mermi_grup.add(self)
    def update(self):
    
        self.rect.y -= self.hiz
        if self.rect.bottom < 0:
            self.kill()

mermi_grup=pygame.sprite.Group()            
oyuncu_grup=pygame.sprite.Group() 
oyuncu= Oyuncu(mermi_grup)
oyuncu_grup.add(oyuncu) 
durum = True


while durum:
    for etkinlik in pygame.event.get():
        if etkinlik.type == pygame.QUIT:
            durum = False
        if etkinlik.type==pygame.KEYDOWN:
            oyuncu.atesle()
    pencere.fill((0,0,0))
    oyuncu_grup.update()
    oyuncu_grup.draw(pencere)
    mermi_grup.update()
    mermi_grup.draw(pencere)
    pygame.display.update()
    saat.tick(fps)
pygame.quit()  

Açıkcası bu kodu bu kadar karmaşık tasarlamaya gerek var mı bilmiyorum. Daha sadeleştirilebilir bence.

1 Beğeni

Çok teşekkür ederim. Sıkıntı döngünün içerisinde ateşleme kısmında nesne ile sınıfı karıştırmammış. Sınıflar konusu çok yeni benim için bu sebeple grupları 1. 2. gibi tanımlamıştım ama daha kafa karıştırıcı oldu :).

1 Beğeni