0

Hello! I am looking for some help, and have provided the code. I'm just trying to do some simple stuff. I'm trying to create a simple game without using classes (but using lists).
What I'm trying to do is create collision detection among list objects ("bullets" and "baddies"), as you'll be able to see in the code.
I'm also trying to fire 10 bullets at once from an "ammoCrate". My problem is that I have to stay on the "ammoCrate" in order for the bullets to do their thing, otherwise they stop in place and disappear, but continue if the "playerRect" stays on the "ammoCrate". Additionally, I'm firing the bullets from a given position, and not from the "ammoCrate" that the "playerRect" is touching/colliding with. NOTE: I'm using primitives instead of loaded images. Here is the code:

import pygame, sys, time, random
from pygame.locals import *

pygame.init()


def collision(playerRect,baddies,baddiesA):
    for b in baddies:
        if playerRect.colliderect(b):
            return True
    for bA in baddiesA:
        if playerRect.colliderect(bA):
            return True
    return False

def bulletCollide(bullets,baddies,baddiesA):
    for b in baddies:#lists will not collide?
        if bullets.colliderect(b):
            baddies.remove(b)
    for bA in baddiesA:
        if bullets.colliderect(bA):
            baddiesA.remove(bA)


def ammoCollide(playerRect,ammoCrate):
    for x in ammoCrate[:]:
        if playerRect.colliderect(x):
            #if ammoCrate.remove(x), the bullets cease bc the cmd is complete. Cmd must keep going.
            for i in range(len(bullets)):
                pygame.draw.rect(screen,WHITE,bullets[i])
                for i in bullets[0:0]:
                    i.move_ip(2,-2)
                for i in bullets[0:1]:
                    i.move_ip(2,2)
                for i in bullets[1:2]:
                    i.move_ip(0,2)
                for i in bullets[2:3]:
                    i.move_ip(2,0)
                for i in bullets[3:4]:
                    i.move_ip(4,-4)
                for i in bullets[4:5]:
                    i.move_ip(-4,4)
                for i in bullets[5:6]:
                    i.move_ip(-2,2)
                for i in bullets[6:7]:
                    i.move_ip(4,4)
                for i in bullets[7:8]:
                    i.move_ip(6,-6)
                for i in bullets[8:9]:
                    i.move_ip(-6,6)
                for i in bullets[9:10]:
                    i.move_ip(6,6)
            bulletCollide(bullets,baddies,baddiesA)


WINDOWWIDTH = 400
WINDOWHEIGHT = 400
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),0,32)
pygame.display.set_caption('Space Pirates!')

mainClock = pygame.time.Clock()

#SHIP = pygame.image.load('spaceship.png')

BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

badCounter = 0 
NextBaddie = 20 
BADDIESIZE = 8
playerMoveSpeed = 6

playerRect = pygame.Rect(5,5,8,8)

#lists using pygame.Rect below
ammoCrate = []
for x in range(2):
    ammoCrate.append(pygame.Rect(30,250,8,8))
    ammoCrate.append(pygame.Rect(60,200,8,8))

bullets = []
for i in range(10): #starting 10: adding the same parameters as pygame.rect (random coordinates and size)
    bullets.append(pygame.Rect(30,250,4, 4))#figure x,y coords for bullets to fire from collided coords.

#make baddies appear randomly in a middle area.
baddies = []
for b in range(2): #starting 2: adding the same parameters as pygame.rect (random coordinates and size)
    baddies.append(pygame.Rect(random.randint(WINDOWWIDTH/2 - 20, WINDOWWIDTH/2 +20 - BADDIESIZE),
                               random.randint(WINDOWHEIGHT/2 -20, WINDOWHEIGHT/2 +20 - BADDIESIZE), BADDIESIZE, BADDIESIZE))

baddiesA = []
for bA in range(2): #starting 2: adding the same parameters as pygame.rect (random coordinates and size)
    baddiesA.append(pygame.Rect(random.randint(WINDOWWIDTH/2 - 20, WINDOWWIDTH/2 +20 - BADDIESIZE),
                               random.randint(WINDOWHEIGHT/2 -20, WINDOWHEIGHT/2 +20 - BADDIESIZE), BADDIESIZE, BADDIESIZE))

