As someone new to pygame, I have searched the web for how to create a rectangle. I then used that code, but when run it just showed a blank screen. No error codes showed, but the code didn't run like I thought it would. What am I doing wrong?
import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
pygame.init
#window variables
WindowHeight = 480
WindowWidth = 640
surface = pygame.display.set_mode((WindowWidth,WindowHeight))
surface.fill((255,255,255))
pygame.draw.rect(surface,(100,0,155),(0,0,100,100))
while True:
for event in GAME_EVENTS.get():
if event.type == GAME_GLOBALS.QUIT:
pygame.quit()
sys.exit()
Your code is pretty, but to write a good pygame code start from this:
import pygame, sys
from pygame.locals import *
WindowHeight = 480
WindowWidth = 640
def main():
pygame.init()
DISPLAY=pygame.display.set_mode((WindowWidth,WindowHeight),0,32)
WHITE=(255,255,255)
blue=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,blue,(200,150,100,50))
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
main()
put everything in the main function, init pygame and update the display.
This creates a simple window that is white. Within the window will be a blue rectangle. Look up pygame.org for more info
Related
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...
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.
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...
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...
Hi I am new to this site.
Here is the question.
Currently the program displays an image which is supposed to be the start up screen for three seconds.
I then want it to stop displaying the first image and display another one instead continuously.
I am using pygame version 1.9.1 and python version 2.7.9.
The code is below.
Thanks in advance too.
import pygame, sys
from pygame.locals import *
pygame.init()
gameDisplay=pygame.display.set_mode((600,600))
pygame.display.set_caption("Platypus")
white=255,255,255
black=0,0,0
red=255,0,0
important_platypus=pygame.image.load("C:/Users/Sarah_2/Pictures/no_one_knows_why_Duck_Billed_Platypi_are_important.jpg")
important_platypus=pygame.transform.scale(important_platypus,(600,600))
gameExit=False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameDisplay.blit(important_platypus, (0, 0) )
pygame.display.update()
pygame.quit()
quit()
time.sleep is the function you want. Below is a full example:
import pygame, sys
from pygame.locals import *
pygame.init()
gameDisplay=pygame.display.set_mo de((600,600))
pygame.display.set_caption ("Platypus")
time.sleep(3)
gameDisplay=pygame.display.set_mo de((600,600))
pygame.display.set_caption ("second image")