"pygame.error: video system not initialized" - python

Does anyone know why this error keeps coming up? I'm not sure what's wrong with the code. I've copied this from a video just to test mouse tracking and yet my version isn't working even though the two sets of code are identical (I believe). Here is the video I took the code from: https://www.youtube.com/watch?v=dxhNcQ-FDaY
import pygame
from pygame.locals import *
import sys
class MousePos(object):
def __init(self):
pygame.init()
self.myFont = pygame.font.SysFont("Arial", 24)
pygame.display.set_caption("Mouse Events")
self.myScreen = pygame.display.set_mode((400, 400), 0 ,32)
self.myScreen.fill((255, 255, 255))
pygame.display.update()
def coords(self):
text = self.myFont.render(str(pygame.mouse.get_pos()), True, (0, 0, 0))
pygame.draw.rect(self.myScreen, (255, 255, 255), (200, 200, 100, 100), 0)
self.myScreen.blit(text, (200, 200))
pygame.display.update()
if __name__ == "__main__":
newMouse = MousePos()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
newMouse.coords()

The problem with your code is that your initialization method __init__, is missing a underscore. Since your class is never being initialized, your never creating a Pygame screen. Thus the error
Change this line: def __init(self):, To this: def __init__(self):

My guess would be a missing dependency or library mismatch.
How did you install pygame, and were there any errors.
I recommend you use pip or your distribution's package manager.
Also, you should make sure that any non-python APIs and modules that you might need are all installed.

Related

Endless attribute errors

I've been trying out a python application that displays text on the full screen. Couldnt convert it into an exe file to run on windows so tried it online.
The latest is AttributeError: 'NoneType' object has no attribute 'fill'.
There seems no end to attribute errors. It began off with a small compact code but what with attribute error after another whenever I added the suggestions from StackOverflow it's a bit bloated now. Would appreciate some way ahead.
I'm running it on the Colaboratory site (https://colab.research.google.com/#)
It's always come up with numerous blocks that after all the amendments it looks like this.
!apt-get -qq install -y libarchive-dev && pip install -U libarchive
import libarchive
!pip install pygame
import pygame
pygame.init()
pygame.init()
def borders(cells):
width=get_screen_width()
height=get_screen_height()
pygame.display.fullscreen = True
pygame.display
pygame.init()
import time
pygame.font.init()
import platform
if platform.system() == "Windows":
def play_game():
display = pygame.display.set_mode((800,600))
bg_color = (230,230,230)
!pip install pgzero
import pgzero
from pgzero.game import screen
screen: pgzero.screen.Screen
screen.fill(bg_color)
myfont = pygame.font.Font(None, 90)
f = open("/Users/bgr/Desktop/Advice.txt","r")
for line in f:
for word in line.split():
text = myfont.render(word,1,(0,255,0))
screen.blit(text, (300,300))
pygame.display.update()
time.sleep(0.2)
screen.fill((background))
pygame.display.update()
pygame.display.quit() ````
Thanks
Your question is a little too broad, so I'm just going to answer a couple
I've been trying out a python application that displays text on the full screen.
pgzero aim to be super simple and hiding a lot of details, it has hooks that pgzero will call if you provides them in the script, to display something on the screen you need to provide draw() hooks (function).
import pgzrun
import pygame
WIDTH = 800
HEIGHT = 600
green = (0, 128, 0)
white = (255, 255, 255)
def draw():
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
screen.fill(green)
screen.draw.text("Hello world", (100, 100), color=white)
pgzrun.go()
I'm running it on the Colaboratory site (https://colab.research.google.com/#)
I think you should your local pc instead, see this

Import "pygame" could not be resolved

I installed pygame using the pip in command prompt but it still shows an error. I have already attempted changing the path so pip could properly be accessed and using the line "pygame.init()" but the error still appears.
Here's the code:
import pygame, sys
pygame.init()
w, h = 600, 600
r = (255, 0, 0)
s = pygame.display.set_mode( (w, h) )
s.fill(r)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
by the way if it still isn't working you could try using Pycharm. If you can't do that you could try using replit and downloading the pygame package, then exporting it with Github.

How do I maximize the display screen in PyGame?

I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back?
Thanks
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
To become fullscreen at native resolution, do
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()
You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
The method you are looking for is pygame.display.toggle_fullscreen
Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.
In your case this would look like
DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)
(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init()
info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h
These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:
window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
This will let you toggle from maximize to the initial size
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
You'd have to add the full screen parameter onto the display declaration like this:
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
Just use pygame.RESIZABLE as an easier option.

background music not playing in pygame

When i run the code it play only the .wav file
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
pygame.mixer.music.stop()
someone know what i should do for the background.ogg to be played too?
Remove this line:
pygame.mixer.music.stop()
Basically, you're loading and playing the background noise, and then immediately stopping it. I also suggest you create a main loop, like so:
import pygame
from pygame.locals import *
# This makes sure that you're not importing the module.
if __name__ == "__main__":
# Create surface
size = width, height = (500, 400)
# You don't need the extra arguments
window = pygame.display.set_mode(size)
# Do sound stuff
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
# This is your main loop.
running = True
while running:
# Check if escape was pressed
Most of the time people just check if Escape was pressed in their main loop (if it was pressed, they set running = False and exit the program).

pygame.error: file not a windows.bmp file (have looked at other similar questions but unsuccessful so far)

I'm very new to pygame, and am using the book "Beginning Game developement with Python and Pygame". I have pretty much typed the example code and keep getting the same
"pygame error: file not a windows.bmp file"
and would like to be able to load jpg/png as in the example in the book. I'm pretty sure I'm in the right directory for mine to work and the images I wanted to use are the same format as in the example. I have also searched for solutions but none of the answers seemed to work for me.
The code from the book is as follows (I have python 2.7.4, Ubuntu 13.04 and (I think) pygame 1.2.15):
background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'fugu.png'
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
my version of the code so far:
import os.path
background = os.path.join('Documents/Python/Pygame','Background.jpg')
cursor_image = os.path.join('Documents/Python/Pygame','mouse.png')
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
Thanks for your help :)
I can almost guarantee that you're getting your paths wrong. In your code, you've put in relative paths, meaning that pygame is looking for your assets in subfolders of the working directory (the directory where you execute your code).
A demo of how I think you would have to have things laid out and where your code is looking is below - in this example you would have a command prompt open in /home/your_username/Documents/my_games (or ~/Documents/my_games) and you'd be running python your_game_script.py.
|---home
|---your_username
|---Documents
|---some_subfolder
|---my_games
|---your_game_script.py
|---Documents
|---Python
|---Pygame
|---Background.jpg
|---mouse.png
This would work, but I suspect you don't have your folders set up this way, and that's the reason it's not working. If you run an interactive python prompt in the same folder as your game script, try the following:
import os
os.path.isfile('Documents/Python/Pygame/Background.jpg')
os.path.isfile('Documents/Python/Pygame/mouse.png')
I suspect the result will be false for both - meaning the files couldn't be found at that subfolder location. I would recommend that you have the following structure for your game files:
|---my_game
|---your_game_script.py
|---images
|---Background.jpg
|---mouse.png
Then in your_game_script.py you can load the files in the following way:
background = 'images/Background.jpg' #relative path from current working dir
cursor_image = 'images/mouse.png' #relative path from current working dir
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()

Categories