Just started a Python course at my school, and our first assignment requires us to write out text on multiple lines. I still cannot figure out how to do so with my code so if anyone could help, that'd be great. Code posted below.
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption('font example')
size = [640, 480]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
myfont = pygame.font.SysFont(None, 48)
text = myfont.render('My Name', True, (0, 0, 0), (255, 255, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery
screen.fill((255, 255, 255))
screen.blit(text, textrect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(20)
simply you can start new line after each print or sys stdout stream output by specify "\n" (ex: print ("hello" + "\n"
then your application will write to diffrent lines
Related
Why does my text not appear? I am trying to make a program in which the text is shown above the image. In the program, I still see the image, but not the text.
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((800, 600))
image1 = pygame.image.load('frog.gif')
image2 = pygame.image.load('frog.jpg')
pygame.display.set_caption('\'Dancing frog\'')
image1 = pygame.transform.scale(image1, (800, 800))
image2 = pygame.transform.scale(image2, (800, 800))
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render('this should appear', True, (255, 255, 255))
screen.blit(text, [10, 10])
run = True
while run:
screen.blit(image1, (0, -200))
pygame.display.update()
time.sleep(0.2)
screen.blit(image2, (0, -200))
pygame.display.update()
time.sleep(0.2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit() ```
You must blit the text Surface after the bbackgorund image:
screen.blit(image1, (0, -200))
screen.blit(text, [10, 10])
pygame.display.update()
I am trying to change the default color from black to white.
not to fill it.
My current code where I fill it:
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
It works however it is being shown as black for a second and then it changes itself, and i am looking for a solution where in advance it starts with white background. Thanks in advance
Maybe this will help you
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
surface.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
By #starbuck45 I used the flag pygame.HIDDEN.
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE | pygame.HIDDEN)
surface.fill("white")
pygame.display.set_mode(size)
line = pygame.draw.line(surface, "black", (0, 0), (600, 600))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
and it worked fine!
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()
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.
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.