So I am attempting to create the foundation for a basic 2D python game with X and Y movement using a sprite.
However the display is unresposive despite the code here attempting to screen.fill and screen.blit
playerX = 50
playerY = 50
player = pygame.image.load("player.png")
width, height = 64*8, 64*8
screen=pygame.display.set_mode((width, height))
screen.fill((255,255,255))
screen.blit(player, (playerX, playerY))
Am I missing something important?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
A minimal, typical PyGame application
- has a game loop
-
has to handle the events, by either
pygame.event.pump()orpygame.event.get(). -
has to update the
Surfacewhuch represents the display respectively window, by eitherpygame.display.flip()orpygame.display.update().
See pygame.event.get():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
See also Python Pygame Introduction
Minimal example:
repl.it/@Rabbid76/PyGame-MinimalApplicationLoop
import pygame
pygame.init()
playerX = 50
playerY = 50
player = pygame.image.load("player.png")
width, height = 64*8, 64*8
screen = pygame.display.set_mode((width, height))
# main application loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the display
screen.fill((255,255,255))
# draw the scene
screen.blit(player, (playerX, playerY))
# update the display
pygame.display.flip()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0