├── README.md ├── player.png ├── coinGold.png ├── stickman ├── stick-L1.gif ├── stick-L2.gif ├── stick-L3.gif ├── stick-L4.gif ├── stick-R1.gif ├── stick-R2.gif ├── stick-R3.gif ├── stick-R4.gif ├── platform1.gif ├── platform2.gif └── platform3.gif ├── guess.py ├── rps.py ├── colorflag.py ├── bouncing balls.py ├── turtle functions.py ├── collect the coins.py ├── pong game.py └── stickman jump.py /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/player.png -------------------------------------------------------------------------------- /coinGold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/coinGold.png -------------------------------------------------------------------------------- /stickman/stick-L1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-L1.gif -------------------------------------------------------------------------------- /stickman/stick-L2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-L2.gif -------------------------------------------------------------------------------- /stickman/stick-L3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-L3.gif -------------------------------------------------------------------------------- /stickman/stick-L4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-L4.gif -------------------------------------------------------------------------------- /stickman/stick-R1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-R1.gif -------------------------------------------------------------------------------- /stickman/stick-R2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-R2.gif -------------------------------------------------------------------------------- /stickman/stick-R3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-R3.gif -------------------------------------------------------------------------------- /stickman/stick-R4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/stick-R4.gif -------------------------------------------------------------------------------- /stickman/platform1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/platform1.gif -------------------------------------------------------------------------------- /stickman/platform2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/platform2.gif -------------------------------------------------------------------------------- /stickman/platform3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kidscancode/intro-python-code/HEAD/stickman/platform3.gif -------------------------------------------------------------------------------- /guess.py: -------------------------------------------------------------------------------- 1 | # KidsCanCode - Intro to Programming 2 | # Random number guessing game 3 | import random 4 | 5 | secret_number = random.randrange(1, 101) 6 | 7 | guess = 0 8 | tries = 0 9 | while guess != secret_number: 10 | guess = int(input("Guess a number: ")) 11 | tries = tries + 1 12 | 13 | if guess < secret_number: 14 | print("Too low!") 15 | elif guess > secret_number: 16 | print("Too high!") 17 | else: 18 | print("You got it!") 19 | print("Number of tries: ", tries) 20 | 21 | 22 | -------------------------------------------------------------------------------- /rps.py: -------------------------------------------------------------------------------- 1 | # KidsCanCode - Intro to Programming 2 | # Rock/Paper/Scissors game 3 | import random 4 | 5 | choices = ['r', 'p', 's'] 6 | 7 | player_wins = ['pr', 'rs', 'sp'] 8 | 9 | player_score = 0 10 | computer_score = 0 11 | 12 | while True: 13 | player_move = input("Your move? ") 14 | if player_move == 'q': 15 | break 16 | 17 | computer_move = random.choice(choices) 18 | 19 | print("You:", player_move) 20 | print("Me:", computer_move) 21 | 22 | combo = player_move + computer_move 23 | 24 | if player_move == computer_move: 25 | print("Tie!") 26 | elif combo in player_wins: 27 | print("You win!") 28 | player_score += 1 29 | else: 30 | print("You lose!") 31 | computer_score += 1 32 | 33 | print("SCORES:") 34 | print("You: ", player_score) 35 | print("Me: ", computer_score) 36 | 37 | -------------------------------------------------------------------------------- /colorflag.py: -------------------------------------------------------------------------------- 1 | # KidsCanCode - Intro to Programming 2 | import turtle 3 | fred = turtle.Pen() 4 | fred.speed(10) 5 | 6 | colors = ["red", "orange", "yellow", "seagreen", 7 | "orchid", "royalblue", "dodgerblue"] 8 | 9 | fred.up() 10 | fred.goto(-320, -195) 11 | fred.width(70) 12 | # Draw the flag with a loop from the color list 13 | for draw_color in colors: 14 | fred.color(draw_color) 15 | fred.down() 16 | fred.forward(640) 17 | fred.up() 18 | fred.backward(640) 19 | fred.left(90) 20 | fred.forward(66) 21 | fred.right(90) 22 | 23 | fred.width(25) 24 | fred.color("white") 25 | fred.goto(0, -170) 26 | fred.down() 27 | fred.circle(170) # draw the circle 28 | fred.left(90) 29 | fred.forward(340) # draw the vertical line 30 | fred.left(180) 31 | fred.forward(170) # go back to the center 32 | fred.right(45) 33 | fred.forward(170) # draw a leg 34 | -------------------------------------------------------------------------------- /bouncing balls.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import time 3 | import random 4 | 5 | WIDTH = 800 6 | HEIGHT = 500 7 | 8 | tk = Tk() 9 | canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="black") 10 | tk.title("Drawing") 11 | canvas.pack() 12 | 13 | colors = ['red', 'green', 'blue', 'orange', 'yellow', 'cyan', 'magenta', 14 | 'dodgerblue', 'turquoise', 'grey', 'gold', 'pink'] 15 | 16 | class Ball: 17 | def __init__(self): 18 | self.size = random.randrange(200, 400) 19 | color = random.choice(colors) 20 | self.shape = canvas.create_rectangle(0, 0, self.size, self.size, fill=color) 21 | self.speedx = random.randrange(1, 10) 22 | self.speedy = random.randrange(1, 10) 23 | 24 | def update(self): 25 | canvas.move(self.shape, self.speedx, self.speedy) 26 | pos = canvas.coords(self.shape) 27 | if pos[2] >= WIDTH or pos[0] <= 0: 28 | self.speedx *= -1 29 | if pos[3] >= HEIGHT or pos[1] <= 0: 30 | self.speedy *= -1 31 | 32 | ball_list = [] 33 | for i in range(100): 34 | ball_list.append(Ball()) 35 | while True: 36 | for ball in ball_list: 37 | ball.update() 38 | tk.update() 39 | time.sleep(0.01) 40 | -------------------------------------------------------------------------------- /turtle functions.py: -------------------------------------------------------------------------------- 1 | # KidsCanCode - Intro to Programming 2 | # Functions example using turtles - draw random shapes 3 | import turtle 4 | import random 5 | fred = turtle.Pen() 6 | 7 | colors = ["red", "green", "blue"] 8 | fred.speed(20) 9 | 10 | # Define how to draw a square 11 | def square(length): 12 | for i in range(4): 13 | fred.forward(length) 14 | fred.left(90) 15 | 16 | # Define how to draw a triangle 17 | def triangle(length): 18 | for i in range(3): 19 | fred.forward(length) 20 | fred.left(120) 21 | 22 | for i in range(100): 23 | x = random.randrange(-200, 200) # Pick a random number for x 24 | y = random.randrange(-200, 200) # Pick a random number for y 25 | size = random.randrange(10, 100) # Pick a random size for the shape 26 | c = random.choice(colors) # Pick a random color 27 | width = random.randrange(1, 10) # Pick a random line width 28 | fred.up() # Pick up the pen before we move 29 | fred.goto(x,y) # Go to the random coordinate 30 | fred.down() # Don't forget to put the pen down 31 | fred.color(c) # Set the color 32 | fred.width(width) # Set the width 33 | triangle(size) # Draw the shape! 34 | 35 | x = random.randrange(-200, 200) 36 | y = random.randrange(-200, 200) 37 | size = random.randrange(10, 100) 38 | c = random.choice(colors) 39 | width = random.randrange(1, 10) 40 | fred.up() 41 | fred.goto(x,y) 42 | fred.down() 43 | fred.color(c) 44 | fred.width(width) 45 | square(size) 46 | -------------------------------------------------------------------------------- /collect the coins.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import time 3 | import random 4 | 5 | WIDTH = 800 6 | HEIGHT = 600 7 | 8 | tk = Tk() 9 | canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="black") 10 | tk.title("Moving around") 11 | canvas.pack() 12 | 13 | player = canvas.create_rectangle(0, 0, 50, 50, fill="red") 14 | 15 | def move(event): 16 | canvas.coords(player, event.x, event.y, event.x+50, event.y+50) 17 | 18 | canvas.bind("", move) 19 | 20 | def make_coin(): 21 | x = random.randrange(WIDTH - 30) 22 | y = random.randrange(HEIGHT - 30) 23 | coin = canvas.create_oval(x, y, x+30, y+30, fill="gold") 24 | return coin 25 | 26 | coins = [] 27 | for i in range(10): 28 | coins.append(make_coin()) 29 | 30 | level = 1 31 | score = 0 32 | timer = 30 33 | time_limit = 30 34 | time_started = time.time() 35 | score_label = canvas.create_text(5, 5, anchor=NW, text="Score: 0", fill="white", 36 | font=('Arial', 24)) 37 | time_label = canvas.create_text(5, 45, anchor=NW, text="Timer: 30", fill="white", 38 | font=('Arial', 24)) 39 | 40 | while timer > 0: 41 | timer = time_limit - int(time.time() - time_started) 42 | canvas.itemconfig(score_label, text="Score: "+str(score)) 43 | canvas.itemconfig(time_label, text="Timer: "+str(timer)) 44 | pos = canvas.coords(player) 45 | hits = canvas.find_overlapping(*pos) 46 | for coin in coins: 47 | if coin in hits: 48 | score += 10 49 | canvas.delete(coin) 50 | coins.remove(coin) 51 | if len(coins) == 0: 52 | level += 1 53 | for i in range(level * 10): 54 | coins.append(make_coin()) 55 | tk.update() 56 | time.sleep(0.01) 57 | -------------------------------------------------------------------------------- /pong game.py: -------------------------------------------------------------------------------- 1 | # Simple pong game - don't let the ball hit the bottom! 2 | # KidsCanCode - Intro to Programming 3 | from tkinter import * 4 | import random 5 | import time 6 | 7 | # Define ball properties and functions 8 | class Ball: 9 | def __init__(self, canvas, color, size, paddle): 10 | self.canvas = canvas 11 | self.paddle = paddle 12 | self.id = canvas.create_oval(10, 10, size, size, fill=color) 13 | self.canvas.move(self.id, 245, 100) 14 | self.xspeed = random.randrange(-3,3) 15 | self.yspeed = -1 16 | self.hit_bottom = False 17 | self.score = 0 18 | 19 | def draw(self): 20 | self.canvas.move(self.id, self.xspeed, self.yspeed) 21 | pos = self.canvas.coords(self.id) 22 | if pos[1] <= 0: 23 | self.yspeed = 3 24 | if pos[3] >= 400: 25 | self.hit_bottom = True 26 | if pos[0] <= 0: 27 | self.xspeed = 3 28 | if pos[2] >= 500: 29 | self.xspeed = -3 30 | if self.hit_paddle(pos) == True: 31 | self.yspeed = -3 32 | self.xspeed = random.randrange(-3,3) 33 | self.score += 1 34 | 35 | def hit_paddle(self, pos): 36 | paddle_pos = self.canvas.coords(self.paddle.id) 37 | if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]: 38 | if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]: 39 | return True 40 | return False 41 | 42 | # Define paddle properties and functions 43 | class Paddle: 44 | def __init__(self, canvas, color): 45 | self.canvas = canvas 46 | self.id = canvas.create_rectangle(0,0, 100, 10, fill=color) 47 | self.canvas.move(self.id, 200, 300) 48 | self.xspeed = 0 49 | self.canvas.bind_all('', self.move_left) 50 | self.canvas.bind_all('', self.move_right) 51 | 52 | def draw(self): 53 | self.canvas.move(self.id, self.xspeed, 0) 54 | pos = self.canvas.coords(self.id) 55 | if pos[0] <= 0: 56 | self.xspeed = 0 57 | if pos[2] >= 500: 58 | self.xspeed = 0 59 | 60 | def move_left(self, evt): 61 | self.xspeed = -2 62 | def move_right(self, evt): 63 | self.xspeed = 2 64 | 65 | # Create window and canvas to draw on 66 | tk = Tk() 67 | tk.title("Ball Game") 68 | canvas = Canvas(tk, width=500, height=400, bd=0, bg='papaya whip') 69 | canvas.pack() 70 | label = canvas.create_text(5, 5, anchor=NW, text="Score: 0") 71 | tk.update() 72 | paddle = Paddle(canvas, 'blue') 73 | ball = Ball(canvas, 'red', 25, paddle) 74 | 75 | # Animation loop 76 | while ball.hit_bottom == False: 77 | ball.draw() 78 | paddle.draw() 79 | canvas.itemconfig(label, text="Score: "+str(ball.score)) 80 | tk.update_idletasks() 81 | tk.update() 82 | time.sleep(0.01) 83 | 84 | # Game Over 85 | go_label = canvas.create_text(250,200,text="GAME OVER",font=("Helvetica",30)) 86 | tk.update() 87 | -------------------------------------------------------------------------------- /stickman jump.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import time 3 | import random 4 | 5 | WIDTH = 800 6 | HEIGHT = 500 7 | 8 | tk = Tk() 9 | canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="light blue") 10 | tk.title("Jump Around") 11 | canvas.pack() 12 | 13 | class Player: 14 | def __init__(self): 15 | self.frames_l = [PhotoImage(file="stickman/stick-L1.gif"), 16 | PhotoImage(file="stickman/stick-L2.gif"), 17 | PhotoImage(file="stickman/stick-L3.gif"), 18 | PhotoImage(file="stickman/stick-L4.gif")] 19 | self.frames_r = [PhotoImage(file="stickman/stick-R1.gif"), 20 | PhotoImage(file="stickman/stick-R2.gif"), 21 | PhotoImage(file="stickman/stick-R3.gif"), 22 | PhotoImage(file="stickman/stick-R4.gif")] 23 | 24 | self.image = canvas.create_image(WIDTH / 2, HEIGHT-100, anchor=NW, 25 | image=self.frames_l[0]) 26 | self.speedx = 0 27 | self.speedy = 0 28 | canvas.bind_all("", self.move) 29 | canvas.bind_all("", self.move) 30 | canvas.bind_all("", self.jump) 31 | canvas.bind_all("", self.stop) 32 | canvas.bind_all("", self.stop) 33 | self.jumping = False 34 | self.current_frame = 0 35 | self.last_time = time.time() 36 | 37 | def jump(self, event): 38 | if not self.jumping: 39 | self.jumping = True 40 | self.speedy = -20 41 | 42 | def stop(self, event): 43 | self.speedx = 0 44 | 45 | def move(self, event): 46 | if event.keysym == 'Right': 47 | self.speedx = 6 48 | if event.keysym == 'Left': 49 | self.speedx = -6 50 | 51 | def animate(self): 52 | now = time.time() 53 | if now - self.last_time > 0.05: 54 | self.last_time = now 55 | self.current_frame = (self.current_frame + 1) % 4 56 | if self.speedx < 0: 57 | canvas.itemconfig(self.image, image=self.frames_l[self.current_frame]) 58 | if self.speedx > 0: 59 | canvas.itemconfig(self.image, image=self.frames_r[self.current_frame]) 60 | 61 | def update(self): 62 | self.animate() 63 | self.speedy += 1 64 | canvas.move(self.image, self.speedx, self.speedy) 65 | pos = canvas.coords(self.image) 66 | self.pos = pos 67 | left, right = pos[0], pos[0]+27 68 | top, bottom = pos[1], pos[1]+30 69 | # check if player hits a platform 70 | for plat in platforms: 71 | if self.speedy > 0: 72 | hits = canvas.find_overlapping(left, top, right, bottom) 73 | if plat.image in hits: 74 | self.jumping = False 75 | self.speedy = 0 76 | canvas.coords(self.image, left, plat.top-30) 77 | 78 | # wrap around edges of screen 79 | if right > WIDTH: 80 | canvas.coords(self.image, 0, top) 81 | if left < 0: 82 | canvas.coords(self.image, WIDTH-27, top) 83 | 84 | class Platform: 85 | def __init__(self, x, y, w, h): 86 | self.image = canvas.create_rectangle(x, y, x+w, y+h, fill="green") 87 | self.top = y 88 | 89 | player = Player() 90 | platforms = [] 91 | plat1 = Platform(0, HEIGHT-40, WIDTH, 40) 92 | platforms.append(plat1) 93 | platforms.append(Platform(150, 205, 100, 40)) 94 | platforms.append(Platform(450, 120, 100, 40)) 95 | platforms.append(Platform(375, 320, 200, 40)) 96 | platforms.append(Platform(45, 20, 100, 40)) 97 | score = 0 98 | score_label = canvas.create_text(WIDTH / 2, 30, text="0", font=('Arial', 22)) 99 | while True: 100 | canvas.itemconfig(score_label, text=str(score)) 101 | player.update() 102 | # die! 103 | if player.pos[1] > HEIGHT: 104 | break 105 | # scroll when player reaches top 1/4 of screen 106 | if player.pos[1] < HEIGHT / 4 and player.speedy < 0: 107 | canvas.move(player.image, 0, -player.speedy) 108 | for plat in platforms: 109 | canvas.move(plat.image, 0, -player.speedy) 110 | plat.top -= player.speedy 111 | if plat.top > HEIGHT: 112 | score += 10 113 | canvas.delete(plat.image) 114 | platforms.remove(plat) 115 | while len(platforms) < 6: 116 | newplat = Platform(random.randrange(0, WIDTH-100), 117 | random.randrange(-50, -30), 118 | random.randrange(50, 150), 119 | 40) 120 | platforms.append(newplat) 121 | tk.update() 122 | time.sleep(0.01) 123 | --------------------------------------------------------------------------------