This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 1 year ago.
So basically, its all well and good when I have it at this stage:
import pygame
from pygame.locals import *
pygame.init()
screen_width = 1000
screen_height = 1000
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
but as soon as i add something like this:
import pygame
from pygame.locals import *
pygame.init()
screen_width = 1000
screen_height = 1000
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
sun_img = pygame.image.load('img/sun.png')
bg_img = pygame.image.load('img/sky.png')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
it just opens and closes immediately.
Im a huge novice with code and am just having some fun for now but this issue is really annoying and I would appreciate any help I could get!
Related
This question already has answers here:
Python Pygame Newbie Code Showing Blank Screen
(4 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 10 months ago.
so im trying to blit tonk but its not working. can you help? all it shows is black
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
screen.blit(Tonk,(0,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
clock = pygame.time.Clock()
clock.tick(60)
You need to blit in the while loop and add a display update too :
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
screen.blit(Tonk,(0,0))
pygame.display.flip()
clock.tick(60)
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
My code is below, when it is run the Pygame screen stays black and is not filled with the correct color at all, (87, 160, 211)
import pygame
pygame.init()
window_size = (800,600)
screen = pygame.display.set_mode((800,600))
car_image = pygame.image.load('car.png')
def display_car(x,y):
screen.blit(car_image, (x,y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
You must update the display by calling either pygame.display.update() or pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
pygame.display.flip()
I followed a turtorial where I was lerning to create games using pygame. I just started and I already have a problem. It says it has trouble with finding the image. Here is what it says:
C:\Users\Patryk\AppData\Local\Programs\Python\Python39\python.exe:
can't open file ''
C:\Users\Patryk\PycharmProjects\PePeSza-Game\main.py: [Errno 2]
I tried looking for someone with same problem, but couldn't find the anwser to my question. Here is whole code:
import pygame
pygame.init()
# Game window
screen_width = 800
screen_height = 640
lower_margin = 100
side_margin = 300
screen = pygame.display.set_mode((screen_width,screen_height))
screen = pygame.display.set_caption(('Level editor'))
# Importing images
bg_img = pygame.image.load('./bg.png')
# Create function for drawing background
def draw_bg():
screen.blit('bg_img', (0, 0))
# Mainloop
run = True
while run:
draw_bg()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
There are two thing you need to do in the code
removing the ./ before image name. It might be causing issues
# Importing images
bg_img = pygame.image.load('bg.png') # removing the ./
In screen.blit in draw_bg, you were passing string but you need to pass the object made with pygame.image.load
# Create function for drawing background
def draw_bg():
screen.blit(bg_img, (0, 0)) # removing the apostrophe
Full code:
import pygame
pygame.init()
# Game window
screen_width = 800
screen_height = 640
lower_margin = 100
side_margin = 300
screen = pygame.display.set_mode((screen_width,screen_height))
screen = pygame.display.set_caption(('Level editor'))
# Importing images
bg_img = pygame.image.load('bg.png')
# Create function for drawing background
def draw_bg():
screen.blit(bg_img, (0, 0))
# Mainloop
run = True
while run:
draw_bg()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
When I run the code a blue rect is displayed in its proper location however the entire window freezes and eventually crashes. How can I fix this?
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
pygame.display.update()
You have to add an event loop. Handle the events, by either pygame.event.pump() or pygame.event.get(). Thus the IO and internal events are handled and the window keeps responding. e.g.:
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the disaply
win.fill(0)
# draw the scene
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
# update the dispaly
pygame.display.update()
pygame.quit()
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Why is my PyGame application not running at all?
(2 answers)
Closed 2 years ago.
import pygame
import sys
import os
basic stuff
'''
Objects
'''
# put Python classes and functions here
'''
Setup #...
'''
# put run-once code here
fps = 60
ani = 4
clock = pygame.time.Clock()
pygame.init()
I think here might be a problem:
On the internet I saw something with .convert() but it keeps giving me :
AttributeError: 'str' object has no attribute 'convert'
when I try to put .conver()
it's been 2 hours of searching and I can't find this noob stuff...
background = pygame.image.load(os.path.join('background.png'))
size = (width, height) = background.get_size()
world = pygame.display.set_mode(size)
...
'''
Main Loop
'''
or here:
world.blit(background, (0,0))
...
while main == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
pygame.display.flip()
clock.tick(fps)