G11_C1
Introduction to Programming with Python


| Activity Flow | Slide No. | Topic | Time |
|---|---|---|---|
| TA | 4 | Ice-breaker | 1 min |
| 5-12 | Programming Intro | 2 min | |
| 13-16 | Object Oriented Programming | 3 min | |
| 17-22 | Introduction to Python + Spyder + Pygame | 3 min | |
| 23-34 | TA - Coding | 15 min | |
| SA | 35-37 | SA | 5 min |
| Wrap - Up | 38-39 | Student Additional Activity | 1 min |
| Wrap-Up | 40 | Quiz | 5 min |

Class Structure
| Slide No. | Topic |
|---|---|
| 12 | Random number code |
| 24-26 | Basic pygame code |
| 32 | Making rectangles |
| 37 | SA Code- Creating ball rect. |
| 44 | Additional Activity- Code |
Preparation and Reference

Prerequisites
FOR TEACHER
FOR STUDENTS
-
Computer with an Internet connection.
-
The latest browser installed.
-
Spyder IDE.
-
Projector to present the screen.
1. Computer with an Internet connection.
2. The latest browser installed.
What is your favorite video game?



Let's play a game



Learning a new skill


The correct way to learn
Instructing a bot
Human vs Computer


B


Programming Languages


B

Learning Programming


B

Learning to Program

Learning how to program


B

Object-Oriented Programming


B

Can you guess the Properties
and Functions of a car?


B

Properties
Color
Name
Tyres
Functions
Drive()
Break()
Horn()


GREAT!

We will be creating lots of
new and different games


B


Breakout game


B

Complex game! Let's break it down


B
-
The paddle can be used to direct the motion of a ball.
-
The player has to guide the ball to collide with a stationary brick wall to destroy the brick and gain points.
-
If the player fails to catch the ball using the paddle, the game ends.

-
The player can move a paddle left/right by pressing the arrow keys on the keyboard.
Integrated Development Environment (IDE)
Python + Spyder

B



B
Pygame Library


Getting a "book issued" from the "library" is same as "importing pre-written code" from "pygame library".
Pygame Library


B
Turtle Library


Getting a "book issued" from the "library" is same as "importing pre-written code" from "turtle library".
Turtle Library


G11C1TA2T


B

import pygame
Step 1: Download and install the "pygame" library
Step 2: Create a new file in Spyder and save it as "breakout.py"
Step 3: Import the "pygame" library to your code


B
Step 4: Initialize the "pygame"
Step 5: Use "pygame" to create a window
Output:
import pygame
pygame.init()import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))


B
Step 6: Add a game loop
Output:
import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()


B
Coordinate system in pygame
y
x
500
width
height
pygame.Rect(200,500,30,10)
x
y
width
height
0,600
400,0
400,600
0,0
200
(10)
(30)


B
Step 7: Use "pygame.Rect()" to make a rectangle
import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))
paddle=pygame.Rect(200,500,30,10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

B
Guess why character
is not displayed?


B
pygame.draw.rect(surface, color, rect)
Drawing !


screen=pygame.display.set_mode((400,600))
paddle=pygame.Rect(200,500,30,30)


B
(Red, Green, Blue)
Red color -> (255, 0, 0)


B
Step 8: Draw the rectangle on the screen
Step 9: Update the screen
import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))
paddle=pygame.Rect(200,500,30,30)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.draw.rect(screen,(23,100,100),paddle)
pygame.display.update()

B
Output :





B

Student activity
Student Activity 1
Hints:
1. Create the rectangle object: pygame.Rect
2. Draw the created rectangle: pygame.draw.rect


B
Step 10: Create another rectangle named "ball" and draw it on the screen
import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))
paddle=pygame.Rect(200,500,30,30)
ball=pygame.Rect(70,50,10,10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.draw.rect(screen,(23,100,100),paddle)
pygame.draw.rect(screen,(255,255,255),ball)
pygame.display.update()


B

Student activity
SAA 1
Hints:
1. Create the rectangle object: pygame.Rect
2. Draw the created rectangle: pygame.draw.rect
3. RGB for orange: 255,69,0
Creation of Orange ball


B
Create another rectangle named "ball" and draw it on the screen
import pygame
pygame.init()
screen = pygame.display.set_mode((400,600))
paddle=pygame.Rect(200,500,30,30)
ball=pygame.Rect(70,50,10,10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.draw.rect(screen,(23,100,100),paddle)
pygame.draw.rect(screen,(255,69,0),ball)
pygame.display.update()
What will be the correct syntax for pygame.draw.rect(_,_,_)
B
Color,Surface,Rect
Surface,Color,Rect
Rect,Color,Surface
Q.1

A
Surface,Color,Rect
B
C
We first pass the surface, then the color, and then the name of the rectangle
What does RGB stand for?
B
Red-Grey-Blue
Red-Green-Blue
Red-Green-Black
Q.2

A
Red-Green-Blue
B
C
RGB stands for red green blue. Each color that we use can be represented as a combination of these 3 colors in varying percentages.



| Activity | Activity Name | Link |
|---|---|---|
| TEACHER ACTIVITY 1 | Random Number Generation | |
| TEACHER ACTIVITY 2 | Paddle creation | |
| TEACHER ACTIVITY 2 SOLUTION | Solution of TA2 | |
| STUDENT ACTIVITY 1 | Ball creation | |
| TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION | Solution of SA1 | |
| STUDENT ADDITIONAL ACTIVITY 1 | Ball creation_SAA1 | |
| TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION | Solution of SAA1 |
G11_C1 Final
By Sanjukta Bhattacharya
G11_C1 Final
- 190