cmdtvt
cmdtvt

Reputation: 71

Python /Pygame blitting multiple image to list coordinates

So i'm making a space game where you have a open world to explore (A black Screen) and i wanna create planets(Blit images to screen) around the universe(from a list).

This is my code currently

star = pygame.image.load("star.png")

planets = random.randrange(100,500)  #<------#####NOT IMPORTANT#######
positions = [(300,50),(400,27),(900,55)] #<------
position = ()

positions has coorinates where images should be blitted but when i blit them

    for position in positions:
        ikkuna.blit(star, position)

it blits the first one or nothing and does not crash why?

################################################-

Here is the full code if it helps (there is bits of the finish language in there hope it does not bother)
"ikkuna = screen (leveys,korkeus) = (width,hight) toiminnassa = in action"

import pygame
import random
import time
import sys
import math

pygame.init()
White = (255,255,255)
red = (255,0,0)

kello = pygame.time.Clock()
star = pygame.image.load("star.png")

planets = random.randrange(100,500)
positions = [(300,50)]
position = ()

tausta_vari = (255,255,255)
(leveys, korkeus) = (1000, 1000)
ikkuna = pygame.display.set_mode((leveys, korkeus))
pygame.display.set_caption("SpaceGenerationTest")
######################################################################
toiminnassa = True
while toiminnassa:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            toiminnassa = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_g:
                print("Generating World....")
                print(planets)


    for position in positions:
        ikkuna.blit(star, position)
        print("hello")

Upvotes: 0

Views: 3135

Answers (2)

Ted Klein Bergman
Ted Klein Bergman

Reputation: 9746

There's some problems with your code:

  1. Your not updating the display, which means that all your changes won't be visible. Use pygame.display.update() or pygame.display.flip() at the end of the game loop to update the screen.
  2. When you're mixing English and Finnish it becomes very inconsistent and hard to follow, especially for people who don't speak Finnish. Try to use English, even when you're just practicing.
  3. Your full example only contain one position in the list, thus it's only creating one star at one position.
  4. Pressing the 'g'-key won't generate any planets. You might want to introduce a boolean variable like in the example below.

I changed your code to english and made some adjustment so it's more consistent.

import pygame
import random

pygame.init()

WHITE = (255, 255, 255)
RED = (255, 0, 0)

# I changed the background color to black, because I understood it as that is what you want.
BACKGROUND_COLOR = (0, 0, 0)  # tausta_vari

clock = pygame.time.Clock()  # kello
star = pygame.Surface((32, 32))
star.fill((255, 255, 255))

planets = random.randrange(100, 500)
positions = [(300, 50), (400, 27), (900, 55)]

WIDTH, HEIGHT = (1000, 1000)  # (leveys, korkeus)
screen = pygame.display.set_mode((WIDTH, HEIGHT))  # ikkuna
pygame.display.set_caption("SpaceGenerationTest")

display_stars = False
running = True  # toiminnassa
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_g:
                print("Generating World....")
                print(planets)
                display_stars = True

    screen.fill(BACKGROUND_COLOR)

    if display_stars:
        for position in positions:
            # This will create 3 stars because there're 3 elements in the list positions.
            # To create more stars you'll need to add more in the list positions.
            screen.blit(star, position)

    pygame.display.update()

Upvotes: 2

Chris
Chris

Reputation: 22953

Your blitting all your images to the exact same position: positions = [(300, 50)], so the last image covers all the other images up. Also, you may not know this, but to display anything in pygame you have to either call pygame.display.flip() or pygame.display.update() after you finish drawing. I made a few revisions to your code, so the stars should show-up:

import pygame
import random
import time
import sys
import math

def main():
    pygame.init()
    White = (255,255,255)
    red = (255,0,0)

    kello = pygame.time.Clock()
    star = pygame.image.load("star.png")

    planets = random.randrange(100,500)
    positions = [(300,50), (310, 60), (320, 80), (607, 451), (345, 231)]

    tausta_vari = (255,255,255)
    (leveys, korkeus) = (1000, 1000)
    ikkuna = pygame.display.set_mode((leveys, korkeus))
    pygame.display.set_caption("SpaceGenerationTest")
    ######################################################################
    toiminnassa = True
    while toiminnassa:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                toiminnassa = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_g:
                    print("Generating World....")
                    print(planets)

        ikkuna.fill((0, 0, 0)) # fill the screen with black
        for position in positions:
            ikkuna.blit(star, position)
            print("Hello")

        pygame.display.flip() # updating the screen

if  __name__ == '__main__': # are we running this .py file as the main program?          
    try:
        main()
    finally:
        pg.quit()
        quit()

~Mr.Python

Upvotes: 0

Related Questions