I am trying to retrieve the color I have drawn onto my display using pygame, but I can't seem to get it to work. I took out some irrelevant code for easier reading, but here is what I have.
import pygame
import sys
from pygame.locals import *
pygame.init()
blue = (0,0,255)
#sets up screen
setDisplay = pygame.display.set_mode((400,400))
pygame.display.set_caption('Connections')
pygame.draw.circle(setDisplay, blue, (20,20), 10, 10)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
print pygame.setDisplay.get_at((20,20))
Whenever I run this code, I get the following error:
TypeError: descriptor 'get_at' requires a 'pygame.Surface' object but received a 'tuple'
setDisplayis a variable you created, not an attribute on pygame. Try just setDisplay.get_at((20,20)). I'm not sure why you are even getting at the descriptor, it seems like you should be getting an AttributeError on pygame.setDisplay before even calling the get_at descriptor, but in any case, you should be calling it against your setDisplay, not off an attribute of pygame.
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 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...
I´m a newbie on pygame, and i have this code for trying to do kind of a startscreen for a football game, this start screen has a static background image that is"img_start_screen" and the logo of the game that I want it to have a little animation that stops when it reaches the certain point on the screen, that logo is the variable "logo"
import pygame,sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((1280,720))
pygame.display.set_caption("CEFoot 3.0")
FPS=60
fpsClock=pygame.time.Clock()
img_start_screen=pygame.image.load("start_screen.gif")
logo=pygame.image.load("logo8bit.png")
logox=390
logoy=0
direction="down"
while True:
window.blit(img_start_screen,(0,0))
if direction == "down":
logoy+=5
if logoy==300:
direction="center"
if direction == "center":
pass
screen.blit(logo,(logox,logoy))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update
fpsClock.tick(FPS)
The problem is that when I run the code it starts as a black screen and not until I move the window "out" of the Windows screen it refreshes, the images aren´t the problem as I ran the code without them and it showed the same black screen. I´m beginning to think that it isn´t code problem but compability as I´m running Python 3.2 and Pygame 1.9(I think) the last version that is really old , all this on Windows 10, so this might be the problem.Thanks in advance.
As you know pygame.display.update is a function
as seen if you print it:
>>> print pygame.display.update
<built-in function update>
so when you call pygame.display.update without parenthesis () you are merely stating a reference to a function. Whereas if you use parenthesis you call the function:
>>> print pygame.display.update()
None
this calls the function and returns None
Having a pygame.display window open, I call pygame.display.quit() upon it in order to destroy the window.
Because I need to open the window again, I call pygame.display.init() and pygame.display.set_mode(), but after these two functions are called, nothing happens.
Can anyone point me to the root of this problem?
Here is example code with a gui module... Whenever you call screen_off() then the display quits. Whenever you want display to come back, type everything you used before to turn it on.
If you want, use pygame.display.quit(), without it being inside the screen_off() function. I suggest taking all the code you used to get the display on, and putting it into a function so you don't have to type it again to turn it on after it's been killed.
from pygame import *
from pygame.locals import *
import pygame, pygame.locals
from easygui import *
def screen_off():
pygame.display.quit()
pygame.init()
canvas = pygame.display.set_mode((400,400),0,32)
red = (255,0,0)
canvas.fill(red)
pygame.display.update()
screen_off() #display is now OFF...
choice = ['Yes', 'No']
cc = buttonbox('Continue?', "Options", choice)
if cc == "Yes":
#if you don't want to type these arguments below again to turn on the display then
#put them into a function and call it
pygame.init()
canvas = pygame.display.set_mode((400,400),0,32)
purple = (204,0,204)
canvas.fill(purple)
pygame.display.update()
#display is now ON...
It should be:
pygame.init()
so i asssume that:
pygame.quit()
works the same
Have you tried calling just pygame.quit() or pygame.init()? I don't believe there is a pygame.display.quit().