2

so I am working on a 2d game using pygame and PyopenGL, I have done some stuff to make it so the window is resizable. When the window resizes, the game resizes with it. This is all good.

My game is a tile based game with scrolling up and down.

However, the issue is, when I resize the screen, if I scroll up or down, strange lines appear between the tiles. This doesnt happen when scrolling left or right, which is odd. These lines only appear when at higher resolutions, on lower resolutions they do not exist. My code is rather large and in multiple files, so I will just post a link to it on github, but here is my window code with the resize function:

import numpy

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

from . import batch
from . import GAH


class window_handler:
    def __init__(self,window_size=(640,360)):
        self.window_size = window_size
        self.main_window = pygame.display.set_mode(self.window_size, DOUBLEBUF|OPENGL|RESIZABLE)
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glViewport(0, 0, self.window_size[0], self.window_size[1])
        
        self.projection_matrix = numpy.array((
                (2/640, 0, 0, 0),
                (0, -2/360, 0, 0),
                (0, 0, 1, 0),
                (0, 0, 0, 1)
            ), numpy.float32)

        self.view_matrix = numpy.array((
                (1, 0, 0, 0),
                (0, 1, 0, 0),
                (0, 0, 1, 0),
                (-320, -180, 0, 1)
            ), numpy.float32)

        self.view_projection_matrix = numpy.dot(self.view_matrix,self.projection_matrix)
        batch.setup_shaders(vpMatrix = self.view_projection_matrix)
        self.renderer = batch.Batch()

    def rezise_request(self, event):
        self.window_size = event.w,event.h
        glViewport(0, 0, self.window_size[0], self.window_size[1])
        
    def update_display(self):
        pygame.display.flip()

    def draw_game(self,draw):
        glClearColor(0, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT)

        self.renderer.begin()
        
        for i in draw["background_0"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_1"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_2"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])
        for i in draw["background_3"]:
            self.renderer.draw(self.graphics_handler["Background_images"][i[1]][0],i[0][0],i[0][1])

        for i in draw["tile_layer"]:
            self.renderer.draw(self.graphics_handler["Tileset"]["tileset_"+str(i[1][1])][i[1][0]],i[0][0],i[0][1])

        for i in draw["entity"].values():
            if i[1] != None:
                image = self.graphics_handler["Sprites"][i[1]][0]
                
                self.renderer.draw(image,i[0][0],i[0][1],i[2])
        self.renderer.end()

    def quit(self):
        self.renderer.delete()
        

        
        

Here is a a link to my code in full: github.com/EvelynMitchell199/Yumi

  • 1
    If the artefacts only appear at high resolutions, you may be seeing some rounding issues, so answers to [this question](https://stackoverflow.com/questions/46699651/how-to-avoid-floating-point-arithmetics-issues) might help. Are you able to create a [mcve]? Being unable to run your code makes it difficult to assist. – import random Sep 03 '20 at 06:14
  • 1
    Ah, my apologise. The reason I didnt want to post all my code is due to how much there is and how much it'd bloat the question, so I have gone with the best alternative. I have uploaded my code to github and it can be found here, I hope being able to run it helps (run main.py to run the game) https://github.com/EvelynMitchell199/Yumi – Evelyn Mitchell Sep 03 '20 at 06:52
  • 1
    @importrandom if you have any questions about the linked code just ask, there is quite a lot there so it can take a while to dig through – Evelyn Mitchell Sep 03 '20 at 07:04
  • 1
    @importrandom So, I looked into it a bit more and its definitely not a rounding error, and I also found that the lines ONLY show up on the window, and not when I print screen. I have been unable to make the lines show up on a print screen at all despite many attempts. Does this help at all? – Evelyn Mitchell Sep 03 '20 at 13:26
  • 1
    Is the github link correct? I ran the game. It's a platform game with jumps, but no scrolling. – Mike67 Sep 03 '20 at 14:39
  • 1
    @Mike67 if you walk off the screen to the right, the camera moves to another area. In this other area the screen can scroll up and down, – Evelyn Mitchell Sep 03 '20 at 14:43
  • though you need to reach a relatively high up hill for the scrolling to start, probably should have noted that @Mike67 – Evelyn Mitchell Sep 03 '20 at 15:17

1 Answers1

1

I figured out whats wrong with our code! So, basically when we set resolutions, if the resolution isn't one of the ones highlighted in green here, then everything scales weirdly creating lines between tiles. https://pacoup.com/2011/06/12/list-of-true-169-resolutions/

The resolution MUST be divisible by 8 otherwise the scaling between tiles will be different, and thus we can get lines between tiles.

Evelyn
  • 192
  • 7