so i've changed the window color to a white color, unfortunately its not updating, it also returns an error
code:
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display = pygame.display.set_caption('PyGame Test')
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__ == '__main__':
main()
what am i doing wrong?
In order to set the caption of the window, you need to type pygame.display.set_caption("Caption"). In your code, I see that you typed pygame.display = pygame.display.set_caption('PyGame Test'), which results in an AttributeError because pygame.display.set_caption() returns None, meaning you set pygame.display to None. So if you replace that line with pygame.display.set_caption('PyGame Test'), it will work.
Full code:
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('PyGame Test')
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__ == '__main__':
main()
For more info, check this page
I believe you need to initialize the display module. Try pygame.init()
http://www.pygame.org/docs/ref/display.html#pygame.display.init
Related
I have python 3.10.5 and Pygame 2.1.2. I dont know whats happening, but my window is not showing up. i have tried changing the code a bit, but nothing seems to be working.
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
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 tried out your initial code and found some bits missing as noted in the comments. I did not see any reference to what color would be used for filling your screen along with the missing "display.flip(). Keeping the spirit of your initial code as much intact as possible, following is a code snippet that does present a white game window.
import pygame
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill((255, 255, 255)) # Added this
pygame.display.flip() # And this
pygame.quit()
if __name__ == '__main__':
main()
You might also want to check out the following link for other tips Pygame Primer.
I already checked other topics and I had tested these solutions and without any pos results.. I need to refresh topic with question. Where I have an error?
import pygame
pygame.init()
WIDTH, HEIGHT = 1000, 800
window = pygame.display.set_mode((WIDTH, HEIGHT))
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
clock.tick(fps)
pygame.quit()
if __name__ == "_main_":
run(window, WIDTH, HEIGHT)
Change from single underscore to double underscores around main here:
if __name__ == "_main_":
It should be
if __name__ == "__main__":
The quit invocation should not be on the while block but after it. Otherwise it quits immediately.
Therefore:
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
clock.tick(fps)
pygame.quit()
It is a matter of Indentation. The window is closing because you are calling pygame.quit() in the application loop. You need to call pygame.quit() after the application loop.
The name of the top level code environment is "__main__", but not "_main_":
def run(window, width, height):
run = True
clock = pygame.time.Clock()
fps = 60
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
clock.tick(fps)
# INDETATION
#<--|
pygame.quit()
if __name__ == "__main__": # <--
run(window, WIDTH, HEIGHT)
I was trying to draw an image onto the screen but I keep getting errors. The error keeps saying "video system not initialized". I am new to python, can anyone help?
import pygame
import os
#game window
WIN = pygame.display.set_mode((1000, 800))
NAME = pygame.display.set_caption("Space War!")
#FPS limit
FPS = (60)
#image i am trying to draw onto screen
SPACE_BACKGROUND = pygame.image.load(os.path.join('space_background.png'))
pygame.init()
#allows pygame to quit
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
pygame.display.update()
#calling def main(): function
if __name__ == "__main__":
main()
do:
def main():
clock = pygame.time.Clock()
run = True
FPS = 60
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
WIN.blit(SPACE_BACKGROUND, (0, 0)) #you FORGOT this part
pygame.display.update()
BTW the main function cannot access the global 'FPS' variable so declare that within the 'main' function(make it local).
import pygame
import os
WIDTH , HEIGHT = 1050,600
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("ok")
WHITE = (255,255,255)
FPS = 60
CHARACTER_SIZE = (100,100)
CHARACTER_IMAGE = pygame.image.load(os.path.join('Models','character.png'))
CHARACTER = pygame.transform.scale('character.png',CHARACTER_SIZE)
def draw_window():
WIN.fill(WHITE)
WIN.blit(CHARACTER,(0,300))
pygame.display.update()
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit
if __name__ == "__main__":
main()
After running this I get this error:
argument 1 must be pygame.Surface, not str
reffering to this line of code:
CHARACTER = pygame.transform.scale('character.png',CHARACTER_SIZE)
What s the issue?
You can't transform a string. The first argument has to be the CHARACTER_IMAGE variable.
Ask for any more information if needed!
I am using vscode's ide and python 3. When I run script.py the display will pop-up, but a half of a second later the display will disappear and will give this error pyagame.error: Video not intiialized.
import pygame
pygame.init()
run = True
while run:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
pygame.quit()
x = 250
y = 250
width = 40
height = 60
vol = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT():
run = False
run = False
pygame.QUIT()
You do pygame.quit() immediately after pygame.display.set_mode(). pygame.quit() terminates all pygame modules. Remove it:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
# pygame.quit() <--- DELETE
pygame.QUIT is not a function, it is an enumerator constant. You can't invoke pygame.QUIT:
if event.type == pygame.QUIT():
if event.type == pygame.QUIT:
You need just one application loop, not 2 of them. Furthermore you have to update the window by either pygame.display.flip() or pygame.display.update()
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
x, y = 250, 250
width, height = 40, 60
vol = 5
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear dispaly
screen.fill((0, 0, 0))
# draw the scene
pygame.draw.rect(screen, (255, 0, 0), (x, y, width, height))
# update display
pygame.display.flip()
pygame.quit()