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



Thanks luke, that did it!

My code now looks like this:
**********************
#Credit to scriptedfun (http://www.scriptedfun.com) for the help with
the typing portion
#Still a very VERY barebones typing script. Next I have to work on
#the basic shoot-em-up part and get the scoring in place

import pygame
from pygame.locals import *
import helpers

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.poscmb = 0 self.stringcmb = 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 self.poscmb = self.poscmb + 1 else: self.poscmb = 0 if len(self.text[self.current_string]) == self.pos: self.pos = 0 if self.current_string +1 == len(self.text): self.current_string = 0 else: self.current_string = self.current_string + 1 self.stringcmb = self.stringcmb + 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?", "Hey Mr Ottman!", "It works!"])
   all.add(textsprite)

   #Set moving to false
   moving = 0

   # game loop
   while 1:

       # get input
       for event in pygame.event.get():
           if event.type == QUIT:
               return
           elif event.type == KEYUP:
               if event.key == K_TAB:
                   moving = 0
           elif event.type == KEYDOWN:
               if event.key == K_ESCAPE:
                   return
               elif event.key == K_TAB:
                   moving = 1
               elif moving == 0:
                   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 made the game start recording how many keystrokes and words you get
right in a row. Next I just have to tell the game to render and blit
that value onto the screen (that goes in def update(self), am I
right?) and the combo system will be done.

Next I'd implement three timer systems, one for each combo counter and
one that dictates how long you have to complete the whole list of
words. If the timer runs out, the game prints "you loose" and starts
over.

How does that sound?

On 1/23/07, Charles Christie <sonicbhoc@xxxxxxxxx> wrote:
> > It doesn't work... I can't quite figure it out. Am I missing a part?
> > It says my syntax is correct...
> syntax errors and logic errors are quite different beasts.
> just because your syntax is correct doesn't mean your program does what
> you want it to,
> just that it will do what you asked it to.
I know, I know, but what I meant to say is "I don't see the problem
and neither does the program".


> > At the very top of the file right after the import statements, I added > > a line that reads: > > > > moving = 0 > > > > After that, I changed this: > > > > def keyin(self, key): > > if key == self.text[self.current_string][self.pos] and moving > > == 0: > Don't do this. Have moving be local to the function with your main game > loop, > and pass it to keyin as an argument. Global variables just confuse the > issue. > Functions should be self-contained. > Good idea! Thank you! > HTH, > -Luke >