I've written this script which creates grey squares which move left and stop at a point, and creates red squares which go right indefinitely. Currently it only makes 1 of each square.
To my mind (I'm a begginer) the part of the script below is in a loop so each time the computer goes through the loop it should create a new grey square until there are a total of 15 squares. Why doesn't it?
(btw germans are grey squares)
if germancount<15:
spawn_soldier(german_startx, german_starty, german_width, german_height, grey)
My full code is below:
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()
edit, i suppose ive worked out a way of defining my problem a bit better.
i need like 15 of these "spawn_soldier" functions just for the Germans.
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,
is there any way to get it to do 115 different versions of this function with different values for y without copy and pasting 115 times? because thats just going to be a nightmare.
gameDisplaywith white , thus not see the others ? BTW, you are never increasing yourgermancount.Try to add aprintstatement insidespawn_soldierto debug.