G11_C2
Variable and Conditional Programming
Camera texture
+
Rainbow colored texture
Rainbow effect limited to person only.
Background remains blank.
Camera feed with a green glow for the person only.
Camera feed modified to look like camera negative.
Superimpose both rectangles to get scary face.
Camera texture with color:
1. Create a rectangle and name it as "Quad3".
3. Create a material and name it as "Quad3".
4. Put texture in Diffuse property as cameraTexture.
5. Put color in Diffuse property as orange.
2. Resize and place the rectangle.
Camera texture with color
1. Create a rectangle and name it as "Quad3".
3. Create a material and name it as "Quad3".
4. Put texture in Diffuse property as cameraTexture.
5. Put color in Diffuse Property as Orange.
2. Resize and place the rectangle.
Add a rectangle.
Rename it Quad4_face.
Add a material.
Rename it Quad4_face.
Change material properties:
1. Diffuse: Camera texture
2. Specular: Rainbow texture
3. Alpha: Segmentation Mask
Add a rectangle.
Rename it Quad4_face.
Add another rectangle.
Rename it Quad4_bkgrd.
Add a material.
Rename it Quad4_bkgrd.
Change material properties:
1. Diffuse : Background of your choice
2. Alpha: Segmentation Mask
3. Select Invert.
Superimpose
Foreground
Background
x
y
z
UNPACK
UNPACK
x
y
z
PACK
PACK
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
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?
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 |
Copy of G11_C2
By Sanjukta Bhattacharya
Copy of G11_C2
- 143