[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Newbie needs help, what's the most efficient (or easiest) way to do this?



Good news everyone! I found out why I could never get anywhere:

I was too afraid of breaking stuff so I didn't try to mess with
anything. However, if you don't break it, how can you fix it? So, I
looked over that pygame thing again and decided to just start doing
things that came to mind. It took me five minutes to get it working,
much faster than if I had begged someone to tell me how to do it.
Python really is a simple language after all, I think I really
understand this now! This is what my code looks like now:

*******************************************
#Credit to scriptedfun (http://www.scriptedfun.com) for the help with
the typing portion
#Stil a very VERY barebones typing script. Still got one last part of the main
#engine to go before getting to work on part two.

import pygame
from pygame.locals import *

SCREENRECT = Rect(0, 0, 640, 480)

class Textsprite(pygame.sprite.Sprite):
   def __init__(self, text):
       pygame.sprite.Sprite.__init__(self)
       self.text = text
       self.pos = 0
       self.current_string = 0
       self.update()
   def update(self):
       self.image = pygame.font.Font(None,
36).render(self.text[self.current_string], 1, (0, 0, 0))
       self.highlight = pygame.font.Font(None,
36).render(self.text[self.current_string][:self.pos], 1, (0, 0, 255))
       self.image.blit(self.highlight, (0, 0))
       self.rect = self.image.get_rect()
       self.rect.center = pygame.display.get_surface().get_rect().center
   def keyin(self, key):
       if key == self.text[self.current_string][self.pos]:
           self.pos = self.pos + 1
       elif key == K_TAB:
           self.pos = self.pos
       else:
           self.combo = 0
       if len(self.text[self.current_string]) == self.pos:
           self.pos = 0
           self.current_string = self.current_string +1

def main():
   pygame.init()

   screen = pygame.display.set_mode(SCREENRECT.size)

   # make background
   background = pygame.Surface(SCREENRECT.size).convert()
   background.fill((255, 255, 255))
   screen.blit(background, (0, 0))
   pygame.display.update()

   # keep track of sprites
   all = pygame.sprite.RenderUpdates()

   # keep track of time
   clock = pygame.time.Clock()

   textsprite = Textsprite(["test string1", "another test!", "can you
type this?"])
   all.add(textsprite)

   # game loop
   while 1:

       # get input
       for event in pygame.event.get():
           if event.type == QUIT:
               return
           elif event.type == KEYDOWN:
               if event.key == K_ESCAPE:
                   return
               else:
                   textsprite.keyin(event.unicode)

       # clear sprites
       all.clear(screen, background)

       # update sprites
       all.update()

       # redraw sprites
       dirty = all.draw(screen)
       pygame.display.update(dirty)

       # maintain frame rate
       clock.tick(30)

if __name__ == '__main__': main()
*******************************************

Now, there's just one problem, and I'm pretty sure you can see it:
After the program goes through all the strings, it doesn't stop at the
last one and tries to add one again. Which won't work, and the program
will print an error about it going out of bounds. I'm working on that
now, it should take no less than five or ten minutes to figure out.
Thanks for all your help!