Karthiks_python_program-1
Karthiks_python_program-1
Game Loop: The core of the game that keeps it running and
updates the game state.
Graphics and Sprites: Images representing the character and
obstacles, often stored in PNG format.
Collision Detection: Logic to detect when Tom hits an obstacle
or the ground, which usually ends the game.
Score Keeping: A system to track the player's progress, often
displayed on the screen during gameplay.
User Input: Handling keyboard or mouse input to control
Tom's movements.
A Brief Introduction About Modules
Used In Program
random:
This module is used to generate random numbers, which can be useful
for various game mechanics such as spawning obstacles at random
intervals or positions.
sys:
This module provides access to some variables used or maintained by
the Python interpreter and to functions that interact strongly with the
interpreter. In game development, it’s often used to handle system-
level operations like exiting the game.
pygame:
This is the main module required to create games with the pygame
library. pygame provides functionalities for rendering graphics,
playing sounds, and handling user inputs.
REQUIREMENTS:
HARDWARE REQUIREMENT:-
Minimum requirement of 5 mb RAM
SOFTWARE REQUIREMENT:-
PYTHON 3.12(works on any version of python 3)
OS: WINDOW 10 or Above
SYSTEM DESIGN
Flow chart
Steps involved
1. Install the latest version of Python.
Import random module
Import sys
2. Now go to command prompt and type the
given lines:-
Pip install pygame
sys: This is another built-in Python module that provides access to some
variables used or maintained by the interpreter. In this code, it is used to exit
the program.
pygame: This is the main library used for creating the game. It provides
functionalities for graphics, sound, and handling user input.
Coding
import random # For generating random numbers
import sys # We will use sys.exit to exit the program
import pygame
from pygame.locals import * # Basic pygame imports
def welcomeScreen():
"""
Shows welcome images on the screen
"""
playerx = int(SCREENWIDTH/5)
playery = int((SCREENHEIGHT -
GAME_SPRITES['player'].get_height())/2)
messagex = int((SCREENWIDTH -
GAME_SPRITES['message'].get_width())/2)
messagey = int(SCREENHEIGHT*0.13)
basex = 0
while True:
for event in pygame.event.get():
# if user clicks on cross button, close the game
if event.type == QUIT or (event.type==KEYDOWN and
event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# If the user presses space or up key, start the game for them
elif event.type==KEYDOWN and (event.key==K_SPACE or
event.key == K_UP):
return
else:
SCREEN.blit(GAME_SPRITES['background'], (0, 0))
SCREEN.blit(GAME_SPRITES['player'], (playerx, playery))
SCREEN.blit(GAME_SPRITES['message'],
(messagex,messagey ))
SCREEN.blit(GAME_SPRITES['base'], (basex, GROUNDY))
pygame.display.update()
FPSCLOCK.tick(FPS)
def mainGame():
score = 0
playerx = int(SCREENWIDTH/5)
playery = int(SCREENWIDTH/2)
basex = 0
# Create 2 pipes for blitting on the screen
newPipe1 = getRandomPipe()
newPipe2 = getRandomPipe()
pipeVelX = -4
playerVelY = -9
playerMaxVelY = 10
playerMinVelY = -8
playerAccY = 1
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and
event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE or
event.key == K_UP):
if playery > 0:
playerVelY = playerFlapAccv
playerFlapped = True
GAME_SOUNDS['wing'].play()
# Add a new pipe when the first is about to cross the leftmost
part of the screen
if 0<upperPipes[0]['x']<5:
newpipe = getRandomPipe()
upperPipes.append(newpipe[0])
lowerPipes.append(newpipe[1])
def getRandomPipe():
"""
Generate positions of two pipes(one bottom straight and one top
rotated ) for blitting on the screen
"""
pipeHeight = GAME_SPRITES['pipe'][0].get_height()
offset = SCREENHEIGHT/3
y2 = offset + random.randrange(0, int(SCREENHEIGHT -
GAME_SPRITES['base'].get_height() - 1.2 *offset))
pipeX = SCREENWIDTH + 10
y1 = pipeHeight - y2 + offset
pipe = [
{'x': pipeX, 'y': -y1}, #upper Pipe
{'x': pipeX, 'y': y2} #lower Pipe
]
return pipe
if __name__ == "__main__":
# This will be the main point from where our game will start
pygame.init() # Initialize all pygame's modules
FPSCLOCK = pygame.time.Clock()
pygame.display.set_caption('Flappy Tom by KUSHAL ROCK ')
GAME_SPRITES['numbers'] = (
pygame.image.load('gallery/sprites/0.png').convert_alpha(),
pygame.image.load('gallery/sprites/1.png').convert_alpha(),
pygame.image.load('gallery/sprites/2.png').convert_alpha(),
pygame.image.load('gallery/sprites/3.png').convert_alpha(),
pygame.image.load('gallery/sprites/4.png').convert_alpha(),
pygame.image.load('gallery/sprites/5.png').convert_alpha(),
pygame.image.load('gallery/sprites/6.png').convert_alpha(),
pygame.image.load('gallery/sprites/7.png').convert_alpha(),
pygame.image.load('gallery/sprites/8.png').convert_alpha(),
pygame.image.load('gallery/sprites/9.png').convert_alpha(),
)
GAME_SPRITES['message']
=pygame.image.load('gallery/sprites/message.png').convert_alpha()
GAME_SPRITES['base']
=pygame.image.load('gallery/sprites/base.png').convert_alpha()
GAME_SPRITES['pipe']
=(pygame.transform.rotate(pygame.image.load(
PIPE).convert_alpha(), 180),
pygame.image.load(PIPE).convert_alpha()
)
# Game sounds
GAME_SOUNDS['die'] =
pygame.mixer.Sound('gallery/audio/die.wav')
GAME_SOUNDS['hit'] =
pygame.mixer.Sound('gallery/audio/hit.wav')
GAME_SOUNDS['point'] =
pygame.mixer.Sound('gallery/audio/point.wav')
GAME_SOUNDS['swoosh'] =
pygame.mixer.Sound('gallery/audio/swoosh.wav')
GAME_SOUNDS['wing'] =
pygame.mixer.Sound('gallery/audio/wing.wav')
GAME_SPRITES['background'] =
pygame.image.load(BACKGROUND).convert()
GAME_SPRITES['player'] =
pygame.image.load(PLAYER).convert_alpha()
while True:
welcomeScreen() # Shows welcome screen to the user until he
presses a button
mainGame() # This is the main game function
print("GameOver”)
print("Tap to play again")
-------------------------------------***--------------------------------------
OUTPUTS
ADVANTAGES:
1. Learning Python: Building a game is a fun and engaging way to deepen your
understanding of Python programming. You'll get hands-on experience with
important concepts such as loops, conditionals, and functions.
2. Problem-Solving Skills: Game development involves solving many small and large
problems, such as collision detection, scoring systems, and game physics. This
enhances your problem-solving skills and logical thinking.
3. Creativity and Innovation: Developing a game allows you to express your
creativity. You can design characters, levels, and gameplay mechanics, making the
game unique and interesting.
DISADVANTAGES:
1. Performance Issues: Python, being an interpreted language, might not be as
fast as compiled languages like C++ or Java. This could lead to performance issues,
especially in more complex games with heavy graphics or processing requirements