-2

Now i know this is probably to much to ask, but could someone please write the code on how to make my ship shoot bullets to the left or right of the screen (depending on if the left arrow key or the right one is pressed) and to add collisions between the bullets and the barrels so that the barrels and the bullet disappear if they collide with each other.

I've been trying to do it myself for a few days now and havent managed to. I watched tutorials on YouTube too but the problem is that my code isn't the way they're showing it so everytime i tried doing it their way it didnt work.

I've been searching for help on the Python and Pygame Discord Server and most of the time i either had to wait hours until i got an answer or had to change my whole code because someone thought that way it would be better.

I know im asking for a lot, but if someone could please do that, that would really really help me out. Heres the code:

import pygame
import random

pygame.init()
# GAME ASSETS #########################

screen = pygame.display.set_mode((1000,1000))#, pygame.FULLSCREEN|pygame.SCALED)
clock = pygame.time.Clock()
menuback = pygame.image.load("Images/menubackground.png").convert()
menuback = pygame.transform.scale(menuback, (1000, 1000))
startbut = pygame.image.load("Images/startbutton.png").convert_alpha()
playagain_surf = pygame.image.load("Images/playagain.png").convert_alpha()

class Button:

    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        # self.clicked = False

    def draw(self):
        global menu, game

        pos = pygame.mouse.get_pos()
        
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0]:# and self.clicked == False:
                # self.clicked = True
                print("Clicked")
                menu = False
                game = True

        screen.blit(self.image, (self.rect.x, self.rect.y))

startbutton = Button(300, 650, startbut, 0.8)

class Button2:
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        print(self.rect)
        # self.clicked = False

    def respawn(self):
        global game, endscreen

        pos = pygame.mouse.get_pos()

        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0]:# and self.clicked == False:
                # self.clicked = True
                print("Clicked, to retart")
                endscreen = False
                game = True
                reset_game()

        screen.blit(self.image, self.rect)


playagain = Button2(250, 650, playagain_surf, 8)


class Pirateship:
    def __init__(self, x, y, blit_list, image = pygame.image.load("Images/ship.png").convert_alpha()):
       self.image = pygame.transform.scale(image, (160, 200))
       self.x = x
       self.y = y
       self.movementspeed = 3
       blit_list.append(self)


    def move(self, direction):
        if direction == "UP":
            self.y -= self.movementspeed
        if direction == "DOWN":
            self.y += self.movementspeed
        if direction == "LEFT":
            self.x -= self.movementspeed
        if direction == "RIGHT":
            self.x += self.movementspeed


class Barrel:
    def __init__(self, x, y, blit_list, barrel_list, image = pygame.image.load("Images/barrel.png").convert_alpha()):
        self.image = pygame.transform.scale(image, (175, 85))
        self.x = x
        self.y = y
        self.movementspeed = 2
        blit_list.append(self)
        barrel_list.append(self)

    def move(self):
        self.y += self.movementspeed
        if self.y >= 1050:
            self.y = random.randint(-200, -10)
            self.x = random.randint(100, 900)

sea = pygame.image.load("Images/sea.png").convert()
sea = pygame.transform.scale(sea, (1000, 1000))
gameover = pygame.image.load("Images/gameover.png").convert()
gameover = pygame.transform.scale(gameover, (1000, 1000))
gameovertext = pygame.image.load("Images/gameovertext.png").convert()
gameovertext = pygame.transform.scale(gameovertext, (500, 350))

blit_list = []
barrel_list = []

def reset_game():

    global psh

    #create barrel_list
    blit_list.clear()
    barrel_list.clear()
    for x in range(3): #creating 3 barrels
            y = random.randint(-200, -10) #minh, maxh are min heigh and max height
            x = random.randint(250, 800)
            Barrel(x, y, blit_list, barrel_list)

    #create prateship
    psh = Pirateship(500, 500, blit_list)
    # psh.image = pygame.transform.scale(psh.image, (160, 200)) # do in INIT
    # psh.rect = psh.image.get_rect(topleft=(psh.x,psh.y))

reset_game()

def menu_loop():

    global menu
    menu = True
    while menu:


        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            
        
        screen.blit(menuback, (0, 0))
        startbutton.draw()


        clock.tick(60)
        pygame.display.update()

def game_loop():

    global game, endscreen, barrel_rect





    while game:

        # Gameloop


        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                raise SystemExit

            
        if keys[pygame.K_w]:
            psh.move("UP")
        if keys[pygame.K_s]:
            psh.move("DOWN")
        if keys[pygame.K_a]:
            psh.move("LEFT")
        if keys[pygame.K_d]:
            psh.move("RIGHT")

        for barrel in barrel_list:
            barrel.move()

            



        screen.blit(sea, (0,0))
        for blit_object in blit_list:
            screen.blit(blit_object.image, (blit_object.x, blit_object.y))


    
        ship_rect = psh.image.get_rect(topleft=(psh.x, psh.y))
         for barrel_object in barrel_list:
             barrel_rect = barrel_object.image.get_rect(topleft=(barrel_object.x, 
 barrel_object.y)).inflate(-15, -20)
            if barrel_rect.colliderect(ship_rect):
                game = False
                endscreen = True

            
            
        clock.tick(60)
        pygame.display.update()

def end_loop():

    while endscreen:
        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                raise SystemExit


        screen.blit(gameover, (0, 0))
        screen.blit(gameovertext, (250, 50))
        playagain.respawn()

        clock.tick(60)
        pygame.display.update()


#MAIN LOOP
menu = True
game = False
endscreen = False

while True:

    if menu:
        menu_loop()
    elif game:
        game_loop()
    elif endscreen:
        end_loop()

I hope i indented everything alright above, had to do it manually. Sadly, i dont have the images added because i dont know how to add them to this question. Sorry :(

Prime
  • 1
  • 3
  • 1
    [How can i shoot a bullet with space bar?](https://stackoverflow.com/questions/59687250/how-can-i-shoot-a-bullet-with-space-bar/59689297#59689297) – Rabbid76 May 29 '22 at 14:58
  • Stackoverflow is a place where you show FULL error message and code which makes this problem and we try to resolve this single problem. It is not place to write all code for you. – furas May 29 '22 at 21:03
  • 1
    you already have class `Pirateship` and `Barrel` - so now create class `Bullet`. And similar to other classes it has to get start position (which you will get from ship position). It need also values `speed_x, speed_y` and function `move` which will use `speed_x, speed_y` to change position. And next you need list (similar to `barrel_list`) to keep new bullets on this list, and later use `for`-loop to run `move()` every bullet (similar to `barrel_list`) and other `for`-loop to draw every bullets (similar to `barrel_list`). And bullet has to use `colliderect` similar to `barrel`. – furas May 29 '22 at 21:09

0 Answers0