简体   繁体   中英

unable to effectively spawn sprites in a controlled manor in python 2.7 with pygame

My goal for this question is to be able to achieve an answer which explains how to make it so that when my character moves around to pick up the 'baddie(s)', the baddies will then automatically re-spawn onto the screen. Keeping total amount of sprites on the screen at any one time for the player to pick up at 40.

I am having problems being able to control the way the 'pikachu.jpg' sprites spawn. I am either able to spawn the sprites solely in a group fashion by entering an integer into my program manually or i write my attempt to test it wrong and the program upholds an infinite loop of drawing baddie sprites. I had set up a system which i believed should have coped with keeping the baddies at 40 sprites solid, though it did not work. I am not very good with python yet and i would really appreciate help with this issue.

import necessary pygame modules
import sys
import random
import time
import pygame
from pygame.locals import *
# initialise pygame
pygame.init()
# define window width and height variables for ease of access
WindowWidth = 600
WindowHeight = 500
global PLAY
PLAY = False
x = 600 / 3
y = 460 / 2 - 25

# load and define image sizes

enemyImage = pygame.image.load('pikachu.jpg')
enemyStretchedImage = pygame.transform.scale(enemyImage, (40,40))
screen = pygame.display.set_mode((WindowWidth, WindowHeight))


def play(PLAY):
    playerImage = pygame.image.load('player.jpg')
    playerStretchedImage = pygame.transform.scale(playerImage, (40, 40))
    movex,movey = 0,0
    charx, chary=300, 200
    enemyy, enemyx = 10, 10
    moveright = False
    moveleft = False
    spawn = True
    direction = 'down'
    baddie = []


    for i in range(20):
        baddie.append(pygame.Rect(random.randint(0, WindowWidth - 40), random.randint(0, 0), 40, 40))
    while True:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == ord('m'):
                    pygame.mixer.music.stop()
                    music = False
                if event.key == ord('n'):
                    pygame.mixer.music.play()
                    music = True
                if event.key == ord('a'):
                    moveleft = True

                if event.key == ord('d'):
                    moveright = True

                if event.key == ord('w'):
                    movey = -0.5
                if event.key == ord('s'):
                    movey = 0.5
                if event.key == ord('p'):
                    time.sleep(5)

            if event.type ==KEYUP:
                if event.key == ord('a'):
                    moveleft = False
                    if moveleft == False:
                        movex = 0
                if event.key == ord('d'):
                    moveright = False
                    if moveright == False:
                        movex = 0
                if event.key == ord('w'):
                    movey = 0
                if event.key == ord('s'):
                    movey = 0
            elif event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                pygame.quit()
                sys.exit()


        screen.fill(pygame.Color("red"))
        player = pygame.Rect(charx, chary, 40, 40)
        if direction == 'down':
            enemyy += 0.1
        if moveright ==True:
            movex = 0.5
        if moveleft ==True:
            movex = -0.5

        if player.bottom > WindowHeight:
            chary = WindowHeight - 40
            movey = 0
        if player.top < 0:
            chary = 1
            movey = 0
        if player.left < 0:
            charx = 1
            movex = 0
        if player.right > WindowWidth:
            charx = WindowWidth  - 40
            movex = 0

        for bad in baddie[:]:    #Here is where my attempt of testing was.
            if bad < 40:
                spawn = True
            if bad >= 40:
                spawn = False
            if spawn == True:
                baddie.append(pygame.Rect(random.randint(0, WindowWidth - 40), random.randint(0,0), 40, 40))



        screen.blit(playerStretchedImage, player)
        charx+=movex
        chary+=movey

        for bad in baddie[:]:
            if player.colliderect(bad):
                baddie.remove(bad)

        for bad in baddie:
            screen.blit(enemyStretchedImage, bad)
        pygame.display.update()

def presskey():
    myfont = pygame.font.SysFont("monospace", 30)
    label = myfont.render("press space to play!", 1, (255,125,60))
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    PLAY = True
                if PLAY == True:
                    play(PLAY)
                    return
        screen.fill(pygame.Color("cyan"))  
        screen.blit(label, (x,y))
        pygame.display.update()



presskey()

Here you're trying to fill up count of baddies up to 40, but the bad is a rectangle and you're using it as a number of buddies:

for bad in baddie[:]:    #Here is where my attempt of testing was.
            if bad < 40:
                spawn = True
            if bad >= 40:
                spawn = False
            if spawn == True:
                baddie.append(pygame.Rect(random.randint(0, WindowWidth - 40), random.randint(0,0), 40, 40))

I suggest you to replace those lines with:

if len(baddie)<40:
    baddie.append(pygame.Rect(random.randint(0, WindowWidth - 40), 

You can get the number of items in your [] list using len(baddie) . You should get a new baddie each game loop cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM