【问题标题】:Enemies not spawning in没有生成的敌人
【发布时间】:2016-01-27 02:49:15
【问题描述】:
import pygame
import constants
import levels
import random, sys
from pygame.locals import *
from player import Player

BADDIEMINSIZE = 45
BADDIEMAXSIZE = 55
ADDNEWBADDIERATE = 13
BADDIEMINSPEED = 1
BADDIEMAXSPEED = 3
TEXTCOLOR = (255, 255, 255)

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                return

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

def playerHasHitBaddie(playerRect, baddies):
    for b in baddies:
        if playerRect.colliderect(b['rect']):
            return True
    return False

def terminate():
    pygame.quit()
    sys.exit()

def main():
    """ Main Program """
    pygame.init()
    pygame.mixer.music.load('background.mp3')

    # set up sounds
    smalljump = pygame.mixer.Sound('small_jump.ogg')
    startmenu = pygame.mixer.Sound('startmenu.ogg')

    # set up fonts
    font = pygame.font.SysFont(None, 48)

    #set up images
    baddieImage = pygame.image.load('baddie.png')

    # Set the height and width of the screen
    size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("OVER 9000 WHEN HE CHECK THE SCOUTER")

    # Create the player
    player = Player()

    # Create all the levels
    level_list = []
    level_list.append(levels.Level_01(player))

    # Set the current level
    current_level_no = 0
    current_level = level_list[current_level_no]
    active_sprite_list = pygame.sprite.Group()
    player.level = current_level
    player.rect.x = 340
    player.rect.y = constants.SCREEN_HEIGHT - player.rect.height
    active_sprite_list.add(player)

    #Loop until the user clicks the close button.
    done = False
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # show the "Start" screen
    drawText('THE INTERNET OF THINGS', font, screen, (constants.SCREEN_WIDTH / 3) - 40, (constants.SCREEN_HEIGHT / 3) + 50)
    drawText('Press a key to start.........', font, screen, (constants.SCREEN_WIDTH / 3), (constants.SCREEN_HEIGHT / 3) + 100)
    startmenu.play()
    pygame.display.update()
    waitForPlayerToPressKey()

    #GAME LOOP
    score = 0

    startmenu.stop()
    pygame.mixer.music.play(-1, 0.0)

    while not done:
        baddieAddCounter = 0
        baddies = []
        score += 1
        fact = ""

        baddieAddCounter += 1
        if baddieAddCounter == ADDNEWBADDIERATE:
            baddieAddCounter = 0
            baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE)
            newBaddie = {'rect': pygame.Rect(random.randint(0, SCREEN_WIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize),
                        'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
                        'surface':pygame.transform.scale(baddieImage, (baddieSize, baddieSize)),
                        }
            baddies.append(newBaddie)

        #GIVES PLAYER A NEW FACT EVERYTIME THEY REACH THE TARGET SCORE
        if score < 400:
           fact = 'NOTHING'

        elif score > 400 and score <= 799:
           fact = 'Physical objects having network connectivity'

        elif score > 800 and score <= 1100:
           fact = '1982 - First inter-connected appliance: Coke machine'


        drawText('Score: %s' % (score), font, screen, 10, 0)
        drawText('Fact: %s' % (fact), font, screen, 10, 40)
        pygame.display.update()

        for event in pygame.event.get():     
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.go_left()
                if event.key == pygame.K_RIGHT:
                    player.go_right()
                if event.key == pygame.K_SPACE:
                    smalljump.play()
                    player.jump()
                if event.key == pygame.K_UP:
                    smalljump.play()
                    player.jump()
                if event.key == K_ESCAPE:
                    terminate()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and player.change_x < 0:
                    player.stop()
                if event.key == pygame.K_RIGHT and player.change_x > 0:
                   player.stop()


        # Move the baddies down.
        for b in baddies:
            b['rect'].move_ip(0, b['speed'])

        # Draw each baddie
        for b in baddies:
            screen.blit(b['surface'], b['rect'])

        pygame.display.update()

        # Delete baddies that have fallen past the bottom.
        for b in baddies[:]:
            if b['rect'].top > WINDOW_HEIGHT:
                baddies.remove(b)
        pygame.display.update()

        pygame.display.update()                          
        active_sprite_list.update()
        current_level.update()

        #WHEN PLAYER GOES RIGHT, SHIFT WORLD LEFT
        if player.rect.right >= 500:
            diff = player.rect.right - 500
            player.rect.right = 500
            current_level.shift_world(-diff)

        #WHEN PLAYER FOES LEFT, SHIFT WORLD RIGHT
        if player.rect.left <= 120:
            diff = 120 - player.rect.left
            player.rect.left = 120
            current_level.shift_world(diff)

        # RESTRICTS PLAYER FROM GOING TO FAR TO THE RIGHT
        current_position = player.rect.x + current_level.world_shift
        if current_position < current_level.level_limit:
            player.rect.x = 120  

        current_level.draw(screen)
        active_sprite_list.draw(screen)

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

    pygame.quit()

if __name__ == "__main__":
    main()

由于某种原因,我的敌人(坏人)没有出现,我从另一个名为“dodger”的程序中获得了所有坏人代码,所以我不知道为什么它不起作用。当我在 dodger 中尝试它时它显然可以工作,但是当我把它放在我的代码中时没有任何反应。我的代码仍然可以运行,但是当你玩游戏时,没有坏人会像它应该的那样从天而降。此代码还有其他模块,但我没有添加它们。

【问题讨论】:

  • 一团糟。在循环中只使用一次pygame.dispkay.update()
  • current_level.draw() 在做什么?是画背景吗?然后这个背景隐藏了你的坏人。

标签: python python-2.7 pygame


【解决方案1】:

您的代码存在几个问题,但您当前的问题是您在主循环的每次迭代中都创建了一个新的坏人列表:

...
while not done:
    baddieAddCounter = 0
    baddies = []
    score += 1
    fact = ""
    ...

您应该将baddies = [] 移到while 循环之外。


您似乎已经使用精灵和组,所以我不知道您为什么要为您的坏人使用字典而不是精灵。

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多