मैं एक अंतरिक्ष आक्रमणकारियों/क्षुद्रग्रहों का खेल बनाने की कोशिश कर रहा हूँ जहाँ आप pygame का उपयोग करके सभी दिशाओं में शूट कर सकते हैं। वर्तमान में, मैं अपने जहाज को सभी दिशाओं में ले जा सकता हूं और सभी दिशाओं में गोली मार सकता हूं, लेकिन जैसे ही मैं दिशा बदलता हूं, मेरी गोलियां जो पहले चलाई गई थीं, उस दिशा में आगे बढ़ना/चलना बंद हो जाएंगी जिस दिशा में जहाज जा रहा है। पहले ही, आपका बहुत धन्यवाद।

import pygame
import random
# intialize the pygame
pygame.init()

# dimensions of the pygame
screen = pygame.display.set_mode((800,600))

# Caption and Icon
pygame.display.set_caption("Saving Earth")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)

# images
earth_image = pygame.image.load('earth.png')

fly_up = pygame.image.load('spaceshipU.png')

fly_down = pygame.image.load('spaceshipD.png')

fly_left = pygame.image.load('spaceshipL.png')

fly_right = pygame.image.load('spaceshipR.png')

background = pygame.image.load('space.jpg')

clock = pygame.time.Clock()

#Player class
class player(object):
    def __init__(self, player_x, player_y, width, height):
        self.player_x = player_x
        self.player_y = player_y
        self.width = width
        self.height = height
        self.speed = 4
        self.left = False
        self.right = False
        self.up = True
        self.down = False

    # draws the ship to screen
    def draw(self, screen):

        if self.left:
            screen.blit(fly_left, (self.player_x,self.player_y))

        elif self.right:
            screen.blit(fly_right, (self.player_x,self.player_y))

        elif self.up:
            screen.blit(fly_up, (self.player_x,self.player_y))

        elif self.down:
            screen.blit(fly_down, (self.player_x,self.player_y))

        else:
            screen.blit(fly_up, (self.player_x,self.player_y))


class Bullet(object):
    def __init__(self, bullet_x, bullet_y, radius, color):
        self.bullet_x = bullet_x
        self.bullet_y = bullet_y
        self.radius = radius
        self.color = color
        self.speed = 6
        self.vertfacing = 1
        self.hortfacing = 1

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.bullet_x,self.bullet_y), self.radius)


# redraws game screen
def redraw_game_screen():
    screen.blit(background, (0,0))
    screen.blit(earth_image, (350, 230))
    ship.draw(screen)
    for bullet in bullets:
        bullet.draw(screen)
    pygame.display.update()

#Game Loop
ship = player(300, 300, 32, 32)
bullets = []
vertfacing = -1
hortfacing = 1
running = True
while running:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    for bullet in bullets:
        if bullet.bullet_x < 800 and bullet.bullet_x > 0 and bullet.bullet_y < 600 and bullet.bullet_y > 0 :
            if hortfacing == -1 and ship.left:
                bullet.bullet_x -= bullet.speed
            elif hortfacing == 1 and ship.right:
                bullet.bullet_x += bullet.speed
            elif vertfacing == 1 and ship.down:
                bullet.bullet_y += bullet.speed
            elif vertfacing == -1 and ship.up:
                bullet.bullet_y -= bullet.speed
        else:
            bullets.remove(bullet)

    #key commands
    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if ship.left:
            hortfacing = -1
        elif ship.right:
            hortfacing = 1
        elif ship.up:
            vertfacing = -1
        elif ship.down:
            vertfacing = 1

        if len(bullets) < 100:
            bullets.append(Bullet(round(ship.player_x + ship.width //2), round(ship.player_y + ship.height//2), 6, (255,165,0)))


    if keys[pygame.K_LEFT] and ship.player_x > ship.speed:
        ship.player_x -= ship.speed
        ship.left = True
        ship.right = False
        ship.up = False
        ship.down = False

    elif keys[pygame.K_RIGHT] and ship.player_x < 800 - ship.speed - ship.width:
        ship.player_x += ship.speed
        ship.right = True
        ship.left = False
        ship.up = False
        ship.down = False

    elif keys[pygame.K_UP] and ship.player_y > ship.speed:
        ship.player_y -= ship.speed
        ship.up = True
        ship.down = False
        ship.left = False
        ship.right = False

    elif keys[pygame.K_DOWN] and ship.player_y < 600 - ship.height - ship.speed:
        ship.player_y += ship.speed
        ship.down = True
        ship.up = False
        ship.left = False
        ship.right = False

    redraw_game_screen()

pygame.quit()
1
Trent 27 नवम्बर 2019, 11:36

1 उत्तर

आपको Bullet कक्षा में vertfacing और hortfacing की विशेषताओं को देखना होगा:

class Bullet(object):
    def __init__(self, bullet_x, bullet_y, radius, color, vertfacing, hortfacing):
        self.bullet_x = bullet_x
        self.bullet_y = bullet_y
        self.radius = radius
        self.color = color
        self.speed = 6
        self.vertfacing = vertfacing
        self.hortfacing = hortfacing

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.bullet_x,self.bullet_y), self.radius)

एक Bullet स्पॉन होने पर कंस्ट्रक्टर को एट्रिब्यूट्स पास करें:

if len(bullets) < 100:
    bullet = Bullet(round(ship.player_x + ship.width //2), 
                    round(ship.player_y + ship.height//2), 6, (255,165,0),
                    vertfacing, hortfacing)
    bullets.append(bullet)

नई स्थिति की गणना करते समय बुलेट (bullet.hortfacing, bullet.vertfacing) की विशेषताओं का उपयोग करें।

कक्षा में एक विधि जोड़ें Bullet जो इसे स्थानांतरित करती है:

class Bullet(object):

    # [...]

    def move(self):
        if self.hortfacing == -1:
            self.bullet_x -= self.speed
        elif self.hortfacing == 1:
            self.bullet_x += self.speed
        elif self.vertfacing == 1:
            self.bullet_y += self.speed
        elif self.vertfacing == -1:
            self.bullet_y -= self.speed

प्रत्येक बुलेट के लिए विधि को कॉल करें:

for bullet in bullets:
    if bullet.bullet_x < 800 and bullet.bullet_x > 0 and bullet.bullet_y < 600 and bullet.bullet_y > 0 :
        bullet.move()
    else:
        bullets.remove(bullet)
2
Rabbid76 27 नवम्बर 2019, 11:48