Course Code VSC SEM-V Practical Credits Lectures/Week
25CSVSP51 GAME PROGRAMMING (P) 2 4
Course Outcomes:
After successful completion of this course, students would be able to
● CO1: Recall and implement vector mathematics and basic transformations in games
● CO2: Explain and implement game loops, events, and sprite-based animations .
● CO3: Develop interactive games with physics, collision detection, and sound effects
● CO4: Analyze and integrate AI-driven behaviours and optimize games for better
performance.
1 Write a program to visualize vector addition, subtraction, and scalar multiplication.
2Implement basic 2D transformations (translation, scaling, rotation) on shapes using
Pygame.
3 Create a Pygame window and display the transformed shapes.
4 Implement a basic game loop with events like quitting or restarting the game.
5 Create a sprite that animates based on player inputs.
6 Simulate gravity and bouncing effects using Pygame.
7 Implement collision detection in a game
8 Create a game with sound effects triggered by specific events
9 Develop a multi-level game with transitions, a game-over screen, and restart functionality.
10 Implement AI behaviour where an enemy chases the player in a 2D environment.
Visualize Vector Addition, Subtraction, and Scalar Multiplication.
Draw and visualize vectors (A, B)
Show:
o A + B (Addition)
o A - B (Subtraction)
o k × A (Scalar multiplication)
*Import Libraries*
import pygame
import sys
pygame: Main game engine library.
sys: Used for exiting the program properly (sys.exit()).
*Initialize Pygame & Set Up Screen*
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Vector Operations")
pygame.init(): Initializes all imported Pygame modules.
screen = ...: Creates a window of 800×600 pixels.
set_caption: Sets window title.
*Define Colors and Origin*
WHITE, RED, GREEN, BLUE, ORANGE, PURPLE, BLACK = ...
origin = (WIDTH // 2, HEIGHT // 2)
font = pygame.font.SysFont("Arial", 20)
Colors are defined as RGB tuples.
origin: Center of the screen – (400, 300).
font: A font object for rendering text.
*Define Vectors*
A = pygame.math.Vector2(100, -50)
B = pygame.math.Vector2(50, -100)
scalar = 1.5
Vector2: A Pygame class for 2D vectors.
Vectors point in specific directions:
o A goes right 100, up 50.
o B goes right 50, up 100.
scalar: Used for multiplying A.
*Function to Draw Vectors*
def draw_vector(v, color, label, offset=(0, 0)):
end = (origin[0] + v.x + offset[0], origin[1] + v.y + offset[1])
pygame.draw.line(screen, color, origin, end, 3)
pygame.draw.circle(screen, color, end, 5)
screen.blit(font.render(label, True, color), end)
What it does:
Takes a vector v, draws a line from the origin to its end.
Draws a small circle at the end point.
Displays the vector name (label).
offset: Optional shift to avoid overlapping labels.
*Main Game Loop*
running = True
while running:
...
This loop runs until the user quits. Inside:
*Handle Events (Quit Condition)*
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Listens for window events.
If close button is pressed, the loop ends.
*Draw Coordinate Axes*
pygame.draw.line(screen, BLACK, (0, HEIGHT//2), (WIDTH, HEIGHT//2), 1)
pygame.draw.line(screen, BLACK, (WIDTH//2, 0), (WIDTH//2, HEIGHT), 1)
Draws X-axis (horizontal) and Y-axis (vertical) through screen center.
*Draw Vectors*
draw_vector(A, RED, "A")
draw_vector(B, GREEN, "B")
draw_vector(A + B, BLUE, "A+B")
draw_vector(A - B, ORANGE, "A-B", offset=(10, 10))
draw_vector(A * scalar, PURPLE, "1.5*A", offset=(15, -15))
Each vector is drawn in a different color.
Subtraction and scalar multiplication use an offset to prevent label overlap.
*Update the Screen*
pygame.display.flip()
pygame.time.Clock().tick(60)
flip(): Refreshes the entire display.
Clock().tick(60): Limits frame rate to 60 frames per second.
*Quit Pygame Safely*
pygame.quit()
sys.exit()
Closes the Pygame window and exits the Python script properly.
Pygame Concepts:
Concept Purpose
pygame.init() Initializes modules
pygame.display.set_mode() Creates window
pygame.draw.line() Draws vectors and axes
pygame.draw.circle() Marks vector tip
pygame.font.SysFont() Renders labels
pygame.event.get() Handles user input
Vector2 Manages vector math
vector geometry,
screen coordinate systems
interactive graphical programming using Pygame.
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen setup
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Vector Visualization")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
# Center point
origin = (WIDTH // 2, HEIGHT // 2)
# Define Vectors A and B
A = pygame.math.Vector2(100, -50) # Pygame uses screen coords: y is flipped
B = pygame.math.Vector2(50, -100)
# Scalar
scalar = 1.5
# Font
font = pygame.font.SysFont("Arial", 20)
# Function to draw a vector as arrow
def draw_vector(v, color, label, offset=(0, 0)):
end_pos = (origin[0] + v.x + offset[0], origin[1] + v.y + offset[1])
pygame.draw.line(screen, color, origin, end_pos, 3)
pygame.draw.circle(screen, color, end_pos, 5)
text = font.render(label, True, color)
screen.blit(text, end_pos)
# Game Loop
running = True
while running:
screen.fill(WHITE)
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Draw axes
pygame.draw.line(screen, BLACK, (0, HEIGHT//2), (WIDTH, HEIGHT//2), 1)
pygame.draw.line(screen, BLACK, (WIDTH//2, 0), (WIDTH//2, HEIGHT), 1)
# Vector Calculations
C=A+B
D=A-B
E = A * scalar
# Draw vectors
draw_vector(A, RED, "A")
draw_vector(B, GREEN, "B")
draw_vector(C, BLUE, "A + B")
draw_vector(D, (255, 165, 0), "A - B", offset=(10, 10))
draw_vector(E, (128, 0, 128), "1.5 * A", offset=(15, -15))
pygame.display.flip()
pygame.time.Clock().tick(60)
pygame.quit()
sys.exit()