Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Basic shapes

What am I Learning today?

I am learning how to use code and co-ordinates to create straight lines in python
I am learning how to draw shapes using just straight lines in python
I am learning how to draw squares and other shapes using co-ordinates and dimensions

Knowledge Organiser

×

Lesson

Task 1 - Getting Started Click to see more

You are going to screenshot all of your work into a Powerpoint file.

To download the PowerPoint by clicking on the image below

To save your powerpoint go to:

  • File explorer
  • -> Year 9 Common Area :S
  • -> You can save here
  • -> Computer Science
  • -> Term 3
  • -> Lesson 1
  • Save the Powerpoint file: as your name


  • Complete Task 1 in your Powerpoint


  • Task 2 - Introducing Pygame - Getting started Click to see more

    Setting up Pygame

    1. Open Thonny and click on Manage packages...
    2. Type in pygame into the search bar click Find Packages from PyPi
    3. Then Click on the Install Button

    Your screen should look like this



    Task 3 - Using the code template - Getting started 2 Click to see more

    Using the code template

    • In the Powerpoint you have just downloaded and saved there is a slide containing the text for our coding template.
    • Copy the text into Thonny and then run your code.
    • Template Code

      
      # pygame template - LearnItWithMrC
      import pygame  # accesses pygame files
      import sys  # to communicate with windows
      
      # game setup ################ only runs once
      pygame.init()  # starts the game engine
      clock = pygame.time.Clock()  # creates clock to limit frames per second
      FPS = 60  # sets max speed of main loop
      SCREENWIDTH = 640 # sets the width of the screen/window
      SCREENHEIGHT = 480  # sets the height of the screen/window
      # creates window and game screen with twin value
      screen = pygame.display.set_mode((SCREENWIDTH ,SCREENHEIGHT)) 
      # set variables for colors RGB (0-255)
      white = (255, 255, 255)
      black = (0, 0, 0)
      red = (255, 0, 0)
      yellow = (255, 255, 0)
      green = (0, 255, 0)
      
      ####################### Your Variables go below here  #################
      
      
      #######################################################################
      
      # game loop #################### runs 60 times a second! 
      while True:  # game loop - note:  everything in the mainloop is indented one tab
      
          for event in pygame.event.get():  # get user interaction events
              if event.type == pygame.QUIT:  # tests if window's X (close) has been clicked
                  pygame.quit()   # stops the game engine
                  sys.exit()  # close operating system window
                  
          # your code starts here ##############################
      
      
          # your code ends here ############################### 
          
          pygame.display.flip()  # updates the entire screen
          clock.tick(FPS)  # limits game to frame per second, FPS value
      
      # out of game loop ###############
      
      

      This will not work if you have not installed pygame you will just get an error !!!


    • When you run your code you should see a game window appear that is 640 by 480 like the example below.

  • Can you work out how to change the screen size to 1024 by 768 ?

  • When you are finished screenshot your working template code into your Learning Journal


  • Task 4 - Using co-ordinates Click to see more

  • When we want to draw something in pygame we have to use the screen co-ordinates.
  • Unlike programs like Scratch, Pygame and most game engines have the origin for their co-ordinates (x = 0 , y = 0) at the top left hand corner of the screen.
  • You can see here in our image above that a pixel, has been placed at the co-ordinates x = 30 , y = 40 which we would write (30,40) from the top left hand corner of the screen.
  • Using that basic understanding let's see if we can draw a line.

  • Drawing a line:

  • In order to draw a line we need the co-ordinates for the start of the line and the co-ordinates for the end of the line. We are going to start in the top left corner so thats easy it will be (0,0) and we are going to draw a line to the co-ordinates we used above (30,40)
  • This is the function we need to use in pygame to draw a line:

  • pygame.draw.line(Where?,color?,(start x? start y?) (end x?, end y?) line thickness?)
  • So lets fill it in with the co-ordinates we mentioned above (0,0)and (30,40) and we will use the colour green and a line thickness of 2

  • pygame.draw.line(screen, green, (0,0), (30,40), 2)
  • Look at the image below to see where to put this line of code in your template. hint: it goes between the lines that say 'your code starts here' and 'your code ends here'.

  • Run your code and you should get a small geen line drawn from the corner of your screen?


  • Can you draw a longer thicker line?
  • Can you draw another line that starts where your fist line ends?
  • Can you draw a shape like a triangle or a square?

  • When you are finished screenshot your working template code into your Learning Journal


  • Task 5 - Drawing shapes Click to see more

    Drawing a rectangle:

    In order to draw a rectangle we only need the co-ordinates for the top left hand corner of the rectangle and then the height and width in pixels.

  • pygame.draw.rect(screen, green, (30,40,50,20))
  • When you run your code it should look like this:


  • Can you draw a square?
  • Can you use a different colour?
  • Can you complete the next task it might seem familiar?

  • When you are finished screenshot your working template code into your Learning Journal


  • Task 6 - Extend it Click to see more

    Can you draw the Microsoft logo?

  • Using the rectangle command and the line command for the white cross in the middle?

  • When you are finished screenshot your working template code into your Learning Journal


  • Task 7 - Making circles Click to see more

  • Can you draw a circle?
  • Can you draw a snowman using circles and some of the other shapes we have used today?


  • When you are finished screenshot your working template code into your Learning Journal

  • When you are finished screenshot your working template code into your Learning Journal


  • Task 8 - Lesson Review/Homework Click to see more


    Summing it all up

    Lets look at the learning outcomes and decide which one best describes our current level of understanding :

    Tick the one you feel is closest to your level

    Learning Outcomes I need to learn how to draw shapes using just straight lines in python

    • I have a basic understanding of how I can draw shapes using just straight lines in python with a little help from my teacher
    • I can show my teacher that I can draw shapes using just straight lines in python without their help.
    • I can draw shapes using just straight lines in python independently and I can also explain it to others and can complete any extension tasks I am given.

    🠜 Now update your learning objectivesClick on the Assessment image



    My Notes: Game Development

    Student_Comment_3 not found

    Task Notes/Comments - Add here Click to see more

    Comments/Notes