0% found this document useful (0 votes)
7 views9 pages

Gdlab

Uploaded by

swetharaghu4311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Gdlab

Uploaded by

swetharaghu4311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex.

no:05

Date:

Developing a 2D interactive using Pygame


Aim:
To develop a 2D interactive using Pygame.

Algorithm:
1. Initialize Pygame

2. Set Up the Screen


3. Load Assets
4. Define Game Objects
5. Handle User Input
6. Update Game State
7. Render Graphics
8. Repeat
9. Handle Game Over Conditions
10. Display Game Over Screen
11. Clean Up

Program:
import pygame
import random
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simple Game")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
player_width = 50
player_height = 50
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - player_height - 20
player_speed = 5
obstacle_width = 50
obstacle_height = 50
obstacle_speed = 3
obstacles = []
clock = pygame.time.Clock()
spawn_obstacle_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_obstacle_event, 1500)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == spawn_obstacle_event:
obstacle_x = random.randint(0, WIDTH - obstacle_width)
obstacle_y = -obstacle_height
obstacles.append(pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed
for obstacle in obstacles:
obstacle.y += obstacle_speed
if obstacle.y > HEIGHT:
obstacles.remove(obstacle)
player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
for obstacle in obstacles:
if player_rect.colliderect(obstacle):
running = False
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, player_rect)
for obstacle in obstacles:
pygame.draw.rect(screen, BLACK, obstacle)
pygame.display.flip()
clock.tick(60)
pygame.quit()

Output:
Result:
Thus the development of 2D interactive using pygame is written , verified and executed
successfully.
Ex.No:06
Date:
Developing a Puzzle game
Aim:
To develop a Puzzle game.

Algorithm:
1. Initialize Pygame
2. Set Up the Screen
3. Load Assets.
4. Split Image into Tiles.
5. Shuffle Tiles.
6. Draw the Puzzle
7. Handle User Input.
8. Move Tiles.
9. Check for Win Condition
10. Display Win Screen
11. Repeat
12. Clean Up

Program:
import pygame
import random
pygame.init()
WIDTH, HEIGHT = 480, 480
TILE_SIZE = 120
ROWS, COLS = 4, 4
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sliding Puzzle")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
image = pygame.image.load("puzzle_image.jpg")
image = pygame.transform.scale(image, (WIDTH, HEIGHT))
tiles = []
for y in range(ROWS):
for x in range(COLS):
rect = pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
tile = image.subsurface(rect)
tiles.append(tile)
shuffled_tiles = tiles[:]
random.shuffle(shuffled_tiles)
def draw_tiles():
for i, tile in enumerate(shuffled_tiles):
x = (i % COLS) * TILE_SIZE
y = (i // ROWS) * TILE_SIZE
screen.blit(tile, (x, y))
running = True
while running:
screen.fill(WHITE)
draw_tiles()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_x, mouse_y = pygame.mouse.get_pos()
clicked_tile = (mouse_x // TILE_SIZE) + (mouse_y // TILE_SIZE) * COLS
blank_tile = shuffled_tiles.index(None)
if clicked_tile - 1 == blank_tile or clicked_tile + 1 == blank_tile or clicked_tile -
COLS == blank_tile or clicked_tile + COLS == blank_tile:
shuffled_tiles[clicked_tile], shuffled_tiles[blank_tile] = shuffled_tiles[blank_tile],
shuffled_tiles[clicked_tile]
pygame.quit()

Output:
Result:
Thus the development of a puzzle game using pygame is written , verified and executed
successfully.

You might also like