nothing happens after pygame image blit [duplicate] - python

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
I recently started to learn to use the pygame module and I've run into my first blocker. I an trying to paste an image of a circle in a grid that I created on top of my solid background. For some reason when I blit the image and update the screen nothing appears and I am not sure why as I am pretty sure that my code is correct. I've pasted my code below. Maybe you guys will see what I am missing.
from pygame import *
def main():
init()
display.set_caption("Othello")
screen = display.set_mode((480,480))
screen.fill((0,128,0))
draw.line(screen,(0,0,0),(60,0),(60,480))
draw.line(screen,(0,0,0),(120,0),(120,480))
draw.line(screen,(0,0,0),(180,0),(180,480))
draw.line(screen,(0,0,0),(240,0),(240,480))
draw.line(screen,(0,0,0),(300,0),(300,480))
draw.line(screen,(0,0,0),(360,0),(360,480))
draw.line(screen,(0,0,0),(420,0),(420,480))
draw.line(screen,(0,0,0),(0,60),(480,60))
draw.line(screen,(0,0,0),(0,120),(480,120))
draw.line(screen,(0,0,0),(0,180),(480,180))
draw.line(screen,(0,0,0),(0,240),(480,240))
draw.line(screen,(0,0,0),(0,300),(480,300))
draw.line(screen,(0,0,0),(0,360),(480,360))
draw.line(screen,(0,0,0),(0,420),(480,420))
display.flip()
image_white = image.load("White_Side.jpg")
image_black = image.load("Black_Side.jif")
screen.blit(image_white,(0,0))
screen.blit(image_black,(60,0))
display.update()
running = True
while running:
for pygame_event in event.get():
if pygame_event.type == QUIT:
running = False
if __name__=="__main__":
main()

Related

pygame Surface.fill doesn't work (at all) [duplicate]

This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 7 months ago.
I was trying to create a small ping-pong game with python and pygame, but I run into a huge problem: it doesnt work.
My code opens a window, but doesn't fill it. I've tried different variants of solving, but nothing seems to help. And I tried to compile this code in online compiler (Replit), but it doesn'tseem to work there neither.
import pygame
#screen initializing
WIDTH, HEIGHT = 900, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Ping-Pong')
for i in range(1000):
screen.fill((200, 200, 200))
this code just opens a black window, no matter what number I write in. What can cause fill() function to not work?
Add pygame.display.update() at the end to update the screen

Annoying error: pygame.error: video system not initialized [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
I've recently started using Pygame but I came across this error and can't seem to find a reason as to why this has happened.
My code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400,500))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.quit():
done = True
pygame.display.flip()
The error I get:
pygame.error: video system not initialized
You have made a small error write pygame.QUIT instead of pygame.quit(). This causes pygame to quit, so the error makes sense.

PyGame normal in second screen but showing black screen in laptop screen [duplicate]

This question already has answers here:
Multiple Displays in Pygame
(3 answers)
Pygame with Multiple Windows
(4 answers)
PyGame os.environ SDL_VIDEO_WINDOW_POS does not work
(2 answers)
Closed 1 year ago.
I'm implementing a code that shows up an image in fullscreen size, using PyGame. I have two screens in my setup, one monitor and the one of the laptop. When I run my code to show on the monitor all works fine, but when I try it on the laptop screen something crash, and only a black screen is shown. My code is as follows:
def open_window2(path, key):
# key dict
key_dict={'a':K_a, 'b':K_b, 'c':K_c, 'd':K_d, 'e':K_e, 'f':K_f,
'g':K_g, 'h':K_h, 'i':K_i, 'j':K_j, 'k':K_k, 'l':K_l,
'm':K_m, 'n':K_n, 'o':K_o, 'p':K_p, 'q':K_q, 'r':K_r,
's':K_s, 't':K_t, 'u':K_u, 'v':K_v, 'w':K_w, 'x':K_x,
'y':K_y, 'z':K_z}
pygame.init()
# get screen resolution
resolution = (pygame.display.Info().current_w, pygame.display.Info().current_h)
surface = pygame.display.set_mode(resolution, FULLSCREEN)
image = pygame.image.load(path)
image = pygame.transform.scale(image, resolution)
rect = image.get_rect(); rect.center = resolution[0]//2, resolution[1]//2
surface.blit(image, rect)
pygame.display.update()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN: #press-key events
if event.key == key_dict[key]: # if C key is pressed ...
running = False
pygame.quit()
When I set it to pygame.FULLSCREEN shows a black screen, but when I set it to pygame.RESIZABLE the image is shown.
What should I do to fix the black screen problem?
I've found a solution. I don't know how pyglet.FULLSCREEN flag works but there is another way to do that without using it. You can just join two flags, pyglet.SCALED and pyglet.NOFRAME and it should work well.
surface = pygame.display.set_mode(resolution, pygame.SCALED|pygame.NOFRAME).
pygame.SCALED refers to adjust to the fullscreen mode, and pygame.NOFRAME removes bars and other showing parts.
It will plot the image you want on the entire screen, without showing bars, menus, etc.

Python adjusting the fullscreen window in different screens [duplicate]

This question already has answers here:
The fullscreen mode in pygame is entirely black
(1 answer)
Switching to Pygame Fullscreen Mode working only one time
(1 answer)
Closed 2 years ago.
I am developing a game and I went into a problem which I could not solve. I want to have a pygame screen which adjusts in every screen, because you can see more of the game background when you have a very large screen and when you have a small screen, you will only see a small piece of the game. I dont know how to fix this. (If it is possible, it would be nice when I can scale every object together)
Code:
pygame.init()
os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"
display_info = pygame.display.Info()
MONITOR_SIZE = width, height = pyautogui.size()
pygame.display.set_caption("Stickman fight")
screen = pygame.display.set_mode(MONITOR_SIZE, pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.FULLSCREEN, 32)
bgs = [pygame.transform.scale2x(pygame.image.load("src/Img/Bg1.jpg").convert_alpha())]

Pygame.display.update() disappearing [duplicate]

This question already has answers here:
Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.
(2 answers)
Closed 7 years ago.
We have recently moved our school lab to Mac, and when I run some code to display an image from a file, the display appears then disappears immediately. I am using the 3.4 interpreter in Pycharm and Pygame version 1.9.2. Can someone please help?
Here is my code:
# displays a hard-coded filename in a window
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0, 0))
pygame.display.update()
If the code you posted is the complete code, then that is the reason why its exiting immediately, because immediately after calling pygame.display.update() , the program exits. I do not think this should have been working before.
One thing you can do for this , add a loop to run till the user presses close, after your code -
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Put a
time.sleep(10)
after pygame.display.update() see if it stays up longer.

Categories

Resources