vendredi 14 février 2020

Randomly spawning an enemy

I am in the progress of making a small 2d game where the objective is to eat as much poop as possible, but I'm having trouble spawning the poop at random times. I want the poop to spawn at the enemy's y location but then shoot forwards like an rpg.

    import pygame
    from pygame.locals import *
    from numpy.random import rand

    pygame.init()
    pygame.display.set_caption('STINKY BEETLE')

    screen_width = 800
    screen_height = 600
    game_running = True
    pl_x = int(screen_width/10)
    pl_y = int(screen_height/2)
    pl_width = 80
    pl_height = 40
    pl_vel = 30
    en_width = 80
    en_height = 40
    en_x = screen_width - screen_width/10 - en_width
    en_y = int(screen_height/2)
    en_yvel = -10
    po_width = 50
    po_height = 30
    po_x = 720
    po_y = en_y
    po_xvel = 15

    screen = pygame.display.set_mode((screen_width, screen_height))
    clock = pygame.time.Clock()

    while game_running:
        clock.tick(10)

        po_delay = rand(1)

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

            if event.type == MOUSEBUTTONDOWN:
                if event.button == 4 and pl_y > pl_vel:
                    pl_y -= pl_vel

                elif event.button == 5 and pl_y < screen_height - pl_width:
                    pl_y += pl_vel

        if po_delay < 0.01:
            poop(po_x, po_y)

        en_y += en_yvel
        if en_y <= 0 or en_y >= screen_height - en_height:
            en_yvel =- en_yvel

        screen.fill((0, 0, 0))
        pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
        pygame.display.update()

        pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
        pygame.display.update()

    pygame.quit()



Aucun commentaire:

Enregistrer un commentaire