G11_C2

Variable and Conditional Programming

Activity Flow Slide No. Topic Time
TA 4-7 Warm-up Quiz+Recap 3 min
8-11 Understanding variables 5 min
12 Arithmetic operations 3 min
13-15 TA - Coding (ball movement) 3 min
SA 16-19 SA-coding (ball movement) 4 min
TA 20-24 Decision Making 5 min
24-26 TA - Coding (ball movement with restriction-x axis) 3 min
27-30 TA - Coding (ball movement with restriction-y axis) 3 min
31-36 Additional Activity 2 min
Wrap - Up 37 Quiz 10 min
Slide No. Topic
14 TA Code- Ball movement 
18 SA Code- Ball movement 
26 TA Code- Ball movement with restrictions
30 SA Code- Ball movement with restrictions
33 Additional Activity 1- Code
35 Additional Activity 2- Code

Class Structure

Preparation and Reference

Prerequisites

FOR TEACHER

FOR STUDENTS

1. Computer with an Internet connection.

2. The latest browser installed.

3. Spyder IDE.

4. Projector to present the screen

1. Computer with an Internet connection.

2. The latest browser installed.

3. Spyder IDE.

(WARM-UP QUIZ)

Q1.

B

Rect(x,y,width,height)

pygame.Rect(x,y,width,height)

pygame.rect(x,y,width,height)

A

pygame.Rect(x,y,width,height)

B

C

Option B is the correct syntax for creating a rectangle. We pass the x-position followed by the y-position, then the width and the height of the rectangle.

Choose the correct command for creating a rectangle using the pygame module.

What is the correct order of steps to be able to see a rectangle on screen?

A. Draw a rectangle on the screen.

B. Create a rectangle object.

C. Pass the x coordinate, y coordinate, width, and height.

Q2.

B

A-B-C

B-C-A

A-C-B

A

B-C-A

B

C

Option B is correct. We first create the rectangle object using the pygame.Rect() command. Then we pass the required parameters. Finally, to see the rectangle, we need to draw the rectangle on the screen

 

Ball

Paddle

Breakout Game

Revision

Data in our daily lives

Processing Data

Sender

Back Office

Receiver

Is the human memory enough?

Sender

Back Office

Receiver

Scenario 1

Scenario 2

x=78
x=78
variable
value
x+5=78+5=83

Hi David! Your slot has been successfully booked on 6/3/2022.

print(variable_name)
print(person1_name)

FunctionName

Input

Open Parenthesis

Close Parenthesis

int(age)

Datatype required

Variable

Id No.=XXXX__

Name: David

Age: 68

Weight:72.5

1111

9999

Some intermediate number is picked.

//Complete the code for the the "up" function
function up()
{
	if(player_y >=     )
	{
		player_y =                     ;
		console.log("block image height = " +               );
		console.log("When Up arrow key is pressed, X =  " + player_x + " , Y = "+player_y);
		canvas.remove(              );
		player_update();
	}
}randint(1111,9999)
import random

die_roll=random.randint(1,6)

print("First value of die_roll:",die_roll)

while die_roll!=6:
  
  die_roll=random.randint(1,6)
  
  print("New value of die_roll:",die_roll)

1. Some random integer between 1 to 6 is assigned to "die_roll"

2. Value of "die_roll" is checked.

6

6

Condition satisfied.

Condition not satisfied.

3.Die is rolled again.

LOOPING

Loop Stops.

Day Study time  Play time  Sleep time
1 7 hours 8 hours 9 hours
2 8 hours 8 hours 8 hours
3 6 hours 7 hours 11 hours
4 10 hours 8 hours 6 hours
5 7 hours 11 hours 6 hours

Studying

Playing

Sleeping

                  OLS Regression Results                            
==============================================================================
Dep. Variable:                     RH   R-squared:                       0.879
Model:                            OLS   Adj. R-squared:                  0.878
Method:                 Least Squares   F-statistic:                     4115.
Date:                Wed, 25 Aug 2021   Prob (F-statistic):               0.00
Time:                        11:59:40   Log-Likelihood:                -20050.
No. Observations:                6269   AIC:                         4.012e+04
Df Residuals:                    6257   BIC:                         4.020e+04
Df Model:                          11                                         
Covariance Type:            nonrobust                                         
=================================================================================
                    coef    std err          t      P>|t|      [0.025      0.975]
---------------------------------------------------------------------------------
const         -1.503e+04   1240.167    -12.118      0.000   -1.75e+04   -1.26e+04
PT08.S1(CO)       0.0148      0.001     14.305      0.000       0.013       0.017
C6H6(GT)         -0.9035      0.075    -12.092      0.000      -1.050      -0.757
PT08.S2(NMHC)    -0.0059      0.002     -2.393      0.017      -0.011      -0.001
PT08.S3(NOx)      0.0015      0.001      1.853      0.064   -8.67e-05       0.003
PT08.S4(NO2)      0.0265      0.001     21.601      0.000       0.024       0.029
PT08.S5(O3)      -0.0011      0.001     -1.945      0.052      -0.002    8.27e-06
T                -2.3549      0.015   -160.240      0.000      -2.384      -2.326
AH               29.5517      0.554     53.337      0.000      28.466      30.638
Year              7.5052      0.618     12.148      0.000       6.294       8.716
Month             1.1679      0.062     18.914      0.000       1.047       1.289
Day               0.0352      0.009      4.040      0.000       0.018       0.052
==============================================================================
Omnibus:                      322.611   Durbin-Watson:                   1.966
Prob(Omnibus):                  0.000   Jarque-Bera (JB):              416.392
Skew:                           0.509   Prob(JB):                     3.82e-91
Kurtosis:                       3.748   Cond. No.                     5.25e+07
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 5.25e+07. This might indicate that there are
strong multicollinearity or other numerical problems.

