2

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.

5
  • 2
    Germans are grey squares? I think not. After all, many of them are quite hip, if indeed grey, so at worst greysupercoolpeople, not greysquares, they are. Commented Aug 16, 2018 at 23:15
  • Is it possible that it is creating like you want but every time the loop starts you are filling the gameDisplay with white , thus not see the others ? BTW, you are never increasing your germancount. Try to add a print statement inside spawn_soldier to debug. Commented Aug 16, 2018 at 23:16
  • Each time through the loop, you do spawn a new soldier. In fact, you do this forever. You erase all the existing soldiers by covering them with white snow, 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). Commented Aug 16, 2018 at 23:17
  • bugger. yeah, ive just realized. if i add to the count of the soldiers it just disappears after 15 frames. because its just redrawing it. this isnt going to work the way i want. im going to have to have a different line of code for each square but i want millions of different squares. any help? like could i make it iterate over my spawn soldier function somehow? Commented Aug 16, 2018 at 23:27
  • @tgmjack The trick to PyGame is that you have to think in terms of what you do every frame. If you paint the whole screen white every frame, you then have to draw everyone and everything before displaying the frame. Whatever else you do (like adding new soldiers that weren't there last time) is on top of that "draw everything" code. Commented Aug 17, 2018 at 0:17

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

2 Comments

im not sure what to do about this part # if len(germans) # i suppose i need to make a list 1 to 15 and then somehow assign a different german to each element in the list, but im struggling. sorry
@tgmjack I'm kind of guessing at what you were trying to do, but… the idea is that you create germans = [] and russians = [] at the start, before entering the main loop. Then, once per frame. you add another German and another Russian until you have enough of each. (And after that… the loop won't do anything useful, you'll just see them facing off against each other, motionless, until you get bored and quit the program. But then if you're simulating World War I, that's not a pretty realistic simulation…) I'll edit the answer to make that clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.