0

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")
2
  • it blits the first one or nothing: so when you run it multiple times sometimes it renders a planet at the first coordinate and other times it doesn't? By the way, why are you defining position = ()? It may not resolve the problem, but you can remove that. Commented Jul 30, 2016 at 14:51
  • yes few times it renderd planet to the first coordinates Commented Jul 30, 2016 at 14:56

2 Answers 2

2

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

1 Comment

Ok this works perfectly! I try to be more consisten with my code in future. And idea of using the boolean varible sound's good ill look into that.
0

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

Comments

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.