while True:

    #set up the game
    moveLeft = moveRight = moveUp = moveDown = False

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == K_ESCAPE:
                pygame.quit()
                sys.exit()

        badCounter += 1 #every iteration +1, when it gets to 30 NextBaddie

        if badCounter >= NextBaddie:
            badCounter = 0
            baddies.append(pygame.Rect(random.randint(WINDOWWIDTH/2 - 20, WINDOWWIDTH/2 +20 - BADDIESIZE),
                                       random.randint(WINDOWHEIGHT/2 -20, WINDOWHEIGHT/2 +20 - BADDIESIZE), BADDIESIZE, BADDIESIZE))

            baddiesA.append(pygame.Rect(random.randint(WINDOWWIDTH/2 - 20, WINDOWWIDTH/2 +20 - BADDIESIZE),
                                       random.randint(WINDOWHEIGHT/2 -20, WINDOWHEIGHT/2 +20 - BADDIESIZE), BADDIESIZE, BADDIESIZE))

        for b in baddies:#Red baddies.
            b.move_ip(random.gauss(0,3),random.gauss(0,3))

            #delete baddies that go beyond the screen (memory)
        for b in baddies:
            if b.top > WINDOWHEIGHT:
                baddies.remove(b)
            if b.left > WINDOWWIDTH:
                baddies.remove(b)
            if b.right < 0:
                baddies.remove(b)
            if b.bottom < 0:
                baddies.remove(b)

        for bA in baddiesA:#Green baddies.
            bA.move_ip(random.gauss(0,4),random.gauss(0,4))

        #establish movement variables for any assigned character. (see playerRect)
        if event.type == KEYDOWN:
            if event.key== ord('a') or event.key==K_LEFT:
                moveRight = False
                moveLeft = True
            if event.key== ord('d') or event.key==K_RIGHT:
                moveLeft = False
                moveRight = True
            if event.key== ord('s') or event.key==K_DOWN:
                moveUp = False
                moveDown = True
            if event.key== ord('w') or event.key==K_UP:
                moveDown = False
                moveUp = True

        if event.type == KEYUP:
            if event.key== ord('a') or event.key==K_LEFT:
                moveLeft = False
            if event.key== ord('d') or event.key==K_RIGHT:
                moveRight = False
            if event.key== ord('s') or event.key==K_DOWN:
                moveDown = False
            if event.key== ord('w') or event.key==K_UP:
                moveUp = False

        #move the playerRect, while also establishing boundaries
        if moveLeft and playerRect.left > 0:
            playerRect.move_ip(-1*playerMoveSpeed, 0)#x,y move for playerRect
        if moveRight and playerRect.right < WINDOWWIDTH:#WINDOWWIDTH, bc it is the largest pixel number before leaving the screen.
            playerRect.move_ip(playerMoveSpeed,0)
        if moveDown and playerRect.bottom < WINDOWHEIGHT:
            playerRect.move_ip(0, playerMoveSpeed)
        if moveUp and playerRect.top > 0:
            playerRect.move_ip(0,-1*playerMoveSpeed)

        screen.fill(BLACK)

        pygame.draw.rect(screen,BLUE,playerRect)#draw the player

        for b in range(len(baddies)):
            pygame.draw.rect(screen,RED,baddies[b])#the appended objects then show up on the screen, starting with
                                                   #the original 2 from range(2)

        for bA in range(len(baddiesA)):
            pygame.draw.rect(screen,GREEN,baddiesA[bA])

        for x in range(len(ammoCrate)):
            pygame.draw.rect(screen,WHITE,ammoCrate[x])

        ammoCollide(playerRect,ammoCrate)

        mainClock.tick(40)
        pygame.display.update()

        #check for collision after iteration
        if collision(playerRect,baddies,baddiesA):
            break



    #out of the main game loop and in the outside loop

    pygame.quit()#quit the game if there is a break in the 2nd loop.
    sys.exit()

    pygame.display.update()
2
Contributors
2
Replies
19
Views
3 Years
Discussion Span
Last Post by a_9
0

You only are drawing them when the player collides with the ammocrate at line 30. Move the draw call out of the if statment eg line 189

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.