Why won't pygame display my text? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My code looks like this, and it opens up the display alright and makes the background white, but it refuses to show my text. It dosnt give any errors either.
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
gameDisplay = pygame.display.set_mode((1250,700))
white = (255, 255, 255)
def text_objects(text, font, color):
textSurface = font.render(text, True, color,)
return textSurface, textSurface.get_rect()
def letter_input():
gameDisplay.fill(white)
smallText = pygame.font.Font("txt.ttf", 20)
textSurf, textRect = text_objects ("Please input something to say", smallText, black)
textRect.center = (11250/2), (700/2)
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
letter_input()
pygame.display.set_caption("Sentence printer")
clock = pygame.time.Clock()
Quite frankly, I'm just confused at why it won't do this.

textRect.center = (11250/2), (700/2)
Here you set the topleft coordinates of the rect to (5625.0, 350.0) and that's outside of your gameDisplay. Try for example textRect.center = (500, 300) instead.

Related

How do i delete the last number that is being shown? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am trying to make a game that has a cookie in the middle and if you click that cookie, your points increase
My problem is that once i display the points and increase it, the numbers overlap
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((1000,500))
pygame.display.set_caption("Cookie Clicker")
clock = pygame.time.Clock()
screen.fill("White")
font1 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 25)
font2 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 30)
font3 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 35)
font4 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 40)
font5 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 45)
font6 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 50)
noc = 0
# Texts
nuofco = font4.render(str(noc), False, "Black")
nuofco_r = nuofco.get_rect(center = (237,80))
# Drawn Rects
cookie = pygame.draw.ellipse(screen, "Brown", (110,150,250,250))
pygame.draw.rect(screen, "Black", pygame.Rect(475,0,25,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie.collidepoint(event.pos):
noc += 1
screen.blit(nuofco,nuofco_r)
pygame.display.update()
clock.tick(60)
After calculating the new number I think you need to redraw everything on the screen including the background. Because everything you draw on the screen is actually drawn on top of the previous one. And everything overlaps when the background is not drawn.
And also in the main loop, the usually recommended order is to take input, update variables, draw everything. It would be better for you if you first calculate everything that needs to be calculated and then draw what needs to be drawn.

PNG File Can't Open [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to make a game using python and pygame but for some reason or another pycharm isn't able to open my png file of a race car and I get this error-
Traceback (most recent call last):
File "C:/Users/Zack's PC/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/main1.py", line 14, in <module>
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
pygame.error: Couldn't open :/Users/Zack's PC/Pictures/gameimages/racecarimage.png
Here is the code
import pygame
pygame.init()
display_width= 900
display_height=600
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
screen= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Race Car')
clock = pygame.time.Clock()
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_height * 0.45)
y = (display_width * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
car(x,y)
gameDisplay.fill(white)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
You forgot the drive letter, change:
Img = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
to:
Img = pygame.image.load('C:/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')

Invalid Syntax on this code, no idea how to fix [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
No idea why the syntax is invalid, anyone have an idea? It says the two bottom lines of code in particular have invalid syntax. This is probably embarassingly simple to fix, so please have mercy on my soul.
I haven't tried anything because it's an extremely basic problem and I'm probably just bad at coding
import pygame
import sys
import time
pygame.init()
(width, height) = (1920, 1080)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('fat')
background_colour = pygame.Color('white')
color = pygame.Color('dodgerblue2')
font = pygame.font.Font(None, 100)
clock = pygame.time.Clock()
running = True
text = ''
while running:
# handle events and user-input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if (event.key >= pygame.K_SPACE and event.key <= pygame.K_z):
# Append key-stroke's character
text += event.unicode
elif (event.key == pygame.K_BACKSPACE):
text = text[:-1]
elif (event.key == pygame.K_RETURN):
#print("interpret(text) - NOT IMPLEMENTED")
text = ""
if len(text) > 20:
text = text[:-1]
# repaint the screen
screen.fill(background_colour)
txt_surface = font.render(text, True, color)
screen.blit(txt_surface, (50, 100))
response = font.render(Room.defaultprompt, True, color)
screen.blit((response,(80, 150))
clock.tick_busy_loop(60) # limit FPS
display.flip()
The code did, but no longer, takes what the user types and presents it onto the screen. It stopped working after I tried to make pygame draw another line of text. (btw I know that Room.defaultprompt is undefined but that's because the rest of the code just isn't in the post)
Looking closely to it, so an error on this line:
screen.blit((response,(80, 150))
When it should be:
screen.blit(response,(80, 150))
#ErikXIII's answer is little different

python and pygame font centering is working but I don't know why

My code is working perfectly although I'm completely in the dark as to why.
my code is:
def textobjects(message, font,color):
Textsurface = font.render(message, True, color)
return Textsurface, Textsurface.get_rect()
def Display_text(message,color):
Heading = pygame.font.SysFont("Arial",70)
TextShown, Textrect = textobjects(message, Heading,color)
Textrect.center = ((displaywidth/2),0)
gameWindow.blit(TextShown,Textrect)
pygame.display.update()
time.sleep(2)
game_loop()
def border():
Display_text("You hit the border",wine)
and I don't know why I was able to do:
Textrect.center = ((displaywidth/2),0)
without having to define ".center". Is .center a pygame/python predefined module?
.center is a part of a rect object that you can both get and set
You can also do
Rect.left
Rect.top
Rect.right
Rect.bottom
Rect.topleft
Rect.bottomleft
Rect.topright
Rect.bottomright
Rect.centerx
Rect.centery

Pygame transparency issue

I have a rather confusing problem. I currently have a countdown and I would ideally have the text from the countdown to be somewhat transparent (say 50% or so)
pygame.init()
surface = pygame.display.set_mode((0,0))
fontObj = pygame.font.Font('freesansbold.ttf', 600)
textSurfaceObj = fontObj.render("3", True, (255, 255, 255))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (surface.get_width() / 2, surface.get_height() / 2)
pygame.mouse.set_visible(False)
while True:
surface.fill(255, 255, 255)
pygame.display.update()
time = str(datetime.datetime.now())
filename = 'photos/' + time.replace(' ', '_') + '.jpg'
for i in xrange(3, 0, -1):
surface.fill(WHITE)
textSurfaceObj = fontObj.render(str(i), True, (255, 0, 0)
surface.blit(textSurfaceObj, textRectObj)
pygame.display.update()
pygame.time.wait(1000)
pygame.display.update()
pygame.time.wait(100);
surface.fill(WHITE)
pygame.display.update()
I've tried putting in surface.set_alpha(50) but that doesnt seem to do anything. Any suggestions are very welcome!
In pygame you must set a background color for antialiased text if you want it to use a color key (which allows you to render transparent text). To get the result you want you just need to match the background color to the surface color, in this case white. The below code (starting right after the for i in ...) worked for me:
surface.fill(WHITE)
textSurfaceObj = fontObj.render(str(i), True, (255, 0, 0),WHITE)
textSurfaceObj.set_alpha(50)
surface.blit(textSurfaceObj,(0,0))
pygame.display.update()
pygame.time.wait(500)
Edit: I did a little digging as to what's going on here, and this mailing list post details the backend properties that cause this for anyone interested.

Categories

Resources