LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2023, 06:38 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
python pygame


why isn't this code moving each text down 10 pixels, i feel like it should be printing text on different lines.
I want each type of text to print 10 pixels lower then that last but its not working!
Code:
#!/usr/bin/python3

import pygame

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((600,600))
print(pygame.font.get_fonts())

pygame.display.set_caption("Let's do some text!");

font = []
text = []
textrect = []
i=0

for fonts in pygame.font.get_fonts():
    font.insert(i, pygame.font.SysFont(fonts,50,italic=True,bold=True))
    text.insert(i, font[i].render("Hello Everyone",True,(255,255,255)))
    textrect.insert(i, text[i].get_rect())
    #textrect[i].center=((600/2, 600/2))
    i+=1

i=0
pos = [0,0]

for fonts in pygame.font.get_fonts():
    textrect[i].move(pos)
    pos[1] = pos[1]+10
    i+=1

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
                quit()

    screen.fill((131,97,97))
    i=0
    for fonts in pygame.font.get_fonts():
        screen.blit(text[i], textrect[i])
        i+=1

    pygame.display.update()
Special note this code below i think is what should be moving the text pixels before they are each printed but it doesnt seem to be moving the text at all i cant figure out why.
Code:
for fonts in pygame.font.get_fonts():
    textrect[i].move(pos)
    pos[1] = pos[1]+10
    i+=1
 
Old 04-22-2023, 07:37 PM   #2
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
ok i did this and commented out that special code:
Code:
    for fonts in pygame.font.get_fonts():
        screen.blit(text[i], pos)
        pos[1]= pos[1]+35
        i+=1
instead of:
Code:
    for fonts in pygame.font.get_fonts():
        screen.blit(text[i], textrect[i])
        i+=1
now it works the way I want it to but what if I want to scroll the screen to all the other texts?
heres the working code:
Code:
#!/usr/bin/python3

import pygame

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((600,1000))
print(pygame.font.get_fonts())

pygame.display.set_caption("Let's do some text!");

font = []
text = []
textrect = []
i=0

for fonts in pygame.font.get_fonts():
    font.insert(i, pygame.font.SysFont(fonts,50,italic=True,bold=True))
    text.insert(i, font[i].render(fonts,True,(255,255,255)))
    textrect.insert(i, text[i].get_rect())
    #textrect[i].center=((600/2, 600/2))
    i+=1

i=0

#for fonts in pygame.font.get_fonts():
#    textrect[i].move(pos)
#    pos[1] = pos[1]+10
#    i+=1

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
                quit()

    screen.fill((131,97,97))
    i=0
    pos = [0,0]
    for fonts in pygame.font.get_fonts():
        screen.blit(text[i], pos)
        pos[1]= pos[1]+35
        i+=1

    pygame.display.update()
 
Old 04-22-2023, 08:06 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
nevermind i figured it all out here's the working code:
Code:
#!/usr/bin/python3

import pygame

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((900,1000))
intermediate = pygame.surface.Surface((800, 9000))
i_a = intermediate.get_rect()
print(pygame.font.get_fonts())

pygame.display.set_caption("Let's do some text!");

font = []
text = []
textrect = []
i=0

for fonts in pygame.font.get_fonts():
    font.insert(i, pygame.font.SysFont(fonts,50,italic=True,bold=True))
    text.insert(i, font[i].render(fonts,True,(255,255,255)))
    textrect.insert(i, text[i].get_rect())
    #textrect[i].center=((600/2, 600/2))
    i+=1

i=0
j=0
#for fonts in pygame.font.get_fonts():
#    textrect[i].move(pos)
#    pos[1] = pos[1]+10
#    i+=1
scroll_y=0
y=0
for fonts in pygame.font.get_fonts():
    intermediate.blit(text[i],(10,y))
    i+=1
    y+=70

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
                quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4: scroll_y = min(scroll_y + 70, 0)
            if event.button == 5: scroll_y = max(scroll_y - 70, -9000)

    screen.fill((131,97,97))
    i=0
    if j < 900:
        j+=1
    else:
        j=0

    pos = [j,0]
    for fonts in pygame.font.get_fonts():
        screen.blit(text[i], pos)
        pos[1]= pos[1]+70
        i+=1
    
    screen.blit(intermediate, (0,scroll_y))

    pygame.display.update()

Last edited by errigour; 04-22-2023 at 08:12 PM.
 
Old 04-22-2023, 08:12 PM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
keep in mind you need to type pip install pygame for this to work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Top 5: Politics of the Linux desktop, learning Python with Pygame, and more LXer Syndicated Linux News 0 12-02-2017 07:21 PM
LXer: Why Python and Pygame are a great pair for beginning programmers LXer Syndicated Linux News 0 11-28-2017 07:33 AM
Creating python + pygame binaries on Slackware 13.37 problem. Braqoon Slackware 3 01-12-2012 05:48 PM
[SOLVED] installing Pygame for Python on Mint8 ldmn Linux Mint 3 06-10-2010 03:38 AM
Python + Pygame: Making a typing game, is it difficult? sonicbhoc Programming 2 11-26-2006 06:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration