为什么Python每次循环都不执行功能?

时间:2018-08-16 23:11:19

标签: python loops pygame

我已经编写了此脚本,该脚本创建了向左移动并停在某个点的灰色方块,并创建了无限地向右移动的红色方块。目前,每个方格只有1个。

在我看来(我是一个初学者),下面的脚本部分处于循环中,因此每次计算机经过循环时,都应创建一个新的灰色方块,直到总共有15个方块。为什么不呢?

(德国人是灰色方块)

if germancount<15:
    spawn_soldier(german_startx, german_starty, german_width, german_height, grey)

我的完整代码如下:

import pygame
import time
import random


pygame.init()

display_width = 1000
display_height= 800

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
grey=(169,169,169)

gameDisplay= pygame.display.set_mode((800,600))

pygame.display.set_caption('stalingrad')

clock = pygame.time.Clock()



def spawn_soldier(thingx,thingy, thingw, thingh, colour):
    pygame.draw.rect(gameDisplay, colour,[thingx, thingy, thingw, thingh])


def game_loop():

    russian_width= 20
    russian_height= 20
    russian_speed = 2
    russian_startx=-30
    russian_starty=random.randrange(0, display_height)

    german_width=20
    german_height=20
    german_speed=-1
    german_startx=780
    german_starty=random.randrange(0, display_height)

    germancount=0
    russiancount=0

    game_exit=False

    while not game_exit:
        gameDisplay.fill(white)



        if germancount<15:
            spawn_soldier(german_startx, german_starty, german_width, german_height, grey)

        if german_startx > 700:
            german_startx += german_speed

        if russiancount<100:
            spawn_soldier(russian_startx, russian_starty, russian_width, russian_height, red)


            russian_startx += russian_speed

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


game_loop()
pygame.quit()
quit()

编辑,我想我已经找到了一种更好地定义我的问题的方法。

我需要像德国人那样的15个“ spawn_soldier”功能。

            spawn_soldier_1(german_startx, german_starty, german_width, 
            spawn_soldier_2(german_startx, german_starty, german_width, 
            spawn_soldier_3(german_startx, german_starty, german_width,

是否有任何方法可以使它使用不同的y值来执行此函数的115个不同版本,而无需复制和粘贴115次?因为那简直就是一场噩梦。

1 个答案:

答案 0 :(得分:4)

每次循环时,您都会生成一个新士兵。实际上,因为您从不更改germancountrussiancount,所以您不仅要做15次,而且要永远做。每次,您都用白色覆盖所有现有的士兵,然后永久地产生一个新的德国人和一个新的俄国人(尽管最终他们不在屏幕边缘,所以您看不到它们)。

我认为您想要的是编写一个吸引士兵的函数:

def draw_soldier(rect, colour):
    pygame.draw.rect(gameDisplay, colour, rect)

然后,在帧循环内,像斯大林格勒的冬天一样,用白雪覆盖的屏幕擦除整个屏幕后,每次添加一个新矩形,然后重新绘制所有矩形:

# When the game starts, there are no soldiers
germans = []
russians = []

while not game_exit:
    gameDisplay.fill(white)

    # Each time through the loop, add another soldier to each
    # side until they're full
    if len(germans) < germancount:
        germans.append([german_startx, german_starty, german_width, german_height])
        german_startx += german_speed
    if len(russians) < russiancount:
        russians.append([russian_startx, russian_starty, russian_width, russian_height])
        russian_startx += russian_speed

    # Now draw all the soldiers in front of that white fill
    for german in germans:
        draw_soldier(german, grey)
    for russian in russians:
        draw_soldier(russian, red)

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