<Your Account Name>

Open

Download

Link.

Download

Application.

Install

Application.

Open Installed Application.

Link your Facebook Account.

Begin Creating!

Open

Download

Link.

Download

Application.

Install

Application.

Open Installed Application.

Link your Facebook Account.

Begin Creating!

Open

Download

Link.

Download

Application.

Install

Application.

Open Installed Application.

Link your Facebook Account.

Begin Creating!

Open

Download

Link.

Download

Application.

Install

Application.

Open Installed Application.

Link your Facebook Account.

Begin Creating!

abcd=a^n+b^n+c^n+d^n
//Complete the code for the the "up" function
function up()
{
	if(player_y >=     )
	{
		player_y =                     ;
		console.log("block image height = " +               );
		console.log("When Up arrow key is pressed, X =  " + player_x + " , Y = "+player_y);
		canvas.remove(              );
		player_update();
	}
}randint(1111,9999)
plt.stackplot(x-axis_variable, y-axis_variable_1,y-axis_variable_2,...,
              colors = list of colors,labels=list of labels)

How does the computer store our data?

Computer

Mother Board

Memory Slots

Mathematical Operations on Variables

Addition

Subtraction

Division

Multiplication

import pygame
pygame.init() 
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size=(600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Breakout Game")
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1
carryOn = True
while carryOn:
    for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop             
    screen.fill(DARKBLUE)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Output :

Expected Output:

Altering Movement Direction

How to make the ball move in the diagonally upward direction?


paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1

Hint:

Alter values of ballx and bally

import pygame
pygame.init() 
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size=(600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Breakout Game")
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=-1
bally=-1
carryOn = True
while carryOn:
    for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop             
    screen.fill(DARKBLUE)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Fault in our game!!

Ball moves out and disappears!!

Decision making

> 60?

Logical operations

Greater than

Lesser than

Equals to

How the computer makes decisions?

age>60?
True
age>60?
True
if
age>60:
discount=200

tab space

colon

Syntax:

import pygame
pygame.init() 
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size=(600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Breakout Game")
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=-1
bally=-1
carryOn = True
while carryOn:
    for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop             
    screen.fill(DARKBLUE)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    if ball.x>=590:
      ballx=-ballx
    if ball.x<=10:
      ball.x=-ballx
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Teacher Activity 2

Output:

import pygame
pygame.init() 
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size=(600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Breakout Game")
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=-1
bally=-1
carryOn = True
while carryOn:
    for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop             
    screen.fill(DARKBLUE)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    if ball.x>=590:
      ballx=-ballx
    if ball.x<=10:
      ball.x=-ballx
      
    if ball.y>=590:
      bally=-bally
    if ball.y<=10:
      ball.y=-bally
      
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Teacher Activity 3

Output

Expected Output:

Paddle movement

SAA-1

How will you make the paddle move?

Hint:

- Paddle will only move in x direction.

-Create "paddlex" variable  


-Increment the paddle's x coordinate.

paddlex=2
paddle.x+=paddlex
import pygame

ballx=-1
bally=-1
paddlex=1
carryOn = True
while carryOn:
    for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop             
    screen.fill(DARKBLUE)
    paddle.x=paddle.x+paddlex
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    
      
   
    

SAA1 solution

SAA-2

Restricting paddle movement

How will you restrict the paddle within the screen?

Hint:

-Screen ranges from 0 to 600 in width.

-Paddle length is 60.

-So paddlex coordinate should be between 0 and 540.

 

 

-Similarly:

 

if paddle.x<0:
  paddlex=-paddlex
if paddle.x>540:
  paddlex=-paddlex

Expected Output:

if paddle.x>=540:
    paddlex=-paddlex
if paddle.x<=60:
    paddlex=-paddlex
   

SAA2 solution

(WRAP-UP QUIZ)

Choose the correct output

Error

Q.1

x=5
if x<10:
print(x+4*10)

B

45

90

A

Error

B

C

Option B is correct. There is an indentation error as  the if statement should be followed by a tab space before each statement under the loop

Choose the correct output

True

Q.2

B

No output

12

A

True

B

C

x=10
x=x+6/3
if x==12:
  print("True")

According to Bodmas division, will be executed first before addition, so the equation will be solved as:

x=10+2     x=12

Hence the logical check will print True.

So option B is correct.

Activity Activity Name Link
TEACHER ACTIVITY 1  Ball Movement
TEACHER ACTIVITY 2 Ball Movement with Restrictions(X-axis)
TEACHER ACTIVITY 3 Ball Movement with Restrictions(Y-axis)
TEACHER ACTIVITY 1 SOLUTION Solution of TA1
TEACHER ACTIVITY 2 SOLUTION Solution of TA2
TEACHER ACTIVITY 3 SOLUTION Solution of TA3
STUDENT ACTIVITY 1 Ball Movement(Direction Change)
TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION Solution of SA1
STUDENT ADDITIONAL ACTIVITY 1 Paddle Movement
STUDENT ADDITIONAL ACTIVITY 2 Paddle Movement with restrictions
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION Solution of SAA1
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 2 SOLUTION Solution of SAA2

Text

G11_C2

By Sanjukta Bhattacharya