Code:
import pygame
import time
import random
speed = 0.025
def lose():
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Lose')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 50)
# Render the text
text = font.render('YOU LOSE', True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.key == pygame.K_DOWN:
game()
def win():
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Win')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 50)
# Render the text
text = font.render('YOU WIN', True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.key == pygame.K_DOWN:
game()
def game():
pygame.display.set_caption('Falling Squares')
enemyspawns = []
enemyspawnsvar = -1
for x in xrange(40):
enemyspawnsvar = enemyspawnsvar + 1
enemyspawns.append(enemyspawnsvar)
#Set appearance values.
background_colour = (0, 0, 0)
dot_colour = (255, 0, 0)
#Put the dot at (0, 0) in the grid.
position_x = 20
position_y=39
#Make dots 5x5 so they're easy to see.
dot_width = dot_height = 5
#Initialize PyGame and create a 200x200 window.
pygame.init()
screen = pygame.display.set_mode((200, 200))
#Create the dot.
dot = pygame.Surface((dot_width, dot_height))
dot.fill(dot_colour)
#enemy
enemy_colour = (0,255,0)
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
enemy_width = enemy_height = 5
enemy = pygame.Surface((enemy_width, enemy_height))
enemy.fill(enemy_colour)
#enemy2
enemy2_colour = (0,255,0)
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
enemy2_width = enemy2_height = 5
enemy2 = pygame.Surface((enemy_width, enemy_height))
enemy2.fill(enemy_colour)
#enemy3
enemy3_colour = (0,255,0)
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
enemy3_width = enemy3_height = 5
enemy3 = pygame.Surface((enemy_width, enemy_height))
enemy3.fill(enemy_colour)
#enemy4
enemy4_colour = (0,255,0)
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
enemy4_width = enemy4_height = 5
enemy4 = pygame.Surface((enemy_width, enemy_height))
enemy4.fill(enemy_colour)
#enemy5
enemy5_colour = (0,255,0)
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
enemy5_width = enemy5_height = 5
enemy5 = pygame.Surface((enemy_width, enemy_height))
enemy5.fill(enemy_colour)
#enemy6
enemy6_colour = (0,255,0)
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
enemy6_width = enemy6_height = 5
enemy6 = pygame.Surface((enemy_width, enemy_height))
enemy6.fill(enemy_colour)
#enemy7
enemy7_colour = (0,255,0)
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
enemy7_width = enemy7_height = 5
enemy7 = pygame.Surface((enemy_width, enemy_height))
enemy7.fill(enemy_colour)
#enemy8
enemy8_colour = (0,255,0)
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
enemy8_width = enemy8_height = 5
enemy8 = pygame.Surface((enemy_width, enemy_height))
enemy8.fill(enemy_colour)
#enemy9
enemy9_colour = (0,255,0)
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
enemy9_width = enemy9_height = 5
enemy9 = pygame.Surface((enemy_width, enemy_height))
enemy9.fill(enemy_colour)
#enemy10
enemy10_colour = (0,255,0)
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
enemy10_width = enemy10_height = 5
enemy10 = pygame.Surface((enemy_width, enemy_height))
enemy10.fill(enemy_colour)
#enemy11
enemy11_colour = (0,255,0)
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
enemy11_width = enemy11_height = 5
enemy11 = pygame.Surface((enemy_width, enemy_height))
enemy11.fill(enemy_colour)
#enemy12
enemy12_colour = (0,255,0)
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
enemy12_width = enemy12_height = 5
enemy12 = pygame.Surface((enemy_width, enemy_height))
enemy12.fill(enemy_colour)
while True:
#enemy
if enemy_position_y < 40:
enemy_position_y += 1
elif enemy_position_y == 40:
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
if enemy_position_x == position_x and enemy_position_y == position_y:
lose()
#enemy2
if enemy2_position_y < 40:
enemy2_position_y += 1
elif enemy2_position_y == 40:
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
if enemy2_position_x == position_x and enemy2_position_y == position_y:
lose()
#enemy3
if enemy3_position_y < 40:
enemy3_position_y += 1
elif enemy3_position_y == 40:
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
if enemy3_position_x == position_x and enemy3_position_y == position_y:
lose()
#enemy4
if enemy4_position_y < 40:
enemy4_position_y += 1
elif enemy4_position_y == 40:
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
if enemy4_position_x == position_x and enemy4_position_y == position_y:
lose()
#enemy5
if enemy5_position_y < 40:
enemy5_position_y += 1
elif enemy5_position_y == 40:
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
if enemy5_position_x == position_x and enemy5_position_y == position_y:
lose()
#enemy6
if enemy6_position_y < 40:
enemy6_position_y += 1
elif enemy6_position_y == 40:
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
if enemy6_position_x == position_x and enemy6_position_y == position_y:
lose()
#enemy7
if enemy7_position_y < 40:
enemy7_position_y += 1
elif enemy7_position_y == 40:
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
if enemy7_position_x == position_x and enemy7_position_y == position_y:
lose()
#enemy8
if enemy8_position_y < 40:
enemy8_position_y += 1
elif enemy8_position_y == 40:
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
if enemy8_position_x == position_x and enemy8_position_y == position_y:
lose()
#enemy9
if enemy9_position_y < 40:
enemy9_position_y += 1
elif enemy9_position_y == 40:
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
if enemy9_position_x == position_x and enemy9_position_y == position_y:
lose()
#enemy10
if enemy10_position_y < 40:
enemy10_position_y += 1
elif enemy10_position_y == 40:
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
if enemy10_position_x == position_x and enemy10_position_y == position_y:
lose()
#enemy11
if enemy11_position_y < 40:
enemy11_position_y += 1
elif enemy11_position_y == 40:
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
if enemy11_position_x == position_x and enemy11_position_y == position_y:
lose()
#enemy12
if enemy12_position_y < 40:
enemy12_position_y += 1
elif enemy12_position_y == 40:
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
if enemy12_position_x == position_x and enemy12_position_y == position_y:
lose()
# Controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_LEFT:
position_x -= 1
print position_x
elif event.key == pygame.K_RIGHT:
position_x += 1
print position_x
elif event.key == pygame.K_UP:
position_y -= 1
print position_y
elif event.key == pygame.K_DOWN:
position_y += 1
print position_y
#How to win
if position_y < 0:
win()
#Force the dot to stay within the window
if position_x < 0:
position_x = 0
if position_x > 39:
position_x = 39
if position_y > 39:
postion_y = 39
#Fill in the background
screen.fill(background_colour)
#Draw the dots.
screen.blit(dot, (position_x * dot_width, position_y * dot_height))
screen.blit(enemy, (enemy_position_x * enemy_width, enemy_position_y * enemy_height))
screen.blit(enemy2, (enemy2_position_x * enemy2_width, enemy2_position_y * enemy2_height))
screen.blit(enemy3, (enemy3_position_x * enemy3_width, enemy3_position_y * enemy3_height))
screen.blit(enemy4, (enemy4_position_x * enemy4_width, enemy4_position_y * enemy4_height))
screen.blit(enemy5, (enemy5_position_x * enemy5_width, enemy5_position_y * enemy5_height))
screen.blit(enemy6, (enemy6_position_x * enemy6_width, enemy6_position_y * enemy6_height))
screen.blit(enemy7, (enemy7_position_x * enemy7_width, enemy7_position_y * enemy7_height))
screen.blit(enemy8, (enemy8_position_x * enemy8_width, enemy8_position_y * enemy8_height))
screen.blit(enemy9, (enemy9_position_x * enemy9_width, enemy9_position_y * enemy9_height))
screen.blit(enemy10, (enemy10_position_x * enemy10_width, enemy10_position_y * enemy10_height))
screen.blit(enemy11, (enemy11_position_x * enemy11_width, enemy11_position_y * enemy11_height))
screen.blit(enemy12, (enemy12_position_x * enemy12_width, enemy12_position_y * enemy12_height))
#Display the new frame.
pygame.display.flip()
#Sleep
time.sleep(speed)
game()
The goal of the game is to get to the top of the map.
Once you get to the "You Win" or "You Lose" screen, you can use the down key to start over.
Known Errors:
If you put the mouse over the game window, the game crashes
The title of the window is "pygame window" Until you win or lose for the first time
If you know how to fix any of the known errors, please tell me.
Note that the cordites are printed in terminal for development purposes, and it is not actually part of the game
UPDATE:
Fixed Both of the known errors
Added a welcome screen
You now press space instead of down at the "welcome, "you win", and "you lose" screens
Code:
import pygame
import time
import random
speed = 0.025
def welcome():
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('Welcome')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Press Space To Begin', True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
game()
def lose():
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Lose')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 50)
# Render the text
text = font.render('YOU LOSE', True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
game()
def win():
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Win')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 50)
# Render the text
text = font.render('YOU WIN', True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
game()
def game():
pygame.display.set_caption('Falling Squares')
enemyspawns = []
enemyspawnsvar = -1
for x in xrange(40):
enemyspawnsvar = enemyspawnsvar + 1
enemyspawns.append(enemyspawnsvar)
#Set appearance values.
background_colour = (0, 0, 0)
dot_colour = (255, 0, 0)
#Put the dot at (0, 0) in the grid.
position_x = 20
position_y=39
#Make dots 5x5 so they're easy to see.
dot_width = dot_height = 5
#Initialize PyGame and create a 200x200 window.
pygame.init()
screen = pygame.display.set_mode((200, 200))
#Create the dot.
dot = pygame.Surface((dot_width, dot_height))
dot.fill(dot_colour)
#enemy
enemy_colour = (0,255,0)
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
enemy_width = enemy_height = 5
enemy = pygame.Surface((enemy_width, enemy_height))
enemy.fill(enemy_colour)
#enemy2
enemy2_colour = (0,255,0)
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
enemy2_width = enemy2_height = 5
enemy2 = pygame.Surface((enemy_width, enemy_height))
enemy2.fill(enemy_colour)
#enemy3
enemy3_colour = (0,255,0)
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
enemy3_width = enemy3_height = 5
enemy3 = pygame.Surface((enemy_width, enemy_height))
enemy3.fill(enemy_colour)
#enemy4
enemy4_colour = (0,255,0)
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
enemy4_width = enemy4_height = 5
enemy4 = pygame.Surface((enemy_width, enemy_height))
enemy4.fill(enemy_colour)
#enemy5
enemy5_colour = (0,255,0)
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
enemy5_width = enemy5_height = 5
enemy5 = pygame.Surface((enemy_width, enemy_height))
enemy5.fill(enemy_colour)
#enemy6
enemy6_colour = (0,255,0)
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
enemy6_width = enemy6_height = 5
enemy6 = pygame.Surface((enemy_width, enemy_height))
enemy6.fill(enemy_colour)
#enemy7
enemy7_colour = (0,255,0)
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
enemy7_width = enemy7_height = 5
enemy7 = pygame.Surface((enemy_width, enemy_height))
enemy7.fill(enemy_colour)
#enemy8
enemy8_colour = (0,255,0)
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
enemy8_width = enemy8_height = 5
enemy8 = pygame.Surface((enemy_width, enemy_height))
enemy8.fill(enemy_colour)
#enemy9
enemy9_colour = (0,255,0)
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
enemy9_width = enemy9_height = 5
enemy9 = pygame.Surface((enemy_width, enemy_height))
enemy9.fill(enemy_colour)
#enemy10
enemy10_colour = (0,255,0)
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
enemy10_width = enemy10_height = 5
enemy10 = pygame.Surface((enemy_width, enemy_height))
enemy10.fill(enemy_colour)
#enemy11
enemy11_colour = (0,255,0)
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
enemy11_width = enemy11_height = 5
enemy11 = pygame.Surface((enemy_width, enemy_height))
enemy11.fill(enemy_colour)
#enemy12
enemy12_colour = (0,255,0)
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
enemy12_width = enemy12_height = 5
enemy12 = pygame.Surface((enemy_width, enemy_height))
enemy12.fill(enemy_colour)
while True:
#enemy
if enemy_position_y < 40:
enemy_position_y += 1
elif enemy_position_y == 40:
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
if enemy_position_x == position_x and enemy_position_y == position_y:
lose()
#enemy2
if enemy2_position_y < 40:
enemy2_position_y += 1
elif enemy2_position_y == 40:
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
if enemy2_position_x == position_x and enemy2_position_y == position_y:
lose()
#enemy3
if enemy3_position_y < 40:
enemy3_position_y += 1
elif enemy3_position_y == 40:
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
if enemy3_position_x == position_x and enemy3_position_y == position_y:
lose()
#enemy4
if enemy4_position_y < 40:
enemy4_position_y += 1
elif enemy4_position_y == 40:
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
if enemy4_position_x == position_x and enemy4_position_y == position_y:
lose()
#enemy5
if enemy5_position_y < 40:
enemy5_position_y += 1
elif enemy5_position_y == 40:
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
if enemy5_position_x == position_x and enemy5_position_y == position_y:
lose()
#enemy6
if enemy6_position_y < 40:
enemy6_position_y += 1
elif enemy6_position_y == 40:
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
if enemy6_position_x == position_x and enemy6_position_y == position_y:
lose()
#enemy7
if enemy7_position_y < 40:
enemy7_position_y += 1
elif enemy7_position_y == 40:
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
if enemy7_position_x == position_x and enemy7_position_y == position_y:
lose()
#enemy8
if enemy8_position_y < 40:
enemy8_position_y += 1
elif enemy8_position_y == 40:
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
if enemy8_position_x == position_x and enemy8_position_y == position_y:
lose()
#enemy9
if enemy9_position_y < 40:
enemy9_position_y += 1
elif enemy9_position_y == 40:
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
if enemy9_position_x == position_x and enemy9_position_y == position_y:
lose()
#enemy10
if enemy10_position_y < 40:
enemy10_position_y += 1
elif enemy10_position_y == 40:
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
if enemy10_position_x == position_x and enemy10_position_y == position_y:
lose()
#enemy11
if enemy11_position_y < 40:
enemy11_position_y += 1
elif enemy11_position_y == 40:
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
if enemy11_position_x == position_x and enemy11_position_y == position_y:
lose()
#enemy12
if enemy12_position_y < 40:
enemy12_position_y += 1
elif enemy12_position_y == 40:
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
if enemy12_position_x == position_x and enemy12_position_y == position_y:
lose()
# Controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_LEFT:
position_x -= 1
print position_x
elif event.key == pygame.K_RIGHT:
position_x += 1
print position_x
elif event.key == pygame.K_UP:
position_y -= 1
print position_y
elif event.key == pygame.K_DOWN:
position_y += 1
print position_y
#How to win
if position_y < 0:
win()
#Force the dot to stay within the window
if position_x < 0:
position_x = 0
if position_x > 39:
position_x = 39
if position_y > 39:
postion_y = 39
#Fill in the background
screen.fill(background_colour)
#Draw the dots.
screen.blit(dot, (position_x * dot_width, position_y * dot_height))
screen.blit(enemy, (enemy_position_x * enemy_width, enemy_position_y * enemy_height))
screen.blit(enemy2, (enemy2_position_x * enemy2_width, enemy2_position_y * enemy2_height))
screen.blit(enemy3, (enemy3_position_x * enemy3_width, enemy3_position_y * enemy3_height))
screen.blit(enemy4, (enemy4_position_x * enemy4_width, enemy4_position_y * enemy4_height))
screen.blit(enemy5, (enemy5_position_x * enemy5_width, enemy5_position_y * enemy5_height))
screen.blit(enemy6, (enemy6_position_x * enemy6_width, enemy6_position_y * enemy6_height))
screen.blit(enemy7, (enemy7_position_x * enemy7_width, enemy7_position_y * enemy7_height))
screen.blit(enemy8, (enemy8_position_x * enemy8_width, enemy8_position_y * enemy8_height))
screen.blit(enemy9, (enemy9_position_x * enemy9_width, enemy9_position_y * enemy9_height))
screen.blit(enemy10, (enemy10_position_x * enemy10_width, enemy10_position_y * enemy10_height))
screen.blit(enemy11, (enemy11_position_x * enemy11_width, enemy11_position_y * enemy11_height))
screen.blit(enemy12, (enemy12_position_x * enemy12_width, enemy12_position_y * enemy12_height))
#Display the new frame.
pygame.display.flip()
#Sleep for about a tenth of a second.
time.sleep(speed)
welcome()
UPDATE:
Added Levels, the blocks move faster as you get to the higher levels
Code:
import pygame
import time
import random
speed = 0.025
level = 0
def welcome():
pygame.init()
global level
level += 1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('Welcome')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
game()
def lose():
pygame.init()
global level
level = 1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Lose')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
global speed
speed = 0.025
game()
def win():
pygame.init()
global level
level +=1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Win')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
global speed
speed -= 0.005
game()
def game():
pygame.display.set_caption('Falling Squares')
enemyspawns = []
enemyspawnsvar = -1
for x in xrange(40):
enemyspawnsvar = enemyspawnsvar + 1
enemyspawns.append(enemyspawnsvar)
#Set appearance values.
background_colour = (0, 0, 0)
dot_colour = (255, 0, 0)
#Put the dot at (0, 0) in the grid.
position_x = 20
position_y=39
#Make dots 5x5 so they're easy to see.
dot_width = dot_height = 5
#Initialize PyGame and create a 200x200 window.
pygame.init()
screen = pygame.display.set_mode((200, 200))
#Create the dot.
dot = pygame.Surface((dot_width, dot_height))
dot.fill(dot_colour)
#enemy
enemy_colour = (0,255,0)
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
enemy_width = enemy_height = 5
enemy = pygame.Surface((enemy_width, enemy_height))
enemy.fill(enemy_colour)
#enemy2
enemy2_colour = (0,255,0)
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
enemy2_width = enemy2_height = 5
enemy2 = pygame.Surface((enemy_width, enemy_height))
enemy2.fill(enemy_colour)
#enemy3
enemy3_colour = (0,255,0)
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
enemy3_width = enemy3_height = 5
enemy3 = pygame.Surface((enemy_width, enemy_height))
enemy3.fill(enemy_colour)
#enemy4
enemy4_colour = (0,255,0)
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
enemy4_width = enemy4_height = 5
enemy4 = pygame.Surface((enemy_width, enemy_height))
enemy4.fill(enemy_colour)
#enemy5
enemy5_colour = (0,255,0)
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
enemy5_width = enemy5_height = 5
enemy5 = pygame.Surface((enemy_width, enemy_height))
enemy5.fill(enemy_colour)
#enemy6
enemy6_colour = (0,255,0)
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
enemy6_width = enemy6_height = 5
enemy6 = pygame.Surface((enemy_width, enemy_height))
enemy6.fill(enemy_colour)
#enemy7
enemy7_colour = (0,255,0)
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
enemy7_width = enemy7_height = 5
enemy7 = pygame.Surface((enemy_width, enemy_height))
enemy7.fill(enemy_colour)
#enemy8
enemy8_colour = (0,255,0)
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
enemy8_width = enemy8_height = 5
enemy8 = pygame.Surface((enemy_width, enemy_height))
enemy8.fill(enemy_colour)
#enemy9
enemy9_colour = (0,255,0)
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
enemy9_width = enemy9_height = 5
enemy9 = pygame.Surface((enemy_width, enemy_height))
enemy9.fill(enemy_colour)
#enemy10
enemy10_colour = (0,255,0)
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
enemy10_width = enemy10_height = 5
enemy10 = pygame.Surface((enemy_width, enemy_height))
enemy10.fill(enemy_colour)
#enemy11
enemy11_colour = (0,255,0)
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
enemy11_width = enemy11_height = 5
enemy11 = pygame.Surface((enemy_width, enemy_height))
enemy11.fill(enemy_colour)
#enemy12
enemy12_colour = (0,255,0)
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
enemy12_width = enemy12_height = 5
enemy12 = pygame.Surface((enemy_width, enemy_height))
enemy12.fill(enemy_colour)
while True:
#enemy
if enemy_position_y < 40:
enemy_position_y += 1
elif enemy_position_y == 40:
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
if enemy_position_x == position_x and enemy_position_y == position_y:
lose()
#enemy2
if enemy2_position_y < 40:
enemy2_position_y += 1
elif enemy2_position_y == 40:
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
if enemy2_position_x == position_x and enemy2_position_y == position_y:
lose()
#enemy3
if enemy3_position_y < 40:
enemy3_position_y += 1
elif enemy3_position_y == 40:
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
if enemy3_position_x == position_x and enemy3_position_y == position_y:
lose()
#enemy4
if enemy4_position_y < 40:
enemy4_position_y += 1
elif enemy4_position_y == 40:
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
if enemy4_position_x == position_x and enemy4_position_y == position_y:
lose()
#enemy5
if enemy5_position_y < 40:
enemy5_position_y += 1
elif enemy5_position_y == 40:
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
if enemy5_position_x == position_x and enemy5_position_y == position_y:
lose()
#enemy6
if enemy6_position_y < 40:
enemy6_position_y += 1
elif enemy6_position_y == 40:
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
if enemy6_position_x == position_x and enemy6_position_y == position_y:
lose()
#enemy7
if enemy7_position_y < 40:
enemy7_position_y += 1
elif enemy7_position_y == 40:
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
if enemy7_position_x == position_x and enemy7_position_y == position_y:
lose()
#enemy8
if enemy8_position_y < 40:
enemy8_position_y += 1
elif enemy8_position_y == 40:
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
if enemy8_position_x == position_x and enemy8_position_y == position_y:
lose()
#enemy9
if enemy9_position_y < 40:
enemy9_position_y += 1
elif enemy9_position_y == 40:
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
if enemy9_position_x == position_x and enemy9_position_y == position_y:
lose()
#enemy10
if enemy10_position_y < 40:
enemy10_position_y += 1
elif enemy10_position_y == 40:
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
if enemy10_position_x == position_x and enemy10_position_y == position_y:
lose()
#enemy11
if enemy11_position_y < 40:
enemy11_position_y += 1
elif enemy11_position_y == 40:
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
if enemy11_position_x == position_x and enemy11_position_y == position_y:
lose()
#enemy12
if enemy12_position_y < 40:
enemy12_position_y += 1
elif enemy12_position_y == 40:
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
if enemy12_position_x == position_x and enemy12_position_y == position_y:
lose()
# Controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_LEFT:
position_x -= 1
print position_x
elif event.key == pygame.K_RIGHT:
position_x += 1
print position_x
elif event.key == pygame.K_UP:
position_y -= 1
print position_y
elif event.key == pygame.K_DOWN:
position_y += 1
print position_y
#How to win
if position_y < 0:
win()
#Force the dot to stay within the window
if position_x < 0:
position_x = 0
if position_x > 39:
position_x = 39
if position_y > 39:
postion_y = 39
#Fill in the background
screen.fill(background_colour)
#Draw the dots.
screen.blit(dot, (position_x * dot_width, position_y * dot_height))
screen.blit(enemy, (enemy_position_x * enemy_width, enemy_position_y * enemy_height))
screen.blit(enemy2, (enemy2_position_x * enemy2_width, enemy2_position_y * enemy2_height))
screen.blit(enemy3, (enemy3_position_x * enemy3_width, enemy3_position_y * enemy3_height))
screen.blit(enemy4, (enemy4_position_x * enemy4_width, enemy4_position_y * enemy4_height))
screen.blit(enemy5, (enemy5_position_x * enemy5_width, enemy5_position_y * enemy5_height))
screen.blit(enemy6, (enemy6_position_x * enemy6_width, enemy6_position_y * enemy6_height))
screen.blit(enemy7, (enemy7_position_x * enemy7_width, enemy7_position_y * enemy7_height))
screen.blit(enemy8, (enemy8_position_x * enemy8_width, enemy8_position_y * enemy8_height))
screen.blit(enemy9, (enemy9_position_x * enemy9_width, enemy9_position_y * enemy9_height))
screen.blit(enemy10, (enemy10_position_x * enemy10_width, enemy10_position_y * enemy10_height))
screen.blit(enemy11, (enemy11_position_x * enemy11_width, enemy11_position_y * enemy11_height))
screen.blit(enemy12, (enemy12_position_x * enemy12_width, enemy12_position_y * enemy12_height))
#Display the new frame.
pygame.display.flip()
#Sleep for about a tenth of a second.
time.sleep(speed)
welcome()
UPDATE:
I made the game a little easier, always losing on the first level gets boring
Code:
import pygame
import time
import random
speed = 0.05
level = 0
def welcome():
pygame.init()
global level
level += 1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('Welcome')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
game()
def lose():
pygame.init()
global level
level = 1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Lose')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
global speed
speed = 0.05
game()
def win():
pygame.init()
global level
level +=1
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('You Win')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 25)
# Render the text
text = font.render('Level '+ str(level), True, (255,255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_SPACE:
global speed
speed -= 0.01
game()
def game():
pygame.display.set_caption('Falling Squares')
enemyspawns = []
enemyspawnsvar = -1
for x in xrange(40):
enemyspawnsvar = enemyspawnsvar + 1
enemyspawns.append(enemyspawnsvar)
#Set appearance values.
background_colour = (0, 0, 0)
dot_colour = (255, 0, 0)
#Put the dot at (0, 0) in the grid.
position_x = 20
position_y=39
#Make dots 5x5 so they're easy to see.
dot_width = dot_height = 5
#Initialize PyGame and create a 200x200 window.
pygame.init()
screen = pygame.display.set_mode((200, 200))
#Create the dot.
dot = pygame.Surface((dot_width, dot_height))
dot.fill(dot_colour)
#enemy
enemy_colour = (0,255,0)
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
enemy_width = enemy_height = 5
enemy = pygame.Surface((enemy_width, enemy_height))
enemy.fill(enemy_colour)
#enemy2
enemy2_colour = (0,255,0)
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
enemy2_width = enemy2_height = 5
enemy2 = pygame.Surface((enemy_width, enemy_height))
enemy2.fill(enemy_colour)
#enemy3
enemy3_colour = (0,255,0)
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
enemy3_width = enemy3_height = 5
enemy3 = pygame.Surface((enemy_width, enemy_height))
enemy3.fill(enemy_colour)
#enemy4
enemy4_colour = (0,255,0)
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
enemy4_width = enemy4_height = 5
enemy4 = pygame.Surface((enemy_width, enemy_height))
enemy4.fill(enemy_colour)
#enemy5
enemy5_colour = (0,255,0)
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
enemy5_width = enemy5_height = 5
enemy5 = pygame.Surface((enemy_width, enemy_height))
enemy5.fill(enemy_colour)
#enemy6
enemy6_colour = (0,255,0)
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
enemy6_width = enemy6_height = 5
enemy6 = pygame.Surface((enemy_width, enemy_height))
enemy6.fill(enemy_colour)
#enemy7
enemy7_colour = (0,255,0)
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
enemy7_width = enemy7_height = 5
enemy7 = pygame.Surface((enemy_width, enemy_height))
enemy7.fill(enemy_colour)
#enemy8
enemy8_colour = (0,255,0)
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
enemy8_width = enemy8_height = 5
enemy8 = pygame.Surface((enemy_width, enemy_height))
enemy8.fill(enemy_colour)
#enemy9
enemy9_colour = (0,255,0)
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
enemy9_width = enemy9_height = 5
enemy9 = pygame.Surface((enemy_width, enemy_height))
enemy9.fill(enemy_colour)
#enemy10
enemy10_colour = (0,255,0)
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
enemy10_width = enemy10_height = 5
enemy10 = pygame.Surface((enemy_width, enemy_height))
enemy10.fill(enemy_colour)
#enemy11
enemy11_colour = (0,255,0)
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
enemy11_width = enemy11_height = 5
enemy11 = pygame.Surface((enemy_width, enemy_height))
enemy11.fill(enemy_colour)
#enemy12
enemy12_colour = (0,255,0)
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
enemy12_width = enemy12_height = 5
enemy12 = pygame.Surface((enemy_width, enemy_height))
enemy12.fill(enemy_colour)
while True:
#enemy
if enemy_position_y < 40:
enemy_position_y += 1
elif enemy_position_y == 40:
enemy_position_x = random.choice(enemyspawns)
enemy_position_y = 0
if enemy_position_x == position_x and enemy_position_y == position_y:
lose()
#enemy2
if enemy2_position_y < 40:
enemy2_position_y += 1
elif enemy2_position_y == 40:
enemy2_position_x = random.choice(enemyspawns)
enemy2_position_y = 0
if enemy2_position_x == position_x and enemy2_position_y == position_y:
lose()
#enemy3
if enemy3_position_y < 40:
enemy3_position_y += 1
elif enemy3_position_y == 40:
enemy3_position_x = random.choice(enemyspawns)
enemy3_position_y = 0
if enemy3_position_x == position_x and enemy3_position_y == position_y:
lose()
#enemy4
if enemy4_position_y < 40:
enemy4_position_y += 1
elif enemy4_position_y == 40:
enemy4_position_x = random.choice(enemyspawns)
enemy4_position_y = 0
if enemy4_position_x == position_x and enemy4_position_y == position_y:
lose()
#enemy5
if enemy5_position_y < 40:
enemy5_position_y += 1
elif enemy5_position_y == 40:
enemy5_position_x = random.choice(enemyspawns)
enemy5_position_y = 0
if enemy5_position_x == position_x and enemy5_position_y == position_y:
lose()
#enemy6
if enemy6_position_y < 40:
enemy6_position_y += 1
elif enemy6_position_y == 40:
enemy6_position_x = random.choice(enemyspawns)
enemy6_position_y = 0
if enemy6_position_x == position_x and enemy6_position_y == position_y:
lose()
#enemy7
if enemy7_position_y < 40:
enemy7_position_y += 1
elif enemy7_position_y == 40:
enemy7_position_x = random.choice(enemyspawns)
enemy7_position_y = 0
if enemy7_position_x == position_x and enemy7_position_y == position_y:
lose()
#enemy8
if enemy8_position_y < 40:
enemy8_position_y += 1
elif enemy8_position_y == 40:
enemy8_position_x = random.choice(enemyspawns)
enemy8_position_y = 0
if enemy8_position_x == position_x and enemy8_position_y == position_y:
lose()
#enemy9
if enemy9_position_y < 40:
enemy9_position_y += 1
elif enemy9_position_y == 40:
enemy9_position_x = random.choice(enemyspawns)
enemy9_position_y = 0
if enemy9_position_x == position_x and enemy9_position_y == position_y:
lose()
#enemy10
if enemy10_position_y < 40:
enemy10_position_y += 1
elif enemy10_position_y == 40:
enemy10_position_x = random.choice(enemyspawns)
enemy10_position_y = 0
if enemy10_position_x == position_x and enemy10_position_y == position_y:
lose()
#enemy11
if enemy11_position_y < 40:
enemy11_position_y += 1
elif enemy11_position_y == 40:
enemy11_position_x = random.choice(enemyspawns)
enemy11_position_y = 0
if enemy11_position_x == position_x and enemy11_position_y == position_y:
lose()
#enemy12
if enemy12_position_y < 40:
enemy12_position_y += 1
elif enemy12_position_y == 40:
enemy12_position_x = random.choice(enemyspawns)
enemy12_position_y = 0
if enemy12_position_x == position_x and enemy12_position_y == position_y:
lose()
# Controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exit()
elif event.key == pygame.K_LEFT:
position_x -= 1
print position_x
elif event.key == pygame.K_RIGHT:
position_x += 1
print position_x
elif event.key == pygame.K_UP:
position_y -= 1
print position_y
elif event.key == pygame.K_DOWN:
position_y += 1
print position_y
#How to win
if position_y < 0:
win()
#Force the dot to stay within the window
if position_x < 0:
position_x = 0
if position_x > 39:
position_x = 39
if position_y > 39:
postion_y = 39
#Fill in the background
screen.fill(background_colour)
#Draw the dots.
screen.blit(dot, (position_x * dot_width, position_y * dot_height))
screen.blit(enemy, (enemy_position_x * enemy_width, enemy_position_y * enemy_height))
screen.blit(enemy2, (enemy2_position_x * enemy2_width, enemy2_position_y * enemy2_height))
screen.blit(enemy3, (enemy3_position_x * enemy3_width, enemy3_position_y * enemy3_height))
screen.blit(enemy4, (enemy4_position_x * enemy4_width, enemy4_position_y * enemy4_height))
screen.blit(enemy5, (enemy5_position_x * enemy5_width, enemy5_position_y * enemy5_height))
screen.blit(enemy6, (enemy6_position_x * enemy6_width, enemy6_position_y * enemy6_height))
screen.blit(enemy7, (enemy7_position_x * enemy7_width, enemy7_position_y * enemy7_height))
screen.blit(enemy8, (enemy8_position_x * enemy8_width, enemy8_position_y * enemy8_height))
screen.blit(enemy9, (enemy9_position_x * enemy9_width, enemy9_position_y * enemy9_height))
screen.blit(enemy10, (enemy10_position_x * enemy10_width, enemy10_position_y * enemy10_height))
screen.blit(enemy11, (enemy11_position_x * enemy11_width, enemy11_position_y * enemy11_height))
screen.blit(enemy12, (enemy12_position_x * enemy12_width, enemy12_position_y * enemy12_height))
#Display the new frame.
pygame.display.flip()
#Sleep for about a tenth of a second.
time.sleep(speed)
welcome()
I still only get to level 3, but more frequently
EDIT:
Got to level 4
UPDATE:
Added sounds
I uploaded it this time so you can download the sounds along with the code
http://www.mediafire.com/?mtk2yzegbbw
EDIT:
Please comment on the sounds, I'm think about taking out the explosion and applause, tell me what you think