D
D
Dmitry2020-03-17 19:31:24
Python
Dmitry, 2020-03-17 19:31:24

Why doesn't the pygame.display.update() function update my screen?

I am writing a primitive game and at the end of the main loop, as expected, I update the screen with the function pygame.display.update(), but the screen is not updated until I start to move the mouse cursor over the window or move part of the window out of the monitor area. What is the problem?

the code itself:

from entity import *
from cfg import *

pygame.display.set_caption('runner')

player = player()

clock = pygame.time.Clock()
FPS = 60
clock.tick(FPS)
pygame.init()
pygame.display.set_caption('runner')


def main():
    run = True
    while run:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                run = False

        display.fill((100, 100, 100))
        player.render()
        player.move()
        pygame.display.update()


if __name__ == '__main__':
    main()


entity.py file code:

from cfg import *


class player:

    def __init__(self, posX=50, posY=50, size=20, moveSpeed=5, motion='none'):
        self.posX = posX
        self.posY = posY
        self.size = size
        self.moveSpeed = moveSpeed
        self.motion = motion

    def move(self):

        motion = 'none'

        for e in pygame.event.get():
            keys = pygame.key.get_pressed()
            if keys[pygame.K_UP or pygame.K_w]:
                motion = 'up'
            if keys[pygame.K_DOWN or pygame.K_s]:
                motion = 'down'
            if e.type == pygame.KEYUP:
                motion = 'none'

        if motion == 'up':
            if self.posY >= self.size * 2:
                self.posY -= self.moveSpeed
        elif motion == 'down':
            if self.posY <= screenHeight - self.size * 2:
                self.posY += self.moveSpeed

    def render(self):
        pygame.draw.circle(display, (255, 255, 255), (self.posX, self.posY), self.size)


cfg.py file code
import pygame

screenWidth = 600
screenHeight = 300
display = pygame.display.set_mode((screenWidth, screenHeight))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IDzone-x, 2020-06-10
@nosochek

Why is there no cycle.
it doesn't work for this reason
Check out this course.
https://younglinux.info/pygame
You need to loop control, update and render.
And if you are a fan of video courses))
I can recommend Gosha the Dudar's pygame course.

M
Michael, 2020-03-17
@Araben

maybe it should be like this? don't know what pygame.key.get_pressed() should output

if keys  in [pygame.K_UP,pygame.K_w]:
    motion = 'up'
if keys in [pygame.K_DOWN,pygame.K_s]:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question