python - 为什么Python每次循环时不执行一个函数?

标签 python loops pygame

我编写了这个脚本,它创建向左移动并停在某个点的灰色方 block ,并创建无限向右移动的红色方 block 。目前每个方 block 只能制作 1 个。

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

(顺便说一句,德国人是灰色方 block )

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 次?因为那将是一场噩梦。

最佳答案

每次循环时,您都会生成一名新士兵。事实上,因为您从不更改 germancount Russiancount,所以您不仅会这样做 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)

关于python - 为什么Python每次循环时不执行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886457/

相关文章:

php - 使用 PHP while 循环更改表格单元格背景颜色

Python 游戏网络

python - 如何获取 pygame 中鼠标点击位置的 RGB 值以及如何更改颜色? (Python 3.3.2)

python - 如何创建 pygame 保存文件

python - 正确使用range(),为什么pylint报错: range built-in referenced when not iterating

python - 从包中安装 Python,终端没有更新?

python - 加速循环以使用另一个数组中最接近的值填充数组

c++ - 在这种情况下如何正确释放内存

python - pyparsing scanString 带有空格无法解析

python - 我可以在描述符的 __init__ 方法期间获得对 'owner' 类的引用吗?