Sublime text 3, pygame display is empty - python

I recently installed sublime text 3 and have been moving over from pycharm to sublime but have run into some difficulties with pygame. I installed python 3 and got it working, then fresh installed pygame, but when I brought over some code and ran it I got an empty pygame screen. Which looked like this:
I was filling the screen black and drawing a red rectangle yet none of it displayed. I am sure the program works because I tested it on both pygame and repl.it. But in case it would help here is the code for the project:
import pygame
import sys
pygame.init()
display_width = 600
display_height = 600
screen = pygame.display.set_mode((display_width, display_height))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 0, 0), ((300, 300), (20, 20)))
pygame.display.update()
I think it has to do with something I am not setting up with sublime. Help appreciated. Thx.

It turns out pygame is not compatible with Mac OS Mojave so you need to use the new dev build to get it to work.
Use pip install pygame==2.0.0.dev4

Related

pygame.error: video system not initialized We are making a cave game [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: why am I receiving the error "video system not initialized" when I used init()? [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...

Pycharm - pygame shows only black screen

Here is the code
import pygame
pygame.init()
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load("D:/Python/Pygame_200830/Background.png")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#screen.fill((0, 0, 255))
screen.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
if I open this with VS code, it works well but, when I do this with Pycham. I can't see "backgound.png"
it's just only black display... and the only difference is IDE program.
I already added external function or something... only that backgound is the problem.
Could you let me know what is the reason...?
It is likely that the root cause of your problem is from Pycharm configurations or venv. see if you can open the python console. If you're new to pycharm, you can find the 'python console' option at the bottom of the screen.
Also here's a link that might help you. try some of the solutions there:
https://www.reddit.com/r/learnpython/comments/au6lvd/pycharm_code_works_but_doesnt_display_output/

pygame: Bouncing rocket

I am just learning how to use pygame.
But whenever I run the code, it doesn't run but a python rocket appears and starts bouncing.
The only thing I can do on the rocket is force quit.
My python is 3.8.2 and pygame is 1.9.6, on my mac, if it helps.
This is the code I'm trying to run, just a basic set up code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
I just ran your code using python 3.7 on a macbook Mojave and I just got a red rectangle (like your code specifies) in one position. Possibly try restarting your computer, checking you version of python, or renaming the file (originally I named the file pygame and it was giving me a bunch of issues) to make it run the correct thing.
Jubin- I just ran your code on Mac and it works. I think the trouble is that IDLE won't allow it to run for some reason.
Try running it from the command line using Terminal app. It runs fine for me doing that, but will not run from IDLE.
Open Terminal
Navigate your way to the folder that contains your file
Type "python filename.py"

The display background color is not changed after a call to `fill` in Pygame

I'm following an example in a pygame tutorial:
import pygame
pygame.init()
surf = pygame.display.set_mode((400, 450))
#surf.fill((255, 0, 0))
surf.fill(pygame.Color(255, 0, 0))
pygame.display.update()
input()
As you can see, I've tried two ways:
surf.fill((255, 0, 0))
surf.fill(pygame.Color(255, 0, 0))
Neither works for me.
My working environment is macOS, the version of pygame is 1.9.6, the version of Python is 3.8.
What I saw:
Why is the background color not changed?
import pygame
pygame.init()
surf = pygame.display.set_mode((400, 450))
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#surf.fill((255, 0, 0))
surf.fill(pygame.Color(255, 0, 0))
pygame.display.update()
After searching for "pygame fill doesn't work" for a while and trying each one of the suggested answers, I found out that the only way to make the background color change happen is to upgrade pygame to 2.0.0.dev4.
Since by default pip only installs the most recent stable version of a package, I need to specify the version explicitly like pip install pygame==2.0.0.dev4.
And now I can see the red background.
I faced the same problem recently. I am using MacOS 10.15.7 and tried versions from 2.0.0 through 2.0.3dev4 of Pygame installed via Pip. Nothing worked. Until I read the following text in the Pygame documentation (https://www.pygame.org/docs/ref/color.html):
Color objects support equality comparison with other color objects and
3 or 4 element tuples of integers. There was a bug in pygame 1.8.1
where the default alpha was 0, not 255 like previously.
So, defining the RGB color with a 4 elements tuple (last number the alpha value, 255 opaque) solved the problem.

Categories