[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?
At long last! I have it working perfectly!
*****************************************
#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 *
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.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
if self.current_string +1 == len(self.text):
self.current_string = 0
else:
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?", "Hey Mr Ottman!", "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'm so ecstatic! Weeeeeee! Thanks guys, you are a great help! Now,
what to work on next? I suppose the next part would be the basic
shoot-em-up part of the engine? (I'll put that in another python file
so I don't screw this one up) And then I'll do scoring. Well, at
least, that's a very basic and not-detailed roadmap for myself. I made
a much better one on my computer...
On 1/19/07, Charles Christie <sonicbhoc@xxxxxxxxx> wrote:
No. I guess I should, huh? Well, whatever. In any case, I've almost
got this thing working. But I forgot to add a line that resets the
program after the last word is typed... It gives some sort of "list
out of index" something or other error. I'm working on it... What I
need to do now is calculate the length of the list - I assume that it
is self.text as self.text[self.current_string] pulls a word out of the
list.
I tried this:
if len(self.text) == self.current_string:
self.current_string = 0
self.pos = 0
without success. Time to get to my next class, though, so I'll
troubleshoot this later...
On 1/17/07, Brandon N <kordova@xxxxxxxxx> wrote:
> Great progress!
>
> One question, are you backing up your code in between "breaking" it?
>
> On Jan 17, 2007, at 11:43 AM, Charles Christie wrote:
>
> > 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!
>
>