Set Up Visual Studio Code & Python
- Open the Visual Studio Code
- Click File - Open Folder
- Using this dialog, create a programming folder in your ICS folder and then create
- a folder called 'FirstProgram' and then click 'select folder'
- When asked to trust, click the option that says trust anything in the parent Folder
- Click New File
- Click Python File
- Copy and paste the code below into your new file
- Try to run the file by pressing the play button near the top right >
- Give the file a name like 'Sample' or 'test' (it should add the .py file extension itself)
- When it says it can't find pygame in the console type this below and hit enter:
- pip install pygame (in the terminal tab at the bottom)
- Click at the bottom right the python version (to the right of where you see a little face)
- Choose Python 3.11 (recommended)
- Try running it now and it should work.
Pygame Starter Code
# Example file showing a basic pygame "game loop"
import pygame
# pygame setup
pygame.display.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
# RENDER YOUR GAME HERE
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Each time a key is pressed a new 'b' is added
if event.type == pygame.KEYDOWN:
keysDown = pygame.key.get_pressed()
if keysDown[pygame.K_SPACE]:
if pCount <=0:
b.append([pX,pY-5])
pCount = 10
if event.type == pygame.KEYUP:
keysDown = pygame.key.get_pressed()
#print(event.key)
#keysDown = pygame.key.get_pressed() # Get the state of all keys
#holding a or d move pX left or right.
if keysDown[pygame.K_a]:
pX -= pSpeed
if keysDown[pygame.K_d]:
pX += pSpeed# Example file showing a basic pygame "game loop"
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
# basic font for user typed
base_font = pygame.font.Font(None, 32)
user1_text = ''
user2_text = ''
# create rectangle
input1_rect = pygame.Rect(200, 200, 140, 32)
input2_rect = pygame.Rect(400, 200, 140, 32)
active1 = False
active2 = False
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
# Check for backspace
if event.key == pygame.K_BACKSPACE:
# get text input from 0 to -1 i.e. end.
if(active1):
user_text1 = user1_text[:-1]
if(active2):
user_text2 = user2_text[:-1]
# Unicode standard is used for string
# formation
else:
if(active1):
user1_text += event.unicode
if(active2):
user2_text += event.unicode
if event.type == pygame.MOUSEBUTTONDOWN:
if input1_rect.collidepoint(event.pos):
active1 = True
else:
active1 = False
if input2_rect.collidepoint(event.pos):
active2 = True
else:
active2 = False
#print(pygame.mouse.get_pos())
print("x=" + str(pygame.mouse.get_pos()[0]))
print("y=" + str(pygame.mouse.get_pos()[1]))
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
# RENDER YOUR GAME HERE
if active1:
pygame.draw.rect(screen, (150,150,250), input1_rect)
else:
pygame.draw.rect(screen, (150, 150, 150), input1_rect)
pygame.draw.rect(screen, (150, 150, 150), input2_rect)
text1_surface = base_font.render(user1_text, True, (255, 255, 255))
text2_surface = base_font.render(user2_text, True, (255, 255, 255))
# render at position stated in arguments
screen.blit(text1_surface, (input1_rect.x + 5, input1_rect.y + 5))
screen.blit(text2_surface, (input2_rect.x + 5, input2_rect.y + 5))
# set width of textfield so that text cannot get
# outside of user's text input
input1_rect.w = max(100, text1_surface.get_width() + 10)
input2_rect.w = max(100, text2_surface.get_width() + 10)
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()Key resource for learning Python:
Python Tutorial (w3schools.com)
Need print command, quotes, brackets
1. Say "Hello, World!" With Python | HackerRank
Need casting, types, *,+,- and input command
2. Arithmetic Operators | HackerRank
Integer vs float division
3. Python: Division | HackerRank
Conditional Statement (Tricky, learn mod (%) first)
4. Python If-Else | HackerRank
Adding with a string, working within functions
5. What's Your Name? | HackerRank
Basic loops - for/while
6. Print Function | HackerRank
Create your own function (Tricky):
7. Write a function | HackerRank
Working with lists (takes time)
8. Lists | HackerRank
Use a list: (A little tricky)
9. Find the Runner-Up Score! | HackerRank
String manipulation (use upper() and lower() commands)
10. sWAP cASE | HackerRank
String (split)
11. String Split and Join | HackerRank
String Slicing (see example in problem definition)
12. Mutations | HackerRank
Substring - use 'in'
13. Find a string | HackerRank
String practice
14. Minion Game | HackerRank
Math practice - please sign up as needed for the competition
15. DSBN Codes Shape Math