From 9886b8e6f4d4a239a96d7fec2652f00fc0e8fecb Mon Sep 17 00:00:00 2001 From: Silight Date: Sun, 2 Feb 2014 01:15:44 -0500 Subject: [PATCH 1/3] Trying to fix shutdown error. Fixed bad dont path and several small things. --- TypingGame.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/TypingGame.py b/TypingGame.py index a24aee7..0b8e5ba 100644 --- a/TypingGame.py +++ b/TypingGame.py @@ -54,10 +54,10 @@ def update(self): if old_top < height/4 and self.rect.top >= height/4: extra_words.append(spawn_word()) if self.rect.bottom >= height: - lose() - def lose(): - print "YOU LOSE! Your score is:",score running = False + + def lose_text(): # lose text + lose_text = ("YOU LOSE! Your score is: " + str(score)) running = True speed = 10 @@ -68,8 +68,13 @@ def lose(): wordfile.close() score = 0 -score_font = pygame.font.Font("score_font.ttf",60) +score_font = pygame.font.Font("score_font.TTF",60) +# set font and color for lose text - silight +fontFill = pygame.font.Font("score_font.TTF",60) +textSurfaceLose = fontFill.render(lose_text(), True, (120, 255, 120), (0,0,0)) +loseTextRect = textSurfaceLose.get_rect() +loseTextRect.center = (200, 20) background = pygame.image.load("background.png").convert() @@ -108,5 +113,6 @@ def lose(): screen.blit(i.image, i.rect) pygame.draw.line(screen,(0,255,0),(width/2, height),(currentword.rect.left+7, currentword.rect.bottom),14) screen.blit(currentword.image, currentword.rect) #draw the word + screen.blit(textSurfaceLose, loseTextRect)# draw lose message - silight pygame.display.flip() #apply the changes pygame.quit() #fix the program breaking in IDLE From 5e6e0b94b546f64e06266f914fc95d1e08db3ae9 Mon Sep 17 00:00:00 2001 From: Silight Date: Sun, 2 Feb 2014 18:38:01 -0500 Subject: [PATCH 2/3] Added intro and started to fix game exit. --- TypingGame.py | 130 +++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 48 deletions(-) diff --git a/TypingGame.py b/TypingGame.py index 0b8e5ba..08abe4d 100644 --- a/TypingGame.py +++ b/TypingGame.py @@ -1,5 +1,8 @@ #BUG: On Mac, if the game exits for any reason it leaves a pygame Dock icon which must be force-quit. -import pygame, random +# Potential Enhancements: High score board, Opening splash screen, Instructions, Count Down before start, +# Adjustable screen size (at least the option to go full screen) - silight +import pygame, random, sys +from easygui import * # I know it increases the prereqs by I think this will help give the game a little extra polish. - silight pygame.init() width,height = (800,600) #this is short for width=800 and height=600 @@ -10,6 +13,60 @@ def spawn_word(): wordStr = random.choice(words).strip() return TypingGameWord(wordStr) +def intro(): # Introduction to the game. Gives instructions on how to play - silight + msg = """ + Welcome to WORD BLASTER! + + The war against the Nation of Grammar Nazis is going badly. They have developed a new weapon that is haveing devestating effects on the population of Facebookia. This is a bomb that can only be shot down through spelling the word on the bomb correctly. Needless to say, the Grammar Nazis are winning. + + You have been recruited to fight against this final assault on the free speaking Facebookian nation. With latest in l337 LZeR technology at your fingertips you are supposed to destroy the bombs before the free-speaking, language butchering people of Facebookia are destroyed forever. + + Also, the Grammar Nazis kidnapped your dog. So, screw them. + + Finger to home keys, when you are ready hit okay. + """ + title = "WORDBLASTER" + msgbox(msg, title) + mainLoop() + +def mainLoop():#the main loop + global running, speed, wordfile, words, currentword, extra_words, score + while running: + clock.tick(20) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False #stops the program + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + running = False #stops the program + else: + if currentword.checkLetter(event.unicode): #event.unicode is the letter the user typed + speed += 3 + score += 1 + if len(extra_words) > 0: + lowestwordindex = 0 + for i in range(len(extra_words)): + if extra_words[i].rect.bottom > extra_words[lowestwordindex].rect.bottom: + lowestwordindex = i + currentword = extra_words.pop(lowestwordindex) + + else: + currentword = spawn_word() + currentword.update() + for i in extra_words: + i.update() + + score_surf = score_font.render("SCORE:"+str(score), True, (0,255,0)) + + screen.fill((0,0,0)) #clears the screen + screen.blit(background,(0,0)) + screen.blit(score_surf,(0,530)) + for i in extra_words: + screen.blit(i.image, i.rect) + pygame.draw.line(screen,(0,255,0),(width/2, height),(currentword.rect.left+7, currentword.rect.bottom),14) + screen.blit(currentword.image, currentword.rect) #draw the word + pygame.display.flip() #apply the changes + class TypingGameWord(pygame.sprite.Sprite): "Represents a word that the user will have to type" @@ -43,7 +100,7 @@ def updateSurface(self): def update(self): global height "Called every frame to update the state of the word." - global speed, running,score, extra_words, currentword + global speed, running,score, extra_words, currentword, losestmt speedCoefficient = len(self.originalWord) if speedCoefficient < len(currentword.originalWord): speedCoefficient = len(currentword.originalWord) @@ -54,10 +111,25 @@ def update(self): if old_top < height/4 and self.rect.top >= height/4: extra_words.append(spawn_word()) if self.rect.bottom >= height: - running = False - - def lose_text(): # lose text - lose_text = ("YOU LOSE! Your score is: " + str(score)) + msg = """ + Congratulations, you scored %s + + Did you want to play again? + """ % score # Congratz instead of 'you lose' to leave on a positive note. - silight + title = "Game Over" + choice = ynbox(msg, title) + if choice == 1: # reset the game. Object is still detected at the bottom of the screen. - silight + speed = 0 + score = 0 + score_surf = score_font.render("SCORE:"+str(score), True, (0,255,0)) + screen.fill((0,0,0)) #clears the screen + screen.blit(background,(0,0)) + screen.blit(score_surf,(0,530)) + pygame.display.flip() + intro() + else: + running = False # Exits game - silight + running = True speed = 10 @@ -70,49 +142,11 @@ def lose_text(): # lose text score_font = pygame.font.Font("score_font.TTF",60) -# set font and color for lose text - silight -fontFill = pygame.font.Font("score_font.TTF",60) -textSurfaceLose = fontFill.render(lose_text(), True, (120, 255, 120), (0,0,0)) -loseTextRect = textSurfaceLose.get_rect() -loseTextRect.center = (200, 20) - background = pygame.image.load("background.png").convert() clock = pygame.time.Clock() -while running: #the main loop - clock.tick(20) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False #stops the program - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - running = False #stops the program - else: - if currentword.checkLetter(event.unicode): #event.unicode is the letter the user typed - speed += 3 - score += 1 - if len(extra_words) > 0: - lowestwordindex = 0 - for i in range(len(extra_words)): - if extra_words[i].rect.bottom > extra_words[lowestwordindex].rect.bottom: - lowestwordindex = i - currentword = extra_words.pop(lowestwordindex) - - else: - currentword = spawn_word() - currentword.update() - for i in extra_words: - i.update() - - score_surf = score_font.render("SCORE:"+str(score), True, (0,255,0)) - - screen.fill((0,0,0)) #clears the screen - screen.blit(background,(0,0)) - screen.blit(score_surf,(0,530)) - for i in extra_words: - screen.blit(i.image, i.rect) - pygame.draw.line(screen,(0,255,0),(width/2, height),(currentword.rect.left+7, currentword.rect.bottom),14) - screen.blit(currentword.image, currentword.rect) #draw the word - screen.blit(textSurfaceLose, loseTextRect)# draw lose message - silight - pygame.display.flip() #apply the changes +# The Game Starts Here - silight +intro() +mainLoop() pygame.quit() #fix the program breaking in IDLE +sys.exit() From 84c35cc255d288c9df90bf95772aa24b93f4c8b4 Mon Sep 17 00:00:00 2001 From: Silight Date: Sun, 2 Feb 2014 21:16:23 -0500 Subject: [PATCH 3/3] Added restart and fixed crash on exit. --- TypingGame.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/TypingGame.py b/TypingGame.py index 08abe4d..91d5b49 100644 --- a/TypingGame.py +++ b/TypingGame.py @@ -13,6 +13,18 @@ def spawn_word(): wordStr = random.choice(words).strip() return TypingGameWord(wordStr) +def init(): # resets all the values in the game. + global running, speed, wordfile, words, currentword, extra_words, score + running = True + speed = 10 + wordfile = open('words.txt', 'r') + words = wordfile.readlines() + currentword = spawn_word() + extra_words = [] + wordfile.close() + score = 0 + + def intro(): # Introduction to the game. Gives instructions on how to play - silight msg = """ Welcome to WORD BLASTER! @@ -119,20 +131,13 @@ def update(self): title = "Game Over" choice = ynbox(msg, title) if choice == 1: # reset the game. Object is still detected at the bottom of the screen. - silight - speed = 0 - score = 0 - score_surf = score_font.render("SCORE:"+str(score), True, (0,255,0)) - screen.fill((0,0,0)) #clears the screen - screen.blit(background,(0,0)) - screen.blit(score_surf,(0,530)) - pygame.display.flip() - intro() + init() else: running = False # Exits game - silight running = True -speed = 10 +speed = 10 wordfile = open('words.txt', 'r') words = wordfile.readlines() currentword = spawn_word()