Keyboard Jump Game in Python
Keyword hop (jump) game is a speed composing game that aides in further developing the composing velocity of players
The object of Keyboard Jump (hop) Game Python Project is to fabricate a console bounce game that assists players with speeding up. We use pygame, arbitrary, and time modules in this task.
In this task, the player needs to press similar keys to the letters showed on the game screen. On the off chance that the player made a blunder while composing, the game moves past.
Project Prerequisites
- To construct keyword hop game task we require pygame, irregular and time modules, and fundamental ideas of python.
- Pygame module is utilized to make mixed media application and games
- Arbitrary modules used to create irregular numbers
- Time module gives usefulness like holding up during execution and furthermore gauges the effectiveness of code.
- To install, we need the pip install command in prompt console:
pip install pygame pip install random
Project (Keyboard Jump Game) File Structure
- Import various module like Installing Pygame and Creating the game window
- Main loop
- Define functions
- Initialize window
Approach to begin developing the game, are performed through multiple stages.
1. Importing various Modules
import pygame import random import time
2. Initializing required window
Code snippet: This shows basically the logic of Initializing required window (not meant for execution), part of the Source code:
import pygame import random import time pygame.init () WIDTH1 = 855 HEIGHT1 = 650 black= (0,0,0) gameDisplay = pygame.display.set_mode ( (WIDTH1, HEIGHT1)) #setting game display size background = pygame.image.load ('keyback.jpg') background = pygame.transform.scale (background, (WIDTH1, HEIGHT1)) #scale image font = pygame.font.Font ('comic.ttf', 45)
Explanation of the above snippet:
- to instate pygame pygame.init ()
- to set the subtitle of the game window pygame.display.set_caption
- utilizing pygame.display.set_mode to set WIDTH1 and HEIGHT1 of game showcase size
- game foundation set by pygame.image.load, which is utilized to set the foundation picture
- pygame.transform.scale is utilized to scale picture to required aspect
3. Defining functions
Code snippet: This shows basically the logic of defining function (not meant for execution), part of the Source code:
word_speed11= 0.3 scores = 0 def new_word (): global displayword, yourword, x_cor1, y_cor1, text, word_speed1 x_cor1 = random.randint (300,700) y_cor1 = 255 #y - cor word_speed11+ = 0.10 yourword1= '' words = open ("words.txt").read ().split (', ') displayword1= random.choice (words) new_word () font_name1 = pygame.font.match_font ('comic.ttf') def draw_text (display, text, size, x, y): font = pygame.font.Font (font_name1, size) text_surface = font.render (text, True, black) text_rect = text_surface.get_rect () text_rect.midtop = (x, y) gameDisplay.blit (text_surface, text_rect) def new_word (): global displayword, yourword, x_cor1, y_cor1, text, word_speed1 x_cor1 = random.randint (300, 700) #randomly choose x - cor between 300 - 700 y_cor1 = 255 word_speed11+ = 0.10 yourword1= '' words = open ("words.txt").read ().split (', ') displayword1= random.choice (words) new_word () def game_front_screen (): gameDisplay.blit (background, (0,0)) if not game_over1 : draw_text (gameDisplay, "GAME OVER!", 95, WIDTH1 / 2, HEIGHT1 / 4) draw_text (gameDisplay,"Scores : " + str (scores), 70, WIDTH1 / 2, HEIGHT1 /2) else: draw_text (gameDisplay, "Press any key to begin!", 54, WIDTH1 / 2, 550) pygame.display.flip () waiting = True while waiting: for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () if event.type = = pygame.KEYUP: waiting = False game_over1 = True game_start1 = True while True: if game_over1: if game_start1: game_front_screen () game_start1 = False game_over1 = False
Explanation of the above snippet:
- new_word () capacity will haphazardly take words from word.txt document and furthermore introduce x and y directions of word
- draw_text () capacity will assist with attracting text in plain view a given textual style and size.
- get_rect () strategy return a rect object
- blit () is utilized to draw a picture or compose text on the screen at the predefined position
- game_front_screen () work show the front game screen and game over screen
- pygame.event.get () will return all the occasion put away in the pygame occasion line
- pygame.display.flip () will refresh just a piece of the screen yet in the event that no contention will pass, it will refresh the whole screen
- occasion type equivalent is utilized to stop the pygame window
- event.KEYUP occasion happens when a console key is squeezed and delivered
4. Main loop
Code snippet: This shows basically the logic of main loop stage (not meant for execution), part of the Source code:
import pygame import random import time game_over1 = True game_start1 = True while True: if game_over1: if game_start1: game_front_screen () game_start1 = False game_over1 = False background = pygame.image.load ('teacher - background.jpg') background = pygame.transform.scale (background, (WIDTH1, HEIGHT1)) character = pygame.image.load ('char.jpg') character = pygame.transform.scale (character, (55,55)) wood = pygame.image.load ('wood - .png') wood = pygame.transform.scale (wood, (95,55)) gameDisplay.blit (background, (0,0)) gameDisplay.blit (wood, (x_cor1 - 55, y_cor1+15)) gameDisplay.blit (character, (x_cor1 - 100, y_cor1)) draw_text (gameDisplay, str (displayword), 45, x_cor1, y_cor1) draw_text (gameDisplay, 'Scores:'+str (scores), 45, WIDTH1/2 , 5) def new_word (): global displayword, yourword, x_cor1, y_cor1, text, word_speed1 x_cor1 = random.randint (300, 700) #randomly choose x - cor between 300 - 700 y_cor1 = 255 word_speed11+ = 0.10 yourword1= '' words = open ("words.txt").read ().split (', ') displayword1= random.choice (words) new_word () y_cor1 + = word_speed1 for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () quit () elif event.type = = pygame.KEYDOWN: yourword1+ = pygame.key.name (event.key) if displayword.startswith (yourword): if displayword1= = yourword: scores + = len (displayword) new_word () else: game_front_screen () time.sleep (2) pygame.quit () if y_cor1 < HEIGHT1 - 5: pygame.display.update () else: game_front_screen ()
Main logic Function
- We take care of key occasions
- Moving the keys across the screen
- we don't need any invalid keys to move into screen
- Assuming two keys squeezed at the same time
- headings all the while
The complete consolidated source code:
#importing modules import pygame import random import time #initializing window pygame.init () WIDTH1 = 855 HEIGHT1 = 650 black= (0,0,0) gameDisplay = pygame.display.set_mode ( (WIDTH1, HEIGHT1)) pygame.display.set_caption ('your Keyboard Jumping Game ') background = pygame.image.load ('keyback.jpg') background = pygame.transform.scale (background, (WIDTH1, HEIGHT1)) font = pygame.font.Font ('comic.ttf', 45) ## function to get words randomly word_speed11= 0.3 scores = 0 def new_word (): global displayword, yourword, x_cor1, y_cor1, text, word_speed1 x_cor1 = random.randint (300, 700) #randomly choose x - cor between 300 - 700 y_cor1 = 255 word_speed11+ = 0.10 yourword1= '' words = open ("words.txt").read ().split (', ') displayword1= random.choice (words) new_word () def game_over1 (): my_font1= pygame.font.SysFont ('times new roman', 51) game_over1_surface = my_font.render ('Your Scores is : ' + str (Scores), True, red_color) game_over1_rect = game_over1_surface.get_rect () game_over1_rect.midtop = (window_x1/2, window_y1/4) game_window.blit (game_over1_surface, game_over1_rect) pygame.display.flip () time.sleep (2) pygame.quit () quit () #function drawing text font_name1 = pygame.font.match_font ('comic.ttf') def draw_text (display, text, size, x, y): font = pygame.font.Font (font_name1, size) text_surface = font.render (text, True, black) text_rect = text_surface.get_rect () text_rect.midtop = (x, y) gameDisplay.blit (text_surface, text_rect) def game_front_screen (): gameDisplay.blit (background, (0,0)) if not game_over1 : draw_text (gameDisplay, "GAME OVER!", 95, WIDTH1 / 2, HEIGHT1 / 4) draw_text (gameDisplay,"Scores : " + str (scores), 70, WIDTH1 / 2, HEIGHT1 /2) else: draw_text (gameDisplay, "Press a key to begin!", 54, WIDTH1 / 2, 550) pygame.display.flip () waiting = True while waiting: for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () if event.type = = pygame.KEYUP: waiting = False #main looping game_over1 = True game_start1 = True while True: if game_over1: if game_start1: game_front_screen () game_start1 = False game_over1 = False background = pygame.image.load ('teacher - background.jpg') background = pygame.transform.scale (background, (WIDTH1, HEIGHT1)) character = pygame.image.load ('char.jpg') character = pygame.transform.scale (character, (55,55)) wood = pygame.image.load ('wood - .png') wood = pygame.transform.scale (wood, (95,55)) gameDisplay.blit (background, (0,0)) y_cor1 + = word_speed1 gameDisplay.blit (wood, (x_cor1 - 55,y_cor1+15)) gameDisplay.blit (character, (x_cor1 - 100,y_cor1)) draw_text (gameDisplay, str (displayword), 45, x_cor1, y_cor1) draw_text (gameDisplay, 'Scores:'+str (scores), 45, WIDTH1/2 , 5) for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () quit () elif event.type = = pygame.KEYDOWN: yourword1+ = pygame.key.name (event.key) if displayword.startswith (yourword): if displayword1= = yourword: scores + = len (displayword) new_word () else: game_front_screen () time.sleep (2) pygame.quit () if y_cor1 < HEIGHT1 - 5: pygame.display.update () else: game_front_screen ()
Explanation:
In this principle circle, assuming that the letters displayed on the game screen is equivalent to the letter you squeezed than your scores will increment and this will go on however on the off chance that your squeezed letter isn't equivalent to the showcase screen letter then the circle quits running and the game over screen will show and the game will over
Output:
Screenshot of output is presented below


Summary
With this undertaking in Python, we have effectively fostered the game. We utilized the famous time and arbitrary modules to deliver our program. Executing various capacities and utilizing circles assisted us with a superior comprehension of python fundamentals.
We have effectively fostered the console bounce game python project. We utilized famous pygame and arbitrary modules. We figured out how to arbitrarily create words. Along these lines, we construct a console bounce game. I truly want to believe that you appreciated building this game task.