Why does my window size change after loading my UI?

I’m trying to make a kind of game like Google dinosaur. The problem is that when I start my application, the height of my window changes a few seconds later, so when I try to place the “dinosaur” at the bottom it remains floating, because the height of the window changed.

The “dinosaur” is the box that is floating:

The "dinosaur" is the box that is floating

This is the main window

import sys 
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QFrame
from PyQt5.QtCore import Qt, QTimer, QRect
from PyQt5.QtGui import QPen, QBrush, QPainter
from PyQt5 import uic
import random

class animation(QWidget):

    def __init__(self):
        super().__init__()
        
        self.shapes = []
        self.dimension = 50

        #hilo del juego
        self.game_timer = QTimer(self)
        self.game_timer.timeout.connect(self.animation)
        self.game_timer.start(10)


        #crear las figuras
        self.shapes_generator_timer = QTimer(self)
        self.shapes_generator_timer.timeout.connect(self.new_shape)
        self.shapes_generator_timer.start(500)

        #jugador
        coord_x = self.width()
        coord_y = self.height() - self.dimension
        
        self.player = QRect(200, coord_y-2, self.dimension, self.dimension)

        

        
    def new_shape(self):
        coord_x = self.width()
        coord_y = self.height() - self.dimension
        
        rect = QRect(coord_x, coord_y-2, self.dimension, self.dimension)
        
        self.shapes.append(rect)

    

    def animation(self):
        for index,shape in enumerate(self.shapes):
            shape.moveLeft(shape.left() - 5)

            self.delete_shape(index,shape)
        self.update()
        print(self.height())

    def delete_shape(self, index,shape):

        if shape.getCoords()[2] < 0:
            del self.shapes[index]
            print(len(self.shapes))
        


    def paintEvent(self, event):
        
        painter = QPainter(self)
        painter.setPen(QPen(Qt.white,0,Qt.SolidLine))
        painter.setBrush(QBrush(Qt.white,Qt.SolidPattern))
        painter.drawRect(self.rect())


        painter.setPen(QPen(Qt.black,1,Qt.SolidLine))
        painter.setBrush(QBrush(Qt.green,Qt.SolidPattern))
        for shape in self.shapes:
            painter.drawRect(shape)

        painter.drawRect(self.player)

    


ui, _ = uic.loadUiType("PYQT5interface-animation.ui")

class MyApp(QWidget,ui):

    def __init__(self):
        super().__init__()

        self.setupUi(self)
        
        

        self.animation = animation()
        frame = self.findChild(QFrame, "frame")

        layout = QHBoxLayout()
        layout.addWidget(self.animation)

        frame.setLayout(layout)
    

app = QApplication(sys.argv)
myApp = MyApp()
myApp.show()
sys.exit(app.exec_())

And this is my UI file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1201</width>
    <height>638</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QFrame" name="frame">
     <property name="styleSheet">
      <string notr="true">background-color:rgb(0,0,0);</string>
     </property>
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Raised</enum>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

Answer

You cannot rely on the geometry of a widget within its instantiation, unless you specify a fixed size: by default all QWidget classes have a size of 640×480 during inizialitation.

In your case, the solution is simple: move the computation of the player position in the resizeEvent(), which is the event that is received everytime a widget is being resized (by the system, the user, or any parent/child layout requirement):

    def resizeEvent(self, event):
        self.player = QRect(200, self.height() - self.dimension-2, self.dimension, self.dimension)

Note that your implementation is not completely correct, as the result is that the obstacles will have a wrong position if the window is resized while playing, so I suggest another approach: always create items at y=0, and translate the rectangles in the paint event:

class animation(QWidget):
    def __init__(self):
        # ...
        self.player = QRect(200, 0, self.dimension, self.dimension)

    def new_shape(self):
        coord_x = self.width()
        rect = QRect(coord_x, 0, self.dimension, self.dimension)
        self.shapes.append(rect)

    # ...
    def paintEvent(self, event):
        painter = QPainter(self)

        painter.setPen(QPen(Qt.white,0,Qt.SolidLine))
        painter.setBrush(QBrush(Qt.white,Qt.SolidPattern))
        painter.drawRect(self.rect())

        coord_y = self.height() - self.dimension

        painter.setPen(QPen(Qt.black,1,Qt.SolidLine))
        painter.setBrush(QBrush(Qt.green,Qt.SolidPattern))
        for shape in self.shapes:
            painter.drawRect(shape.translated(0, coord_y))

        painter.drawRect(self.player.translated(0, coord_y))

Please note that using findChild for a UI created with uic/pyuic is pointless, as the instance attributes for each widget are already created in the setupUi: you can already access the frame using self.frame:

class MyApp(QWidget,ui):
    def __init__(self):
        # ...

        self.frame.setLayout(layout)

You probably found out about that approach in some online tutorial, unfortunately they are completely wrong about that, and suggesting it is a very bad practice.