简体   繁体   English

无法使用pygame在python 2.7中的受控庄园中有效产生精灵

[英]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. 随时将屏幕上的精灵总数保持在屏幕上,以供玩家在40点时拿起。

I am having problems being able to control the way the 'pikachu.jpg' sprites spawn. 我在控制'pikachu.jpg'精灵生成的方式时遇到问题。 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. 我建立了一个系统,我认为该系统应该可以使40个精灵的坏人保持稳定,尽管它无法正常工作。 I am not very good with python yet and i would really appreciate help with this issue. 我对python的了解还不是很好,我非常感谢您提供有关此问题的帮助。

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: 在这里,您尝试填充最多40个坏人,但bad是一个矩形,并且将它用作许多伙伴:

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) . 您可以使用len(baddie)获得[]列表中的项目数。 You should get a new baddie each game loop cycle. 您应该在每个游戏循环周期获得一个新的Baddie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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