pygame 'screen' not defined - python

Following the Python Crash Course, I copied this input and ran it with an error
import os
import pygame
def play_game():
#Creates Screen
resolution = pygame.display.set_mode((1920, 1080))
pygame.init()
pygame.display.set_caption('Game')
#Colors the background
bg_color = (230,230,230)
#Starts the game
while True:
#Sets background color
screen.fill(bg_color)
#Looks for user inputs
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#updates the screen
pygame.display.flip()
play_game()
The error reads:
NameError: name 'screen' is not defined
How do I fix this? I believe I installed pygame and pip correctly.

You didn't define the name 'screen'. Use the same variable to refer that in screen.fill(), change it to resolution.fill()

Just change line five to :
screen = pygame.display.set_mode((1920, 1080))

Related

pygame.error: video system not initialized mouse = pygame.mouse.get_pos() [duplicate]

I am getting this error whenever I attempt to execute my pygame code:
pygame.error: video system not initialized
from sys import exit
import pygame
from pygame.locals import *
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
def game_loop():
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): # error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
exit()
game_loop()
#!/usr/bin/env python
You haven't called pygame.init() anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display that's complaining that you called either its set_caption or its flip without calling its init first. But really, as the tutorial says, it's better to just init everything at the top than to try to figure out exactly what needs to be initialized when.
Changing code to this, avoids that error.
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
pygame.quit()
if running:
screen.fill(white)
pygame.display.flip()
#this will fool the system to think it has video access
import os
import sys
os.environ["SDL_VIDEODRIVER"] = "dummy"
You just need to add
exit()
To stop running code
example :
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
exit() # Solution
You get an error because you try to set the window title (with set_caption()) but you haven't created a pygame window, so your screen variable is just a tuple containing the size of your future window.
To create a pygame window, you have to call pygame.display.set_mode(windowSize).
Good luck :)
If you doing pygame.init() then solve the problem video system initialized.
but you get the next error like:
(AttributeError: tuple object has no attribute 'fill') this.
this problem is solving when you doing this
screen = pygame.display.set_mode((600, 400))
but not doing like
screen = screen_width, screen_height = 600, 400
Then the full problem is solved.
I made some modifications to your code:
import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
while True:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(green)
pygame.display.flip()
For me, it was a problem because I didn't set pygame.quit() out of the loop at the end.
You have to add:
pygame.init()
Before you quit the display you should stop the while loop.
If you using class for your pygame window don't use pygame.init() in your class. Use pygame.init() at below libraries.
You need to initialized pygame using this command pygame.init
If the problem is not solved then following this step
this problem happens when you using a beta version.
so my suggestion is please use the new, old version(If now lunch 3.8 python, you
need to install python 3.7)
Now goto python terminal and install pygame (pip install pygame)
now the problem is solved...

Pygame always showing a black screen

I am trying to recreate this pygame but i keep getting a black screen without anything displayed. I get no error's so i don't know where i need to start.
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.image.load('graphics/Sky.png')
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)
The problem got solved after reinstalling python and pycharm. Not sure if both were needed but it did the trick for me, thanks all.

Error when trying to run pygame: " video system not initialized" [duplicate]

I am getting this error whenever I attempt to execute my pygame code:
pygame.error: video system not initialized
from sys import exit
import pygame
from pygame.locals import *
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
def game_loop():
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): # error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
exit()
game_loop()
#!/usr/bin/env python
You haven't called pygame.init() anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display that's complaining that you called either its set_caption or its flip without calling its init first. But really, as the tutorial says, it's better to just init everything at the top than to try to figure out exactly what needs to be initialized when.
Changing code to this, avoids that error.
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
pygame.quit()
if running:
screen.fill(white)
pygame.display.flip()
#this will fool the system to think it has video access
import os
import sys
os.environ["SDL_VIDEODRIVER"] = "dummy"
You just need to add
exit()
To stop running code
example :
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
exit() # Solution
You get an error because you try to set the window title (with set_caption()) but you haven't created a pygame window, so your screen variable is just a tuple containing the size of your future window.
To create a pygame window, you have to call pygame.display.set_mode(windowSize).
Good luck :)
If you doing pygame.init() then solve the problem video system initialized.
but you get the next error like:
(AttributeError: tuple object has no attribute 'fill') this.
this problem is solving when you doing this
screen = pygame.display.set_mode((600, 400))
but not doing like
screen = screen_width, screen_height = 600, 400
Then the full problem is solved.
I made some modifications to your code:
import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *
pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
while True:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(green)
pygame.display.flip()
For me, it was a problem because I didn't set pygame.quit() out of the loop at the end.
You have to add:
pygame.init()
Before you quit the display you should stop the while loop.
If you using class for your pygame window don't use pygame.init() in your class. Use pygame.init() at below libraries.
You need to initialized pygame using this command pygame.init
If the problem is not solved then following this step
this problem happens when you using a beta version.
so my suggestion is please use the new, old version(If now lunch 3.8 python, you
need to install python 3.7)
Now goto python terminal and install pygame (pip install pygame)
now the problem is solved...

pygame.error: video system not initialized python code error

After running this code, I got an error:
pygame.error: video system not initialized
My code:
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
Can anyone help me and explain what this error means and how to rectify it?
The error is raised because pygame.event.get is called without an initialized display (pygame.display.set_mode). The problem is that your while loop is not indented correctly, so it is executed before the run_game function is called. The loop should be inside of the run_game function.
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
put your code:
run_game()
before while statment`

unable to see image using pygame.image.load()

I am unable to see the image when I start the program. I have a folder named "game" and "mygame.py" file with a "background.png" all in it. I have tried using the the PATH "/game/background.png" instead of "background.png" but it doesn't seem to work. Any ideas?
my code:
import pygame , sys
pygame.init()
#screen start
def screen():
screen = [1024,768]
screen = pygame.display.set_mode(screen,0,32)
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.png")
screen.blit(background, (0,0))
while True:
screen.blit(background, (0,0))
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen()
Thank you.
You are missing the flip/update call:
clock = pygame.time.Clock()
while True:
#keyboard commands
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.flip()
clock.tick(40) # keep program running at given FPS
Every blit occurs in a internal buffer, you need to call flip or update once per frame to update the real screen.
To my knowledge you can make this work one of two ways.
You can use an absolute path to the file, such as:
"C:\path_to_game_folder\game\background.png"
or you can use a relative path. To do this add the following code to your program:
import os
dir = os.path.dirname(__file__)
backgroundFile = os.path.join(dir, "background.png")
and change:
pygame.image.load("background.png")
to
pygame.image.load(backgroundFile)
I suggest using relative paths whenever possible it keeps the code portable as well as makes it easier to maintain and distribute.
I got it! Using pygame its important to use "pygame.display.update()" inside while True: screen.flip doesn't work with pygame to refresh or update the screen. Thanks to the users who responded earlier though.
Full Code:
import pygame , sys
pygame.init()
#screen start
def screen():
width , height = 1280,768
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Testing Caption")
background = pygame.image.load("background.jpg")
screen.blit(background, (0,0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(background, (0,0))
pygame.display.update()
screen()

Categories

Resources