1

I am trying to complete a a python course (beginner) and I'm trying to make a game using pgzero. The problem I am encountering is that the background is not loading correctly i.e. the size of the window and the background are not matching and part of it is cut off. The strangest part is that whilst messing around with the code to try and figure it out I have put the code pgzrun.go() twice at the bottom (by mistake. When I run the code the screen pops up with the issue (1st screen shot), i then close the window and a second screen pops up (2nd screen shot) and this time its working and the background fits with the screen giving me the desired outcome. I have played around with the variable to change the width, height, size etc but the only thing that has worked (which is not ideal) is having the 2 pgzrun.go() at the end.

enter image description here

  • CODE:
import pgzrun
import pygame
# from pgzero.game import screen

GRID_WIDTH = 16
GRID_HEIGHT = 12
GRID_SIZE = 50

WIDTH = GRID_WIDTH * GRID_SIZE
HEIGHT = GRID_HEIGHT * GRID_SIZE
MAP = ["WWWWWWWWWWWWWWWW",
       "W              W",
       "W              W",
       "W  W  KG       W",
       "W  wwwwwwwwww  W",
       "W              W",
       "W      P       W",
       "W  wwwwwwwwww  W",
       "W     GK    W  W",
       "W              W",
       "W              D",
       "WWWWWWWWWWWWWWWW"
       ]


def screen_coords(x, y):
    return x * GRID_SIZE, y * GRID_SIZE


def draw_background():
    for y in range(GRID_HEIGHT):
        for x in range(GRID_WIDTH):
            screen.blit("floor1", screen_coords(x, y))


def draw_scenery():
    for y in range(GRID_HEIGHT):
        for x in range(GRID_WIDTH):
            square = MAP[y][x]
            if square == "W":
                screen.blit("wall", screen_coords(x, y))
            elif square == "D":
                screen.blit("door", screen_coords(x, y))


def draw():
    # screen.clear()
    draw_background()

    draw_scenery()



pgzrun.go()




pgzrun.go()

can any one figure out what's going on. TIA

ANSWER FOUND: The window screen was opening off centre and due to the size of the window part of it was opening outside the screen and this caused some kind of glitch? By adding the below code above the import statements meant I was able to open the window with co ordinates (50,50) allowing the whole screen to fit and it rendered perfectly.

x = 50
y = 50
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = f'{x},{y}'

Expected result:enter image description here

  • Please post all errors and expected result INSIDE the post, not as links. Report all textual error messages as text, not images – Caridorc Feb 10 '23 at 14:13
  • Which IDE (advanced text editor) are you using? it could be interfering with pgzero – Caridorc Feb 10 '23 at 14:53
  • Currently using it in "Pycharm Community Edition 2021.2.1". Would Visual Studio Code be a better choice. – LekkerCoder Feb 10 '23 at 15:01
  • try running from command line with `python filename.py` – Caridorc Feb 10 '23 at 15:11
  • So I have tried it in the command line as well as Visual Studio Code and the same thing is happening in both cases? First window pops up with the issue and then I close it and immediately the second window pops up with the correct result. Cant understand why the second run of pgzrun.go() fixes it? – LekkerCoder Feb 10 '23 at 15:20
  • don't know sadly, never used this library – Caridorc Feb 10 '23 at 15:21
  • 1
    Thanks for trying (& and advice) . I'll keep going with the game and see if it sorts itself out by the end (once a functional game) but it is strange that it needs two runs of the pgzrun.go() to work? – LekkerCoder Feb 10 '23 at 15:32
  • 1
    @Caridorc. Solved it. The issue was that the window screen was opening to the right of centre. What was happening is the window was therefore cut off and this caused some kind of glitch? which meant it wasnt loading the background/images etc correctly. After some googling i came across something about adding system variables(??) and got the window to open in the centre which solved the issue – LekkerCoder Feb 10 '23 at 15:47
  • 1
    perfect, please post it as an answer for future visitors – Caridorc Feb 10 '23 at 15:52

1 Answers1

0

The issue was that the window screen was opening to the right of centre. What was happening is the window was therefore cut off and this caused some kind of glitch? which meant it wasn't loading the background/images etc correctly. After some googling I came across something about adding system variables(??) and got the window to open in the centre which solved the issue.

Amendment made to question to show solution