Jigsaw
Jigsaw

Reputation: 320

Why is my pygame screen only displaying a black image?

For some reason even though I have defined everything in the level and level editor I don't get a response. What I really want to do is to display the map on my pygame screen. I am using cv2, sys, cv2_imread, pygame and pygame_locals libraries. No error message is appearing so I did not find any answers on the internet. It also seems to be a problem with the opencv side of code.

Code:

pygame.init()

screen = pygame.display.set_mode((226, 318))
player_pos = [95, 292]

player = pygame.image.load('Player.png').convert()
player.set_colorkey((255, 255, 255))

level1 = list(imread('Map1.png'))  # the​ image

moving_right = False
moving_left = False
moving_up = False
moving_down = False


clock = pygame.time.Clock()
while True:
    screen.fill((15, 15, 15))

    player_rect = screen.blit(player, player_pos)

    if moving_right == True:
        player_pos[0] += 2
    if moving_left == True:
        player_pos[0] -= 2
    if moving_up == True:
        player_pos[1] -= 2
    if moving_down == True:
        player_pos[1] += 2

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                moving_up = True
            if event.key == K_DOWN:
                moving_down = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                player_pos[0] += 24
                moving_right = False
            if event.key == K_LEFT:
                player_pos[0] -= 24
                moving_left = False
            if event.key == K_UP:
                player_pos[1] -= 24
                moving_up = False
            if event.key == K_DOWN:
                player_pos[1] += 24
                moving_down = False

    if player_pos[1] > 290:
        moving_down = False #This will be changed later so you die
    if player_pos[1] < 4:
        moving_up = False
    if player_pos[0] < 4:
        moving_left = False
    if player_pos[0] > 212:
        moving_right = False

    a = screen.get_width()/32
    b = screen.get_height()/24

    for i in range(len(level1)):
        for j in range(len(level1[i])):
            blue = list(level1[i][j])[0]
            green = list(level1[i][j])[1]
            red = list(level1[i][j])[2]
            if blue == 255 and green == 255 and red == 255:
                pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(j*a, i *b, 32, 32))
            else:
                pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(j * a, i * b, 32, 32))

    pygame.display.update()
    clock.tick(120)
    print(clock.get_fps())

pygame.quit()

Player.png:

enter image description here

Map1.png:

2

Upvotes: 1

Views: 796

Answers (1)

m.i.cosacak
m.i.cosacak

Reputation: 730

I cannot solve the whole problem, but one of the main reason you get black screen is because you multiply j*a and b*i.

For instance, if you try:

if blue == 255 and green == 255 and red == 255:
    pygame.draw.rect(screen, (255,255,255), pygame.Rect(j, i, 32, 32))
else:
    pygame.draw.rect(screen, (0,255,0, pygame.Rect(j, i, 32, 32))

and if you set coordinate to, let's say 100 you will see the player as well:

if blue == 255 and green == 255 and red == 255:
    pygame.draw.rect(screen, (255,255,255), pygame.Rect(100, 100, 32, 32))
else:
    pygame.draw.rect(screen, (0,255,0, pygame.Rect(100, 100, 32, 32))

as a result, please double check pygame.Rect(j*a, b*i, 32, 32). I do not know exactly what you want to do. But I think this will help you.

the full code for j, i;

import sys
import pygame
from pygame import *
import cv2

pygame.init()

screen = pygame.display.set_mode((226, 318))
player_pos = [95, 292]

player = pygame.image.load('Player.png').convert()
player.set_colorkey((255, 255, 255))

level1 = list(cv2.imread('Map1.png'))  # the​ image

moving_right = False
moving_left = False
moving_up = False
moving_down = False

clock = pygame.time.Clock()
while True:
    screen.fill((15, 15, 15))
    player_rect = screen.blit(player, player_pos)
    if moving_right == True:
        player_pos[0] += 2
    if moving_left == True:
        player_pos[0] -= 2
    if moving_up == True:
        player_pos[1] -= 2
    if moving_down == True:
        player_pos[1] += 2
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                moving_up = True
            if event.key == K_DOWN:
                moving_down = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                player_pos[0] += 24
                moving_right = False
            if event.key == K_LEFT:
                player_pos[0] -= 24
                moving_left = False
            if event.key == K_UP:
                player_pos[1] -= 24
                moving_up = False
            if event.key == K_DOWN:
                player_pos[1] += 24
                moving_down = False
    if player_pos[1] > 290:
        moving_down = False #This will be changed later so you die
    if player_pos[1] < 4:
        moving_up = False
    if player_pos[0] < 4:
        moving_left = False
    if player_pos[0] > 212:
        moving_right = False
    a = screen.get_width()/32
    b = screen.get_height()/24
    print(a, b)
    for i in range(len(level1)):
        for j in range(len(level1[i])):
            blue = list(level1[i][j])[0]
            green = list(level1[i][j])[1]
            red = list(level1[i][j])[2]
            if blue == 255 and green == 255 and red == 255:
                pygame.draw.rect(screen, (255,255,255), pygame.Rect(j, i, 32, 32))
            else:
                pygame.draw.rect(screen, (0,255,0), pygame.Rect(j, i, 32, 32))
    pygame.display.update()
    clock.tick(120)
    print(clock.get_fps())

pygame.quit()

the full code for j = 100, i = 100;

import sys
import pygame
from pygame import *
import cv2

pygame.init()

screen = pygame.display.set_mode((226, 318))
player_pos = [95, 292]

player = pygame.image.load('Player.png').convert()
player.set_colorkey((255, 255, 255))

level1 = list(cv2.imread('Map1.png'))  # the​ image

moving_right = False
moving_left = False
moving_up = False
moving_down = False

clock = pygame.time.Clock()
while True:
    screen.fill((15, 15, 15))
    player_rect = screen.blit(player, player_pos)
    if moving_right == True:
        player_pos[0] += 2
    if moving_left == True:
        player_pos[0] -= 2
    if moving_up == True:
        player_pos[1] -= 2
    if moving_down == True:
        player_pos[1] += 2
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                moving_up = True
            if event.key == K_DOWN:
                moving_down = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                player_pos[0] += 24
                moving_right = False
            if event.key == K_LEFT:
                player_pos[0] -= 24
                moving_left = False
            if event.key == K_UP:
                player_pos[1] -= 24
                moving_up = False
            if event.key == K_DOWN:
                player_pos[1] += 24
                moving_down = False
    if player_pos[1] > 290:
        moving_down = False #This will be changed later so you die
    if player_pos[1] < 4:
        moving_up = False
    if player_pos[0] < 4:
        moving_left = False
    if player_pos[0] > 212:
        moving_right = False
    a = screen.get_width()/32
    b = screen.get_height()/24
    print(a, b)
    for i in range(len(level1)):
        for j in range(len(level1[i])):
            blue = list(level1[i][j])[0]
            green = list(level1[i][j])[1]
            red = list(level1[i][j])[2]
            if blue == 255 and green == 255 and red == 255:
                pygame.draw.rect(screen, (255,255,255), pygame.Rect(100, 100, 32, 32))
            else:
                pygame.draw.rect(screen, (0,255,0), pygame.Rect(100, 100, 32, 32))
    pygame.display.update()
    clock.tick(120)
    print(clock.get_fps())

pygame.quit()

Upvotes: 1

Related Questions