简体   繁体   English

无法弄清楚如何在我的pygame中成倍增加外星人?

[英]Can't figure out how to multiply aliens in my pygame?

So I've just been writing a dodger game for practice using pygame and python. 所以我刚刚用pygame和python编写了一个用于练习的道奇游戏。 I've read a lot of tutorials and looked at other similar games to help me. 我已经阅读了很多教程并看了其他类似的游戏来帮助我。 Basically, aliens come down from the top of the screen (they're supposed to be random sizes and speeds and their location is supposed to be random as well) and the player uses their mouse to move the spaceship to dodge the falling aliens. 基本上,外星人从屏幕的顶部下来(它们应该是随机的大小和速度,它们的位置也应该是随机的)并且玩家使用他们的鼠标移动飞船来躲避坠落的外星人。

I have an if statement that is supposed to multiply the aliens coming down from the top but when I run the game only one alien falls and when it reaches the end of the screen it just disappears. 我有一个if语句,它应该会使从顶部下来的外星人成倍增加,但是当我运行游戏时,只有一个外星人掉下来,当它到达屏幕的末端时它就会消失。 No other aliens appear. 没有其他外星人出现。 The program is still running so the game hasn't ended. 该程序仍在运行,因此游戏尚未结束。

Here is my full code. 这是我的完整代码。

import pygame
import random
import sys
from pygame.locals import *
alienimg = pygame.image.load('C:\\Python27\\alien.png')
playerimg = pygame.image.load('C:\\Python27\\spaceship.png')

def playerCollision(playerRect, aliens): # a function for when the player hits an alien
    for a in aliens:
        if playerRect.colliderect(a['rect']):
            return True
    return False

def screenText(text, font, screen, x, y): #text display function
    textobj = font.render(text, 1, (255, 255, 255))
    textrect = textobj.get_rect()
    textrect.topleft = (x,y)
    screen.blit(textobj, textrect)

def main(): #this is the main function that starts the game

    pygame.init()

    screen = pygame.display.set_mode((500,500))
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(False)
    pygame.display.set_caption('Dodge the Aliens') 
    font = pygame.font.SysFont("monospace", 45)


    pressed = pygame.key.get_pressed()

    aliens = []
    score = 0
    alienAdd = 0
    addedaliens = 0
    gameOver = False
    moveLeft = moveRight = moveUp = moveDown = False
    topScore = 0
    while gameOver==False: #while loop that actually runs the game
        score += 1

        playerImage =   pygame.image.load('C:\\Python27\\spaceship.png').convert() # the player images
        playerRect = playerImage.get_rect()
        screen.blit(playerImage, [300, 250])
        alienImage = pygame.image.load('C:\\Python27\\alien.png').convert() #alien images
        alienRect = alienImage.get_rect()
        screen.blit(alienImage, [ 50, 50 ])
        pygame.display.flip()


        for event in pygame.event.get(): #key controls
            if event.type == KEYDOWN and event.key == pygame.K_ESCAPE: #escape ends the game
                gameOver = True
            elif event.type == MOUSEMOTION:
                playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)
        screen.fill((0,0,0))
        pygame.display.flip()


        if not gameOver:
            alienAdd += 1
        if alienAdd == 6: # randomly adding aliens of different sizes and speeds
            aliendAdd = 0
            alienSize = random.randint(8, 25)
            newAlien = {'rect': pygame.Rect(random.randint(0, 500 - alienSize), 0 -alienSize, alienSize, alienSize),
                    'speed': random.randint(1, 8),
                    'surface':pygame.transform.scale(alienImage, (alienSize, alienSize)),
                    }
            aliens.append(newAlien)

        if moveLeft and playerRect.left > 0:
            playerRect.move_ip(-1 * 5, 0)
        if moveRight and playerRect.right < 500:
            playerRect.move_ip(5, 0)
        if moveUp and playerRect.top > 0:
            playerRect.move_ip(0, -1 * 5)
        if moveDown and playerRect.bottom < 500:
            playerRect.move_ip(0, 5)

        for a in aliens:
            if not gameOver:
                a['rect'].move_ip(0, a['speed'])


        for a in aliens[:]:
            if a['rect'].top > 500:
                aliens.remove(a) #removes the aliens when they get to the bottom of the screen

        screenText('Score %s' % (score), font, screen, 10, 0)
        screen.blit(playerImage, playerRect)

        pygame.display.update()

        for a in aliens:
            screen.blit(a['surface'], a['rect'])
        pygame.display.flip()


        if playerCollision(playerRect, aliens):
            if score > topScore:
                topScore = score
            gameOver = True


        clock.tick(30)

    screenText('Game Over!', font, screen, (750 / 6), ( 750 / 6))
    screenText('Press ENTER To Play Again.', font, screen, ( 750 / 6) - 80, (750 / 6) + 50)
    pygame.display.update()

if __name__ == "__main__":
    main()

Also, sidenote, whenever I put my code in here I go down every line and indent it. 另外,旁注,每当我把代码放在这里时,我都会沿着每一行向下并缩进它。 Is there an easier way to do that? 有更简单的方法吗?

You have a typo in aliendAdd = 0 , it should be alienAdd = 0 . 你在aliendAdd = 0aliendAdd = 0 ,应该是alienAdd = 0 This is causing you to keep incrementing alienAdd more and more and not hit your if statement of alienAdd == 6 again. 这导致你继续增加alienAdd越来越多,而不是再次命中你的alienAdd == 6 if语句。

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

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