My pygame source code

This is a simple game that checks your typing speed. Though this is a simple one, this might be helpful for you to understand the basic stuffs of pygame. Expect you will enjoy the game,and for any further help regarding the code please don’t hesitate to contact me (find it on about tab).

Please download the tar file from this link : worgame

import sys, pygame
from pygame import *
pygame.init()

bal_x = 500  # initial x co-ordinate position of baloon
bal_y = 0    #initial y co-ordinate position of baloon
bal_move = 1 #moving speed of baloon
black = [0, 0, 0]
white = [255, 255, 255]
red = [255,0,0]
yellow = [255,255,0]
green = [0,255,0]
points = 0  # variable used to display points on screen

# List words to be appear in screen

word_list = ['sky','man','how','type','kites','yahoo','speed',
'use','twist','google','water','change','play','move','dance',
'squirrel','python','newyork','india','coconut','webmaster',
'transperancy','cocroach','corresponding','random',
'youarethewinner']

#word_list = ['sky','man','how','type','kites']
list_size = len(word_list)

# setting the size of game window
screen = display.set_mode((900,800))
# caption appeared on the header of game window
display.set_caption('Word Game')
# loading background image appeared on the screen
sky = pygame.image.load('sky_new0.jpg')
# obtain rectangular space needed to exist sky
sky_rect = sky.get_rect()
baloon = pygame.image.load('baloon.gif')
# update the full display Surface to the screen
pygame.display.flip()					

#setting the customised font to use in  game the below code prepare
a varible correct_word,which is used to display a message on the screen 
when player enter a correct word
basicfont = pygame.font.Font('freesansbold.ttf',28)
correct_word=basicfont.render('Entered word is correct.',True,white)
wrong_letter = basicfont.render('Wrong letter!',True, white)
game_over=basicfont.render('Failed to enter correct word',True,red)
better_luck= basicfont.render('Better luck next time...',True,white)
winner=basicfont.render('CONGRATS.!!! YOU WON THE GAME',True,yellow)
exit_game = basicfont.render('Exiting game...',True,red)

#following function defines how a baloon move
def move_baloon():
        global bal_y
        global word
        global count
	global points
        while bal_y = 800 :
			screen.blit(game_over,(150,100))
			screen.blit(better_luck,(150,200))
			pygame.display.update()
			pygame.time.delay(5000)
			sys.exit()

#this function returns true if user enter matching letter as in the 
screen else return false.

def match_letter():
        global count
	input_char = getkeypress()
     	if input_char == word[count]:
                count=count+1
                return True
	else :
                screen.blit(wrong_letter,(200,100))
                pygame.display.update()
		pygame.time.delay(2000)
                return False
#return the corresponding letter that of pressed key.
def getkeypress():
        for event in pygame.event.get(pygame.KEYDOWN):
                ascii_val = event.key
                character = chr(ascii_val)
                return character

#initial function to start the game
def start_game():
	global word
	global bal_x
	global bal_y
        for word in word_list:
		for event in pygame.event.get():
			if event.type == pygame.QUIT: sys.exit()

                global count
		bal_y = 0
		count=0
		move_baloon()
		if word == word_list[list_size-1]:
			screen.fill(black)
			screen.blit(winner,(150,400))
			screen.blit(exit_game,(300,500))
			pygame.display.update()
			pygame.time.delay(5000)
			sys.exit()

start_game()
pygame.quit()

find this code at bitbucket :https://bitbucket.org/jinesh_paloor/pygame/changeset/8320440015ec

Advertisements