0

How do I shoot an object from my existing sprite? I want the shooting object to collide with other objects and remove them from the screen. I only want to control my spaceship sprite object with a mouse and left click to shoot an object forward. When that "bullet" hits an object I want that collision to remove that object from the screen

import pygame
import random
from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()

sprite object

class spaceship(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("ship.png")
        self.image = pygame.transform.scale(self.image, (50,50))
        self.rect = self.image.get_rect()
        self.movex = 0
        self.movey = 0
        self.frame = 0
    def control(self, x, y):
        self.movex += x
        self.movey += y
    def update(self):

        self.rect.center = pygame.mouse.get_pos()
       
        
# screen      
width = 1600
height = 1000
screen = pygame.display.set_mode((width, height))
background = pygame.image.load("spacebg.jpg")
pygame.mouse.set_visible(False)

WHITE = (255,255,255)
colorbg = WHITE
screen.fill(colorbg)
pygame.display.set_caption("background color is " + str(background))
x = 50
y = 50

#spaceship
spaceship = spaceship()
spaceship_group = pygame.sprite.Group()
spaceship_group.add(spaceship)

# txt object in upper left corner 
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('Update Time', True, WHITE)
textRect = text.get_rect()

Game loop

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

    keys = pygame.key.get_pressed()

       
    pygame.display.flip()
    screen.blit(background, (0,0))
    screen.blit(text,(0,0))
    spaceship_group.draw(screen)
    spaceship_group.update()
    clock.tick(60)

    
    pygame.display.update()                         

pygame.quit()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • Make a new sprite class called bullet. When you shoot you simply add the Bullet() to a spriteGroup and .draw(screen) it (.update() it to move the bullet). – CozyCode Dec 13 '21 at 10:58

0 Answers0