One of the issues with Pygame is its barrier to entry but it has great benefits in given kids something visual to play with which has its own intrinsic motivation. This is a link to all the files I will be using tonight as well as my notes (probably to be added later) on each step and where the pitfalls are.
Clearly, if you are an experienced programmer and familiar with objects, there are much better ways to design games but this project is scoped first and foremost to teach the bare minimum to get the kids going.
Files for use with the game
Steps to build the game in code (steps in text coming)
Step 1: a screen
#import stuff
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
#screen.fill((255, 255, 255))
#pygame.display.flip()
#setup vars and files
#instructions
#keeping track of cards
#managing time
#main loop
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
#if the quit button was pressed
#else if there was a keypress (any keypress)
#check if card matches
#increase score and print outcome
#update screen with outcome and pause
#reset time delay, update previous_card and get new card
#update screen
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
#screen.fill((255, 255, 255))
#pygame.display.flip()
#setup vars and files
#instructions
#keeping track of cards
#managing time
#main loop
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
#if the quit button was pressed
#else if there was a keypress (any keypress)
#check if card matches
#increase score and print outcome
#update screen with outcome and pause
#reset time delay, update previous_card and get new card
#update screen
Step 2: quitting
#import stuff
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
#instructions
#keeping track of cards
#managing time
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
#else if there was a keypress (any keypress)
#check if card matches
#increase score and print outcome
#update screen with outcome and pause
#reset time delay, update previous_card and get new card
#update screen
pygame.quit()
#import stuff
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
#instructions
#keeping track of cards
#managing time
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
#else if there was a keypress (any keypress)
#check if card matches
#increase score and print outcome
#update screen with outcome and pause
#reset time delay, update previous_card and get new card
#update screen
pygame.quit()
Step 3: Blit
#import stuff
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
#user_score = 0
#computer_score = 0
#instructions
#keeping track of cards
#managing time
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
#yet to implement
if pygame.key.get_pressed()[pygame.K_SPACE]:
#increase score and print outcome
#user_score = user_score + 1
print("You Win!")
#print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
#screen.blit(image, (300, 80))
#pygame.display.flip()
#pygame.time.wait(500)
#reset time delay, update previous_card and get new card
#update screen
#pygame.quit()
import pygame
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
#user_score = 0
#computer_score = 0
#instructions
#keeping track of cards
#managing time
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
#yet to implement
if pygame.key.get_pressed()[pygame.K_SPACE]:
#increase score and print outcome
#user_score = user_score + 1
print("You Win!")
#print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
#screen.blit(image, (300, 80))
#pygame.display.flip()
#pygame.time.wait(500)
#reset time delay, update previous_card and get new card
#update screen
#pygame.quit()
Step 4: Cards
#import stuff
import pygame, time, random
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
user_score = 0
computer_score = 0
cards = ["ace.png","2.png","3.png","4.png"]
#instructions
#keeping track of cards
previous_card = "0"
card = cards[0]
#managing time
sec_to_add = random.randint(2,5)
time_delay = time.time()+sec_to_add
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
if previous_card == card:
#increase score and print outcome
user_score = user_score + 1
print("You Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
screen.blit(image, (300, 80))
pygame.display.flip()
pygame.time.wait(500)
#reset time delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
#screen.fill((255, 255, 255))
#pygame.display.flip()
#pygame.time.wait(200)
image = pygame.image.load(card)
screen.blit(image, (10, 100))
pygame.display.flip()
pygame.quit()
import pygame, time, random
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
user_score = 0
computer_score = 0
cards = ["ace.png","2.png","3.png","4.png"]
#instructions
#keeping track of cards
previous_card = "0"
card = cards[0]
#managing time
sec_to_add = random.randint(2,5)
time_delay = time.time()+sec_to_add
#main loop
while not game_over:
#check time passed (slow computer down)
#if there is a snap event
#increase score and print outcome
#update screen and pause
#reset time_delay, update previous_card and get new card
#update screen
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
if previous_card == card:
#increase score and print outcome
user_score = user_score + 1
print("You Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
screen.blit(image, (300, 80))
pygame.display.flip()
pygame.time.wait(500)
#reset time delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
#screen.fill((255, 255, 255))
#pygame.display.flip()
#pygame.time.wait(200)
image = pygame.image.load(card)
screen.blit(image, (10, 100))
pygame.display.flip()
pygame.quit()
Step 5: Computer Play (final ish)
#import stuff
import pygame, time, random
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
user_score = 0
computer_score = 0
cards = ["ace.png","2.png","3.png","4.png"]
#instructions
print("Press any key to SNAP!")
#keeping track of cards
previous_card = "0"
card = cards[0]
#managing time
sec_to_add = random.randint(2,5)
time_delay = time.time()+sec_to_add
#main loop
while not game_over:
#check time passed (slow computer down)
if time.time() > time_delay:
if previous_card == card:
#increase score and print outcome
computer_score = computer_score + 1
print("Computer Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
#update screen and pause
image = pygame.image.load("snap_computer.png")
screen.blit(image, (300, 240))
pygame.display.flip()
pygame.time.wait(500)
#reset time_delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
screen.fill((255, 255, 255))
pygame.display.flip()
pygame.time.wait(200)
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
if previous_card == card:
#increase score and print outcome
user_score = user_score + 1
print("You Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
screen.blit(image, (300, 80))
pygame.display.flip()
pygame.time.wait(500)
#reset time delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
screen.fill((255, 255, 255))
pygame.display.flip()
pygame.time.wait(200)
image = pygame.image.load(card)
screen.blit(image, (10, 100))
pygame.display.flip()
pygame.quit()
import pygame, time, random
#setup screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
pygame.display.flip()
#setup vars and files
game_over = False
user_score = 0
computer_score = 0
cards = ["ace.png","2.png","3.png","4.png"]
#instructions
print("Press any key to SNAP!")
#keeping track of cards
previous_card = "0"
card = cards[0]
#managing time
sec_to_add = random.randint(2,5)
time_delay = time.time()+sec_to_add
#main loop
while not game_over:
#check time passed (slow computer down)
if time.time() > time_delay:
if previous_card == card:
#increase score and print outcome
computer_score = computer_score + 1
print("Computer Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
#update screen and pause
image = pygame.image.load("snap_computer.png")
screen.blit(image, (300, 240))
pygame.display.flip()
pygame.time.wait(500)
#reset time_delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
screen.fill((255, 255, 255))
pygame.display.flip()
pygame.time.wait(200)
#check for events
for event in pygame.event.get():
#if the quit button was pressed
if event.type == pygame.QUIT:
print("Game over")
game_over = True
elif event.type == pygame.KEYDOWN:
#check if card matches -- faking this
if previous_card == card:
#increase score and print outcome
user_score = user_score + 1
print("You Win!")
print("Computer: " + str(computer_score) + " User: " + str(user_score))
image = pygame.image.load("snap.png")
else:
image = pygame.image.load("boo.png")
#update screen with outcome and pause
screen.blit(image, (300, 80))
pygame.display.flip()
pygame.time.wait(500)
#reset time delay, update previous_card and get new card
time_delay = time.time() + random.randint(2,5)
previous_card = card
card = random.choice(cards)
#update screen
screen.fill((255, 255, 255))
pygame.display.flip()
pygame.time.wait(200)
image = pygame.image.load(card)
screen.blit(image, (10, 100))
pygame.display.flip()
pygame.quit()
All files can be found here: https://drive.google.com/drive/u/0/folders/0BzfvVt8-JyCmR0F5bmJOZzh6MVU
No comments:
Post a Comment