1

Why doesn't my map appear on my pygame display? I have used the number system for a map but still no result.

Code

Don't need to read the notes, they are for myself:

import sys
import pygame
from pygame.locals import *

clock = pygame.time.Clock()  

pygame.init() 
pygame.display.set_caption("Kemal AKsay") 

WINDOW_SIZE = (600, 400)  

ekran = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
display = pygame.Surface((300, 200))
oyuncu_görüntüsü = pygame.image.load("tt.png")
çimen_görüntüsü = pygame.image.load('çimen.png')
toprak_görüntüsü = pygame.image.load("toprak.png")

TILE_SIZE = çimen_görüntüsü.get_width()

game_map = [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '0', '0', '2', '2', '2', '2', '2', '0', '0', '0', '0', '0', '0', '0'],
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['2', '2', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2'],
            ['1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']]

sağa_ileri = False
sola_ileri = False

oyuncu_konumu = [50, 50]  
oyuncu_y_hızı = 0

player_rect = pygame.Rect(oyuncu_konumu[0], oyuncu_konumu[1], oyuncu_görüntüsü.get_width(), oyuncu_görüntüsü.get_height())  

while True:  
    ekran.fill((146, 244, 255))  # bu arkaplan o 3 sayı rgb

    tile_rects = []
    y = 0
    for row in game_map:
        x = 0
        for tile in row:
            if tile == '1':
                display.blit(toprak_görüntüsü, (x * TILE_SIZE, y * TILE_SIZE))
            if tile == "2":
                display.blit(çimen_görüntüsü, (x * TILE_SIZE, y * TILE_SIZE))
            if tile != "0":
                tile_rects.append(pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE , TILE_SIZE))
            x += 1
        y += 1

    ekran.blit(oyuncu_görüntüsü, oyuncu_konumu,)  

    oyuncu_y_hızı += 0.2
    oyuncu_konumu[1] += oyuncu_y_hızı

    if sağa_ileri:
        oyuncu_konumu[0] += 1
    if sola_ileri:
        oyuncu_konumu[0] -= 1

    oyuncu_karesi = pygame.Rect(oyuncu_konumu[0], oyuncu_konumu[1], oyuncu_görüntüsü.get_width(), oyuncu_görüntüsü.get_height())

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                sağa_ileri = True
            if event.key == K_LEFT:
                sola_ileri = True

        if event.type == KEYUP:
            if event.key == K_RIGHT:
                sağa_ileri = False
            if event.key == K_LEFT:
                sola_ileri = False
            if event.key == K_DOWN:
                alta_ileri = False
            if event.key == K_UP:
                üste_ileri = False

    pygame.display.update()  
    clock.tick(60)  
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
pitrome
  • 11
  • 2
  • It's hard to read your code because it is not English. I suppose it's Turkish?! __Please explain__ and [edit] your question. You should clarify: (a) What does the given code? (b) What do you expect to be shown? – hc_dev Jul 28 '21 at 16:11
  • As an _Image is worth a thousand words_ (Bir Resim, Bin Kelimeye Bedeldir) it would help if you could upload and __paste your tiles__ (e.g. see [Formatting: Images](https://stackoverflow.com/help/formatting) using Imgur). – hc_dev Jul 28 '21 at 16:39
  • Still there are other issues making the question not ready to answer: (a) not reproducible without tiles, (b) game-objects (player and background tiles) moving out of the Surface, (c) background Surface `display` is black when blended. I want to point out issues that would help others, trying understand this specific Q&A journey. – hc_dev Jul 28 '21 at 19:15

3 Answers3

1

The Surface wich is associated to the screen is ekran instead of display. Since you draw the tiles on display, the are not visible on the screen.

Create the display Surface before the application loop and blit it on the ekran Surface in the application loop:

# [...]

tile_rects = []
for y, row in enumerate(game_map):
    for x, tile in enumerate(row):
        if tile == '1':
            display.blit(toprak_görüntüsü, (x * TILE_SIZE, y * TILE_SIZE))
        if tile == "2":
            display.blit(çimen_görüntüsü, (x * TILE_SIZE, y * TILE_SIZE))
        if tile != "0":
            tile_rects.append(pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE , TILE_SIZE))

while True:  
    ekran.fill((146, 244, 255))  # bu arkaplan o 3 sayı rgb
    ekran.blit(display, (0, 0))
    ekran.blit(oyuncu_görüntüsü, oyuncu_konumu)

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

It shows a tile moving down

When I reproduced with following images:

  1. blue as placeholder for "tt.png",
  2. red as placeholder for 'çimen.png'
  3. green as placeholder for "toprak.png"

I saw the blue tile tt.png entering on top, moving vertically down, and falling down at the end of the screen. With the right & left arrow-keys I could move it a bit to the right or left.

pygame showing a tile falling down at the end of the screen

After the blue tile disappeared as fallen through the window, the game-loop continued adding tiles (even different ones) with blit to the Surface display.

Issue: 2 Surfaces, one shown, the other with tiles

There are two screens (Turkish "ekran"), in PyGame we call it Surface, you are using:

  • ekran as shown Surface
  • display as hidden Surface

See your code given:

// initially
ekran = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
display = pygame.Surface((300, 200))

// in the game-loop
while True:
    ekran.fill((146, 244, 255))  # bu arkaplan o 3 sayı rgb
    // here according to map the tiles are added to display 
    ekran.blit(oyuncu_görüntüsü, oyuncu_konumu, )

Different usages

This ekran as active Surface is shown in the open window. On the otherhand display is currently an inactive Surface. But there all tiles are moved from the map. It is never shown.

Read the docs for showing Surfaces

In the official PyGame docs there is an example for opening a window on the screen:

# Open a window on the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width, screen_height])

Might be a bit confusing:

  • the return value of set_mode is of type Surface.
  • the return value of set_mode is shown in the open window.

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38
0

The variable display does not reference the display, but references a surface in memory. This surface is never drawn to the actual display. If you replace display.blit by ekran.blit the output should appear on screen.

H. Rittich
  • 814
  • 7
  • 15