'No available video device' error in pygame on vs code - python

So I am getting started with learning pygame and running the following code:
import pygame as pg
pg.init()
window = pg.display.set_mode((500, 500))
pg.display.set_caption("First Game")
x = 50
y = 50
height = 60
width = 30
vel = 10
run = True
while run:
pg.time.delay(100)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
pg.quit()
While trying to execute this via terminal, I am getting an error as "No available video device".
How do I resolve this error?
I am running Python 3.7.6, pygame 1.9.6 on VS Code, Ubuntu 20.04

Please run this
import pygame
pygame.font.init()
width = 700
height = 700
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Client")
def redrawWindow():
win.fill((128, 128, 128))
pygame.display.update()
def main():
run = True
clock = pygame.time.Clock()
redrawWindow()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
pygame.display.update()
main()

Related

Error: video system not initialized when using pygame

I keep having the error whenever I try to run this code:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.quit:
run = False
pygame.quit()
if __name__ == "__main__":
main()
I've tried adding pygame.init() but it didn't help.
You have a couple of problems here. The immediate cause of the error is that your call to pygame.quit() isn't inside the main function, so it gets called immediately when the program starts. You need to fix that:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.quit:
run = False
pygame.quit()
if __name__ == "__main__":
main()
This will allow your program to run...but it's never going to quit. You're comparing event.type == pygame.quit, but pygame.quit is a function; this comparison will always be False. You need to compare it to pygame.QUIT, which is an integer value that will match event.type as expected:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()

Keep getting an error about pygame not being initialised

I keep getting an error about system not being initialised. The error is about the pygame.display.update()
Where is this meant to go?
import pygame #IMPORTS THE PYGAME CLASSES, METHODS AND ATTRIBUTES
from pygame.locals import * #IMPORTING ALL PYGAME MODULES
pygame.init() #INITIALISING PYGAME
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("On The Run")
blue = 146,244,255 #BACKGROUND COLOUR
width = 80
height = 60
x = 200 #X-POSITION OF THE CHARACTER
y = 100 #Y-POSITION OF THE CHARACTER
player1 = pygame.image.load('assets/characterMove3.jpg') #DISPLAYING THE IMAGE ONTO THE SCREEN
player1 = pygame.transform.scale(player1,(width,height)) #SCALING THE IMAGE TO SUITABLE DIMENSIONS
WINDOW.blit(player1,(x,y))
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False #WE WILL QUIT THE GAME AS THE VARIABLE run IS NOW FALSE
pygame.quit() #IT WON'T SHOW THE MOST RECENT THING I DREW UNLESS I MANUALLY UPDATE IT
pygame.display.update()
game_loop()
The problem is that pygame.quit() is called in the application loop. pygame.quit() deinitializes all Pygame modules and crashes all subsequent Pygame API calls. pygame.quit() must be the very last Pygame API call. Call pygame.quit() after the application loop:
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pygame.quit() <-- DELETE
pygame.display.update()
pygame.quit() # <-- INSERT
game_loop()

Pygame - window opens but fill() doesn't work and no events detected

I'm creating a simple pygame program (PyGame 1.9.6 on Python 3.7), but some of the code inside my while loop doesn't seem to work. When I run the program, the window opens, but the screen doesn't fill with black, nor does the window close when I press "x"
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
In the console, "tick" appears like normal and "event detected" appears after pressing any keys or mouse click. I don't get any errors when I run it.
If event == pygame.QUIT: should be if event.type == pygame.QUIT:
I usually use that event like this:
if event.type == pygame.QUIT:
pygame.quit()
quit()
Just as #Telan said
if event==pygame.QUIT:
should be
if event.type==pygame.QUIT:
However to properly shutdown pygame, pygame.quit() is important to shutdown pygame modules which is the reverse of pygame.init()
While sys.exit() is used to properly shutdown the main python program.
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
Full code is shown below. Enjoy!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()

My pygame screen is not updating event with pygame.display.update

My pygame screen is not updating correctly and i am not sure why.
pygame code:
import pygame
import os
import random
import time
pygame.init()
width = 750
height = 750
win = pygame.display.set_mode((width, height))
pygame.display.set_caption('Space attack')
bg_size = 750, 750
bg = pygame.image.load("/home/pi/Pygames/Space_attack/background-black.png")
image = pygame.transform.scale(bg, bg_size)
#ships
r_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_red_small.png')
g_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_green_small.png')
b_ship= pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_blue_small.png')
#player
player = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_ship_yellow.png')
#laser
r_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_red.png')
b_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_blue.png')
g_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_green.png')
p_laser = pygame.image.load('/home/pi/Pygames/Space_attack/pixel_laser_yellow.png')
#game
def main():
run = True
fps = 30
clock = pygame.time.Clock()
def redraw_window():
win.blit(bg, (0,0))
pygame.display.update
while run:
clock.tick(fps)
redraw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
main()
I have look on the internet and found nothing that works, I am new and this is my first pygame.
Any help would be appreciated.
Simply change pygame.display.update to pygame.display.update()
pygame.display.update does not call the function.
If you want to call the function, use function_name(args).

Pygame window closes right after

import pygame
WIDTH, HEIGHT = 1100, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
WHITE = (255, 255, 255)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
if __name__ == "__GAME!__":
main()
When I run this code, the pygame window closes right after while using Virtual Studio Code. While using Jupyter notebook it runs but goes unresponsive after 2-3 seconds. Both of them do not display any errors though.
I'm not sure why you're using
if __name__ == "__GAME!__"
But if I change this to
if __name__ == "__main__"
Then it works.

Categories

Resources