Background image not appearing in pygame - python

My code currently looks like this
import pygame
from pygame.locals import *
pygame.init()
width,height = 1080,810
keys = [False,False,False,False]
screen = pygame.display.set_mode((width,height))
game_running = True
background = pygame.image.load("resources/images/background.png")
while game_running:
screen.fill((0,0,0))
screen.blit(background,(0,0))
When the code is run, a black window pops up, but the background isn't there.
I checked the directories to make sure the loading of the image has no issue too.

pygame draws in buffer in RAM memory (to make animation less flicking and tearing).
You have to use pygame.display.update() or pygame.display.update() to send from buffer to video card which will display it on monitor.
import pygame
width = 1080
height = 810
keys = [False, False, False, False]
pygame.init()
screen = pygame.display.set_mode((width,height))
background = pygame.image.load("resources/images/background.png").convert()
game_running = True
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game_running = False
screen.fill((0,0,0))
screen.blit(background, (0,0))
pygame.display.flip()
pygame.quit()
Wikipedia: Double buffering in computer graphics

Related

Pygame: Whenever I exit full-screen window, my pygame window always goes to top right corner [duplicate]

This question already has answers here:
Pygame Display Position
(8 answers)
Closed 11 months ago.
Whenever I exit from my full-screen pygame display and return to normal pygame window dimensions the window always goes to the uppermost right corner of my monitor screen is there a way that I can specify the position of the window
This is my source code:
#Setup Python---------------------------------------------------#
import pygame, sys, random, os
#Constants
WIDTH, HEIGHT = (600,400)
BLACK = (0,0,0)
#Setup pygame/window--------------------------------------------#
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('screen resize')
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
#fullscreen
fullscreen = False
#Loop-----------------------------------------------------------#
while True:
#Background-------------------------------------------------#
screen.fill(BLACK)
#Buttons----------------------------------------------------#
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_f:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
#Update-----------------------------------------------------#
pygame.display.update()
mainClock.tick(60)
I found the solution by using this code:
from pygame._sdl2.video import Window
window = Window.from_display_module()
window.position = (100,100)
I implemented into my source code like this (represented by the <----):
#Setup Python---------------------------------------------------#
import pygame, sys, random, os
from pygame._sdl2.video import Window <-----
#Constants
WIDTH, HEIGHT = (600,400)
BLACK = (0,0,0)
#Setup pygame/window--------------------------------------------#
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('screen resize')
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
#fullscreen
fullscreen = False
#Loop-----------------------------------------------------------#
while True:
#Background-------------------------------------------------#
screen.fill(BLACK)
#Buttons----------------------------------------------------#
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_f:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
window = Window.from_display_module() <----
window.position = (100,100) <----
#Update-----------------------------------------------------#
pygame.display.update()
mainClock.tick(60)

Why my background image is not appearing in the window in pygame?

Why my background image is not appearing in the window in pygame?
import pygame
pygame.init()
screen = pygame.display.set_mode ((1000,437))
background = pygame.image.load("background.jpg")
running = True
while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
You just need to update the display with this command: pygame.display.update()
Place the command at the end of your while running: loop.
pygame.init()
screen = pygame.display.set_mode ((1000,437))
background = pygame.image.load("background.jpg")
running = True
while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

can you help me it wont display my rectangle

import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
As #Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.
Since a piece of sample code is worth a thousand words:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
# Handle user-events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Re-draw the screen
pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
pygame.display.flip()
pygame.quit()
sys.exit()

Pygame window freezes on quit

I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()

image is bad with python in pygame

I have a program in python:
import sys, random, pygame
from pygame.locals import *
pygame.init()
# Preparing Pygame
size = width, height = 718, 502
screen = pygame.display.set_mode(size)
framerate = pygame.time.Clock()
pygame.display.set_caption('Flappy Cube')
#Peparing background
background_x = 0
background_y = 0
background = pygame.image.load("background.jpg")
#Preparing Cube
cube_x = 100
cube_y = 200
cube_unscaled = pygame.image.load("cube.png")
cube = pygame.transform.smoothscale(cube_unscaled, (64, 64))
#Preparing Tubes
tube_x = 750
tube_y= 300
tube_unscaled = pygame.image.load("tube.png")
tube = pygame.transform.smoothscale(tube_unscaled, (125, 500))
# The main game loop
def exit_game():
sys.exit()
while True:
#Background
screen.blit(background, (background_x,background_y))
background_x = background_x+254.5
screen.blit(cube,(cube_x, cube_y))
#Tube
tube_x = tube_x -.075
screen.blit(tube,(tube_x, tube_y))
# If exit
for event in pygame.event.get():
if event.type == pygame.QUIT: exit_game()
# If Space Key is presed
elif event.type == pygame.KEYDOWN and event.key == K_SPACE:
cube_y = cube_y-10
#If Clicked
elif pygame.mouse.get_pressed()[0]:
cube_y = cube_y-10
framerate.tick(60)
pygame.display.flip()
run_game()
I get this result: http://i.stack.imgur.com/MKYRM.png
I have to boost framerate.tick(60) to framerate.tick(700) it looks a glichy. when I program the gravity the multiple images won't look good.
how can i fix the images been drawn multiple times before the screen updates?
Try:
cube_unscaled = pygame.image.load("cube.png").convert_alpha()
You shouldn't need to boost your FPS in such way, 60 is a good value.
Also, I like better this order for your main loop: check events, draw, update, tick.
I don't think it makes a difference to your problem, but I think it easier to understand that way.
while True:
# If exit
for event in pygame.event.get():
if event.type == pygame.QUIT: exit_game()
# If Space Key is presed
elif event.type == pygame.KEYDOWN and event.key == K_SPACE:
cube_y = cube_y-10
#If Clicked
elif pygame.mouse.get_pressed()[0]:
cube_y = cube_y-10
#Background
screen.blit(background, (background_x,background_y))
background_x = background_x+254.5
screen.blit(cube,(cube_x, cube_y))
#Tube
tube_x = tube_x -.075
screen.blit(tube,(tube_x, tube_y))
pygame.display.flip()
framerate.tick(60)
I fixed it! I just needed to fill the screen with black before "bliting" it.

Categories