My Pygame image not loading - python

I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame.
I saved a png image in the same folder as my py script. When I run the script it shows no error but my image does not shows in pygame window. Kindly advice
Here's the script
import pygame,sys
pygame.init()
WIDTH,HEIGHT = 640,360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
dog_img = pygame.image.load("dog.png")
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#PROCESS
#LOGIC
#LOGIC
#DRAW
screen.blit(dog_img,(0,0))
pygame.display.flip()
#DRAW
clock.tick(FPS)

Fix the indentation. screen.blit(dog_img, (0, 0)) and the two lines below should be inside of the while loop (indented with 4 spaces).
import pygame,sys
pygame.init()
WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
# Always use `.convert()` or `.convert_alpha()`. It'll improve the performance.
dog_img = pygame.image.load("dog.png").convert_alpha()
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#LOGIC
#DRAW
screen.blit(dog_img, (0, 0))
pygame.display.flip()
clock.tick(FPS)

Related

pygame surface isn't visible

I'm currently trying to follow the Introduction to Pygame tutorial and I'm stuck on one step where the speaker makes the surface. His surface is bright red while my surface isn't visible at all.
Here's the code:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)
I've already tried starting from the very beginning, my code and the steps I make are identical to his. I've also tried deleting python and pygame. I've installed Python version that the speaker has (3.9), but nothing helps.
It is a matter of indentation. You have draw the scene and update the display in the application loop:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# INDENTATION
#<------|
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)

I am trying to insert a sprite image into pygame window and it wont pop up

(i followed a yt tut so idk) my code: idk if i put code in wrong place
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
player_sprite = pygame.image.load("will smith.png") .convert_alpha
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
not really sure why it does this, im new to python so idk
There are multiple things that needs to fixed. First of all to you actually need to blit the image on the screen.
while not exit:
for event in pygame.event.get():
#...[block of code]
display.fill(white)
display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN
.convert_alpha() is a method and you need to call it after you initialize pygame and set the video mode.
You can read more about convert_alpha() here
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
#converting it after init and setting game mode
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Here's the final working code
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
display.blit(player_sprite, (100,100))
pygame.display.update()
pygame.quit()
quit()
You have to blit the image in the application loop, after clearing the display and before updating the display:
display.blit(player_sprite, (100, 100))
convert_alpha is function. You missed the parentheses
player_sprite = pygame.image.load("will smith.png").convert_alpha
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Also, you cannot call a pygame function before you initialize pygame. pygame.init() should be the very first pygame function called.
Complete example:
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
display.blit(player_sprite, (100, 100))
pygame.display.update()
pygame.quit()
quit()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()

I'm very new to python and one of my images will not draw onto the screen

I was trying to draw an image onto the screen but I keep getting errors. The error keeps saying "video system not initialized". I am new to python, can anyone help?
import pygame
import os
#game window
WIN = pygame.display.set_mode((1000, 800))
NAME = pygame.display.set_caption("Space War!")
#FPS limit
FPS = (60)
#image i am trying to draw onto screen
SPACE_BACKGROUND = pygame.image.load(os.path.join('space_background.png'))
pygame.init()
#allows pygame to quit
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
pygame.display.update()
#calling def main(): function
if __name__ == "__main__":
main()
do:
def main():
clock = pygame.time.Clock()
run = True
FPS = 60
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
WIN.blit(SPACE_BACKGROUND, (0, 0)) #you FORGOT this part
pygame.display.update()
BTW the main function cannot access the global 'FPS' variable so declare that within the 'main' function(make it local).

Python- Pygame won't display images or captions

My code runs with no errors, but the background image does not display and neither does the caption at the top of the window. I have checked that my script is importing the correct pygame module, and tried different images. I have Python 3.6.2 on a Mac.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Users/MyMacbook/Desktop/pac-man/background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()
Yeah it appears that #skrx is correct, there's something blocking by not clearing the event buffer. This code works for me:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Path/To/Background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()

How to queue multiple pygame scripts in a module?

Does anyone know if there is a way to make pygame scripts run one after another?
This is my module below, I would like to know if I could get them to deploy at certain intervals, that is, I would like 'A_long_time_ago' to run, last for 5 seconds then close and immediately load 'sounds' and 'logo', then 3 seconds later for them to close, transitioning into 'Title_Crawl'.
The Module
from A_long_time_ago import *
from sounds import *
from logo import *
from Title_Crawl import *
A_long_time_ago
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((1376, 760))
clock = pygame.time.Clock()
done = False
font = pygame.font.SysFont("franklingothicbook", 40)
text = font.render("A long time ago in a galaxy far, far away....", True, (75, 213, 238))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
screen.fill((0, 0, 0))
screen.blit(text,
(700 - text.get_width() // 2, 380 - text.get_height() // 2))
pygame.display.flip()
clock.tick(60)
sounds
import pygame
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.music.load('The_Theme.wav')
pygame.mixer.music.play()
logo
import pygame
import os
_image_library = {}
def get_image(path):
global _image_library
image = _image_library.get(path)
if image == None:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
_image_library[path] = image
return image
pygame.init()
screen = pygame.display.set_mode((1000, 800))
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((0, 0, 0))
screen.blit(get_image('sw.png'), (10, 10))
pygame.display.flip()
clock.tick(60)
Title_Crawl
#!/usr/bin/python
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Title Crawl')
screen = pygame.display.set_mode((1000, 800))
screen_r = screen.get_rect()
font = pygame.font.SysFont("franklingothicdemibold", 40)
clock = pygame.time.Clock()
def main():
crawl = ["Star Wars - The Wilds"," ","It is a dark time for the Galaxy. The evil Dark","Lord, Vitiate is rising to power. Alone, a single", "spec is on a trip, a trip that will ultimately", "rectify the wrongs of the galaxy. The keepers ", "of peace are dying out and the DARK SIDE is", "lurking, a conniving force determined to", "become the omniarch."]
texts = []
# we render the text once, since it's easier to work with surfaces
# also, font rendering is a performance killer
for i, line in enumerate(crawl):
s = font.render(line, 1, (229, 177, 58))
# we also create a Rect for each Surface.
# whenever you use rects with surfaces, it may be a good idea to use sprites instead
# we give each rect the correct starting position
r = s.get_rect(centerx=screen_r.centerx, y=screen_r.bottom + i * 45)
texts.append((r, s))
while True:
for e in pygame.event.get():
if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE:
return
screen.fill((0, 0, 0))
for r, s in texts:
# now we just move each rect by one pixel each frame
r.move_ip(0, -1)
# and drawing is as simple as this
screen.blit(s, r)
# if all rects have left the screen, we exit
if not screen_r.collidelistall([r for (r, _) in texts]):
return
# only call this once so the screen does not flicker
pygame.display.flip()
# cap framerate at 60 FPS
clock.tick(60)
if __name__ == '__main__':
main()
The best way to do this would be to make them into classes and give them a run method, and then instantiate each one on a timer and call its run method to do what it needs to do.

Categories