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.
- 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}'