Pygame in StudioCode on mac wont blit [duplicate] - python

This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I cant blit anything to my pygame screen using studio code on my mac. Is this a known issue or is there a way to fix it I am overlooking? I'm not getting any errors it's just not doing anything. I am kinda new to pygame so anything could work. here's my code:
pygame.display.set_caption('The space Simulator')
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
screen = pygame.display.set_mode([1000, 1000])
background = pygame.Surface((1000,1000))
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = background.get_rect().centerx
running=True
while running:
screen.blit(image, (textpos))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

You're just not flushing / updating the drawing primitives to the screen. Really you just need a pygame.display.update() or pygame.display.flip() after all your blits are done.
I guess you removed parts of your code to keep it simple for the question, but I put them back in for a working answer.
I also re-arranged the code, and removed the creation of the background Surface just to get the centre co-ordinate. This operation can be performed on the existing screen Surface.
import pygame
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
pygame.init()
pygame.display.set_caption('The space Simulator')
screen = pygame.display.set_mode([1000, 1000])
#image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
image = pygame.image.load('background.png' ).convert()
image = pygame.transform.smoothscale( image, (1000,1000) )
#background = pygame.Surface((1000,1000))
myfont = pygame.font.SysFont( None, 24 )
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx
running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(image, (0,0))
screen.blit(text1, (textpos))
pygame.display.flip()
pygame.quit()

Related

It wont draw my rectangle (pygame on prdroid)

The screen wont fill and it says there is no fill cmd for screen, what did I do wrong?
import pygame
pygame.init()
screen = pygame.display
set_mode((0, 0))
white = (255, 255, 255)
Square =(0, 0, 0)
loop = True
while loop:
screen.fill(white)
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.display.update()
There are some syntax errors in your code:
screen = pygame.display
set_mode((0, 0))
screen = pygame.display.set_mode((0, 0))
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
More than that, the event loop is missing. This can cause your application to freeze. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Correct program:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
white = (255, 255, 255)
Square = (0, 0, 0)
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
screen.fill(white)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
pygame.display.update()

Python pygame need help on drawing rectangles

Im trying to draw a rectangle using python's pygame module and i don't really know what im doing wrong
import pygame
import sys
white = 255, 255, 255
red = 255, 0, 0
blue = 0, 0, 255
green = 0, 255, 0
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Rectangle test')
isRunning = pygame.get_init
while isRunning:
pygame.display.update()
pygame.event.get()
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
print(pygame.mouse.get_pos())
pygame.draw.rect(win, blue, (269, 165, 15, 15), 1)
win.fill((255, 255, 255))
pygame.quit()
Thats the code that im currently using, it opens up the window, the window fill works as it should and the event.get also works, it prints what it should print but it doesnt draw any rectangle
Your fill of the window is wiping out your rectangle. Just make the two calls in the opposite order:
win.fill((255, 255, 255))
pygame.draw.rect(win, blue, (269, 165, 15, 15), 1)
and you'll see your rectangle. I do when I run your code with this change applied.

I can't draw a circle with pygame in python 3.7.7 [duplicate]

This question already has answers here:
Why is my pygame application loop not working properly?
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I've look for the docs, many video on YouTube, questions on stackoverflow but I still can't fix it.This is my code:
import pygame
pygame.init()
run = True
#color def
BK = (0, 0, 0)
WT = (255, 255, 255)
GY = (127, 127, 127)
#create screen
screen = pygame.display.set_mode((800,600))
#title, icon
pygame.display.set_caption("Clock")
icon = pygame.image.load("clock.png")
pygame.display.set_icon(icon)
#loop
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.circle(screen, WT, (0, 0), 175, width=1)
You missed to update the display by by either pygame.display.update() or pygame.display.flip(). And you need to draw circle in the application loop. Care about the Indentation:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#-->| INDENTATION
# clear display
screen.fill(0)
# draw scene (circle)
pygame.draw.circle(screen, WT, (0, 0), 175, width=1)
# update display
pygame.display.flip()

Segmentation Fault with opening and closing Pygame WIndows

I'm working on a project where I am trying to alternate between the camera preview on a picamera, and some text on screen using pygame windows. I have gotten to the point where I can open the picamera, then some text, then the picamera again, but when I try to open a pygame window for more text, I get a segmentation fault.
I think the main problem is quitting the pygame window, without quitting the other things I need to open up another pygame window. The commands like sys.exit, and pygame.quit seem to quit things to much. I've tried alternatives like putting the text into a while loop and then making the loop false at the end so that it closes the window without an actual quit command, but that didn't seem to close anything really. The code works perfectly up until the second time I try to initialize pygame. That's when it gives me the segmentation fault and opens a new window in my python idle with a whole bunch of other code that I didn't write.
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
camera()
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('You', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
pygame.quit()
camera()
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('test', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
What I would like to be able to do is switch between picamera and text several more times, so if I can figure out how to fix it this one time, then I just have to copy and paste the code a bit more to get the next iterations. I'm brand new to coding.
Ok. so I made a silly mistake. The problem is, for some reason, initializing pygame twice; which I still don't understand since pygame.quit() should have quit it I think. But I just removed the second pygame.init() and replaced the first pygame.quit() with pygame.display.quit() instead.

Pygame not responding when trying to show texts on macOS Sierra

I've installed pygame version 1.9.4 on python 3.5.3 using instructions on programarcadegames.com. When I use pygame to draw lines and shapes it's just fine but when I try to use pygame.font.SysFont the pygame is not responding anymore. For example when I run following code a non-responsive pygame window will show up:
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
size = (400, 500)
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
screen.fill(WHITE)
font = pygame.font.SysFont(None, 25, True, False)
text = font.render("Some Text", True, BLACK)
screen.blit(text, [0, 0])
pygame.display.flip()
clock.tick(60)
pygame.quit()
Replacing font = pygame.font.SysFont(None, 25, True, False) with font = pygame.font.Font(None, 25) fixed the problem, but it is still unclear what caused it.

Categories

Resources