Reputation: 42
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.
Upvotes: 2
Views: 94
Reputation: 365807
Each time through the loop, you do spawn a new soldier. In fact, because you never change germancount
or russiancount
, you do this not just 15 times, but forever. Each time, you erase all the existing soldiers by covering them with white, then spawn a new German and a new Russian, forever (although eventually they're off the edge of the screen so you won't see them).
I think what you want is to write a function that draws soldiers:
def draw_soldier(rect, colour):
pygame.draw.rect(gameDisplay, colour, rect)
And then, inside your frame loop, after erasing the whole screen with a field of white snow like a winter in Stalingrad, add a new rectangle each time, and then draw all of the rectangles anew:
# 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)
Upvotes: 4