0
screen = pygame.display.set_mode((WIDTH,HEIGHT))
canvas = pygame.Surface((WIDTH,HEIGHT))
def makefunnytiles(saice):
    global WIDTH
    global HEIGHT
    global screen
    global roomdata
    global tiledefinitions
    print("Baking Tiles")
    print(roomdata)
    index = 0
    index2 = 0
    for symbolic in saice:
        index2 = 0
        symbolic = symbolic.strip()
        print("sanity")
        for symbol in list(symbolic):
            print(symbol)
            if symbol in tiledefinitions:
                canvas.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2),(32 * index)))
            index2 += 1
        index += 1
    screen.blit(canvas, (0,0))
    pygame.display.flip()
    print("drew screen")
    print(str(canvas.get_width))

I'm having a problem with this where for some reason the canvas cuts off at the middle of the screen.

Image of cut off

Another problem I'm having is that pgzrun.go() at the end of the file, which crashes the program with this error:

Traceback (most recent call last):
  File "C:\Users\RushVisor\Documents\Glamore\main.py", line 100, in <module>
    pgzrun.go()
  File "E:\Python39\lib\site-packages\pgzrun.py", line 31, in go
    run_mod(mod)
  File "E:\Python39\lib\site-packages\pgzero\runner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 217, in run
    self.mainloop()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 225, in mainloop
    self.reinit_screen()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 73, in reinit_screen
    self.mod.screen.surface = self.screen
AttributeError: 'pygame.Surface' object has no attribute 'surface'

I've tried modifying the resolution values for both canvas and screen and even the sprite's positions themselves. I am blitting the canvas to the screen rather than drawing to the screen directly because if I'm right it should allow me to add scrolling easier.

I appreciate any help anyone can give me.

EDIT: Code is here https://paste.pythondiscord.com/tetogequze.py

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Rush Fox
  • 1
  • 1
  • 1
    Why have a separate `canvas` surface? Just blit to `screen`? – Iain Shelvington May 24 '21 at 03:29
  • 1
    Why are you loading the images every time you blit them to the canvas? Load the images separately to a list or dictionary and blit them that way, otherwise you are loading them every time which is not the correct way. We cannot help with the other issue as we cannot see your main.py file. – marienbad May 24 '21 at 03:47
  • What is the size of the tile bitmaps (pngs)? – Rabbid76 May 24 '21 at 06:09
  • 32 pixels also sorry heres the code. https://paste.pythondiscord.com/tetogequze.py – Rush Fox May 24 '21 at 16:47

1 Answers1

0

The Error

self.mod.screen.surface = self.screen

The variable self.mod.screen seams to be a an object of the class pygame.Surface. Objects of this class do not have a variable called surface, thats were the error AttributeError: 'pygame.Surface' object has no attribute 'surface' seems to come from.

This should solve the problem, if you do not want to implement an extra class with a surface attribute: self.mod.screen = self.screen. But do not forget that this does not copy the surface self.screen, if you want that you need something like this: self.mod.screen = self.screen.copy().

Scrolling

Your idea is one possible way to implement scrolling. Another one this that your store some kind of origin and blit the tiles based one that. If you now want to scroll, you just need to change this origin you defined before.

Definition of origin

origin = [10,10]  # or use pygame.math.Vector2 or something else

Blit code

index = 0
index2 = 0
for symbolic in saice:
    index2 = 0
    symbolic = symbolic.strip()
    print("sanity")
    for symbol in list(symbolic):
        print(symbol)
        if symbol in tiledefinitions:
            screen.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2)+origin[0],(32 * index)+origin[1]))
        index2 += 1
    index += 1

Now you just need to call the blit code once every frame. If you now want to scroll, just do something like this: origin[0] += 10 (to move all the symbols to the right)

The above blit code is not the fastest, as it needs to load all images on every frame, but this is only a example how scrolling can work with this other idea

EDIT: I am not very familar with Pygame-Zero, so this is just based on what I know about standart Pygame.

Twistios_Player
  • 120
  • 3
  • 11