[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?



I will not give you the code since you have stated this in an assignment, but hopefully I can help.

You are correct that you will need an array, or a list in Python, to store the strings. I would be more concerned with ease of programming than efficiency, as this is a pretty straightforward and non- demanding application.

So, with that said, you will want your Textsprite class to take a list of strings instead of just a string. You can do this by saying:

a = Textsprite(["test string1", "another test!", "can you type this?"])

Now, in your textsprite class, you have a variable that stores the index of the current letter. You will also have to store the index of the current string, and replace all references of self.text with it, as in self.text[self.current_string]. Now, when the user completes a string, you can increment self.current_string and reset self.pos and the next string will be presented.

I hope that this is helpful for you.

Cheers,
 Brandon

On Jan 4, 2007, at 2:45 PM, Charles Christie wrote:

Hey, I am making a typing game in Python using pygame. I have finally hit a
roadblock, and I think I'm toast. I started this game as my senior project,
and I need to have something to at least demonstrate by June.


I think I screwed myself on this one. I thought I knew a reasonable amount of
programming, but absolutely none of it is coming in handy. I had to ask for
help to get what I have now, and now I've hit a road block.


So, what I need is:

A. To find a way to feed multiple strings into the program. As in, when you
finish one string, instead of starting it over again you instead go to
the next word in the list, which I guessed should be an array of
strings. I'm probably wrong, though. What's the most efficient (or
easiest) way to do this?


B. Two timers, a combo timer and a word timer that count backwards. I
know I can figure out how to do these myself, though.

The code I have so far looks like this:
**************************************************************
#Credit to scriptedfun (http://www.scriptedfun.com) for the help
#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.update()
def update(self):
self.image = pygame.font.Font(None, 36).render(self.text, 1, (0, 0, 0))
self.highlight = pygame.font.Font(None,
36).render(self.text[: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.pos]:
self.pos = self.pos + 1
self.combo = self.combo + 1
else:
self.combo = 0
if len(self.text) == self.pos:
self.pos = 0


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('It works!')
   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()
**************************************************************

I've never used a mailing list before. I'm quite new at this. Sorry if
I disobeyed a rule of conduct or did something considered
implite/noobish/stupid.