Skip to content

Instantly share code, notes, and snippets.

@wibbia
Created September 22, 2015 14:20
Show Gist options
  • Save wibbia/2db3140c0299a0872dbe to your computer and use it in GitHub Desktop.
Save wibbia/2db3140c0299a0872dbe to your computer and use it in GitHub Desktop.
O
O____O______OOOOO
OO_OOO_OOO_OO___O
OO_O____O__OOOO_O
O__OOO__OO______O
OO_OOO_OOO_OOOO_O
OO_O___OOOOOOOO_O
O____O__OO__OOO_O
OOOOOO_OO__OO___O
OOOO______OOOOOXO
O
O
O_TOTOTOTOTOTOTOTOTOTOO
O_____________________>
<_____________________O
O_____________________>
<_____________________O
O__________X__________>
<__________X__________O
O_____________________>
<_____________________O
O_____________________>
<_____________________O
OtOtOtOtOtOtOtOtOtOtOtO
O
OOOOOOTOTOTOTOTOTOTTOOOOOOOOOOOOOOOOOOOOOOOOOOO
O_O_________________O______________O__________O
<_O_>OOOOOOOOOOOOOO_O_OOOOOOOOOOOO_OOOOOOOOOO_O
<_O_>_______________O____________O____________O
<_O_>_OtOtOtOtOtOtOOOtOtOtOtOtOt_tOtOtOtOtOtO_O
<_O_>_TOTOTOTOTOTOTOTOTOTOTOTOTO_OOOOOOOOOOOO_O
<_O_>__________________________>_O__________O_O
<_O_><_________________________O_O_OOOOOOOO_O_O
<_O_>O_________________________>_O_O______O_O_O
O___O<_________________________O_O_O_OOOOXO_O_O
OOOOOO_________________________>_O_O_OOOOOO_O_O
OOOOO<_________________________O_O_O________O_O
OOOOOO_________________________>_O_OOOOOOOOOO_O
OOOOO<___________________________O____________O
OOOOOOOtOtOtOtOtOtOtOtOtOtOtOtOOOOOOOOOOOOOOOOO
O
OOOOOOOOOOOOOOOOOOOOOOOOO
O_T_T_T_T_T_T_T_T_T_T_T_O
O______________________>O
O<______________________O
O___O________O_____O___>O
O<______O________O______O
O___________O__________>O
O<____O_________________O
O_______________O____O_>O
O<______________________O
O___O____O_____________>O
O<______________________O
O___________O__________>O
O<____O___O____O__O_____O
O____________________O_>O
O<___________O__________O
O__O___________________>O
O<_________________O____O
O_____O____O___O_______>O
O<______________________O
O_______O____O_____O___>O
O<______________________O
O______________________>O
O__t_t_t_t_t_t_t_t_t_tXXO
OOOOOOOOOOOOOOOOOOOOOOOOO
O
O__________________________________________O
O__________________________________________O
O__________________________________________O
O__________________________________________O
O___O___O_OOO_O_O__O___________O_O_O___O___O
O____O_O__O_O_O_O___O_________O__O_OO__O___O
O_____O___O_O_O_O____O___O___O___O_O_O_O___O
O_____O___O_O_O_O_____O_O_O_O____O_O__OO___O
O_____O___OOO_OOO______O___O_____O_O___O___O
O__________________________________________O
O__________________________________________O
O__________________________________________O
O__________________________________________O
O
OOOOOOOOOO
O O
O O
O XXXXXX O
OOOOOOOOOO
O
O_OOOOOOOOOOOOOO
O_OOO_OO_O___OOO
O___O____O_OOOOO
O_O_O_OOOO_OOOOO
O_OOO_________OO
O_____O_OOOOO__O
OO_OOOO_OO_O__OO
OO___OO____O__XO
OOOO_OOOOO__OOOO
O
O
O___OOOOOOOOOOOOOOOOOOOOOOOOOOOO
O_____OO___O__________O________O
O_OOO____O_O_O_OOOOOOOO_OOOOOO_O
O__OOO_OOOOO_O_OO_O_____O_O____O
OO___O___O___O_OO_O_OOOOO_O_OOOO
O__O_O_OOO_O_O____O_O___O_O____O
OO_OOOOOOO_O_OOOOOO___O_O_O_OOOO
OO_________O_O______OOO_O_O____O
OOOOOOOOOOOO_O_O_OOOO___O_O_OO_O
O____________O_O____O_O_O_O__O_O
OOOO_OOOOO_OOO_OOOO_O_O_O_O_0__O
O____OOOOO___O_O____O_O_O_OOO_OO
O_OOOOOOOOOOOO_O_OOOO_O_O_OOO_OO
O______________OOOOOO_O___OOOXOO
O
O
O__OOO___OOO_OO___OOOOOOOOOOOOOOOOOXO
O__OOO_O___O_OO_OO_OOOO__RO_RRO___O_O
ORROOO_ORO_O____OO_OOOO_ORO_ORO_O_O_O
ORR____OROOOOO_OOO____O_ORO_ORO_OOO_O
ORROOOOORO_RR__OOO__O_O_OR__ORO_O_O_O
O__OOOOOR__OOO__O__OO___OROOORO___O_O
O__O__OOOOOOOOO___OO_OROOROOORO_OOO_O
O____O___OOOOO_OOOOOOOOOOROOO___OR__O
OOOO___O____________RR__OR__OOOOOROOO
O____OOOROOOOOO_OOOOOOOOOOO_O___OROOO
OROOO___RO___OO_OOOOORRR_OO_ORO_OROOO
OROOOROOO__O__O______ROO_OO_ORO_OROOO
OROOOR__O_OOOOOOOOOO_OOO_OO_ORO_OROOO
OROOOOOOO_RRRR_OO__OOOO__OO_ORO_OROOO
O_________OOOO____OOOO__OOO_RRO___OOO
O
O
O___TOOOOOOXO
O_________ORO
O_________ORO
OOOOOOOO__ORO
O_________ORO
O_________ORO
O__OOOtOOOORO
O__OOOOOOOO_O
O___________O
O
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOTOTOTOO
________X
OOOtOtOtO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
O
O__OTORRRR_O___OO
O______OtO_O_O_OO
OO_OOOOOO____OROO
O__OOR__OOOtOOROO
O_TORRO_O___OOROO
O____OO_O_OOORROO
OOOOOOO_O___OOOOO
O_______O_O_OOX_O
O_tOtOtOO_O_TTT_O
ORRRRRRRR_O_____O
O
O
O_TOTOTOTOTOTOTOTO_O
O__________________O
O__________________>
O__________________O
OOOtOtOtOtOtOtOtOt_O
O__________________O
O_TtTtTtTtTtTtTtTtTO
O__________________X
O
O
O_TOTOTOTOTOTOTOTOTOTOTOTOTOTOOOO
O_______________RRRRRRRRRRRRRR_OO
O_OtOtOtOtOtOtOtOtOtOtOtOtOtOt_OO
OOOOTOTOTOTOTOTOTOTOTOTOTOTOTO_OO
OOOO___________________________OO
OOOO_tOtOtOtOtOtOtOtOtOtOtOtOtOOO
OOOO_OTOTOTOTOTOTOTOTOTOTOTOTOTOO
OOOO___________________________XO
O
import pygame, sys, time
black = 0,0,0
red = 255,0,0
wall_color = 255,0,0
background_color = 255,255,255
green = 0,255,0
blue = 0,0,255
white = 255,255,255
dark_blue = 25,25,112
dark_green = 85,107,47
turret_colour = 11,123,156
enemy_color = 160,160,160
grey = 160,160,160
bullet_delay = 40
if wall_color == background_color:
print("You complete Fool!")
print("Good luck")
time.sleep(2)
pygame.init()
size = width, height = 1000,600
screen = pygame.display.set_mode(size)
wall_list = pygame.sprite.Group()
solid_wall_list = pygame.sprite.Group()
river_wall_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
level_number = 1
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, x_direction, y_direction):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([9, 9])
self.image.fill(enemy_color)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
self.x_direction = x_direction
self.y_direction = y_direction
def update(self):
self.rect.x += self.x_direction
self.rect.y += self.y_direction
for wall in pygame.sprite.spritecollide(self, wall_list, False):
if not isinstance(wall, WallTurret):
bullet_list.remove(self)
break
class WallTurret(Wall):
def __init__(self, x, y, width, height, color, x_direction, y_direction):
Wall.__init__(self, x, y, width, height, color)
self.x_direction = x_direction
self.y_direction = y_direction
self.since_fired = 0
def update(self):
self.since_fired += 1
if self.since_fired >= bullet_delay:
self.since_fired = 0
new_bullet = Bullet(self.rect.centerx, self.rect.centery, self.x_direction, self.y_direction)
bullet_list.add(new_bullet)
keys_down = []
def is_key_down(key):
return key in keys_down
def load_map():
global wall_list
global solid_wall_list
global river_wall_list
global bullet_list
all_sprites_list.remove(wall_list)
all_sprites_list.remove(solid_wall_list)
all_sprites_list.remove(river_wall_list)
wall_list = pygame.sprite.Group()
solid_wall_list = pygame.sprite.Group()
river_wall_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
raw_map = open('maps/map' + str(level_number) + '.txt', 'r').read()
rows = raw_map.split('\n')
block_height = height / float(len(rows))
y = 0
for row in rows:
block_width = width / float(len(row))
x = 0
for cell in row:
if cell == 'O':
new_wall = Wall(x, y, block_width, block_height, wall_color)
wall_list.add(new_wall)
if cell == 'X':
new_wall = Wall(x,y, block_width, block_height, dark_green)
solid_wall_list.add(new_wall)
if cell == 'R':
new_wall = Wall(x, y, block_width, block_height, dark_blue)
river_wall_list.add(new_wall)
if cell == 'T':
new_wall = WallTurret(x, y, block_width, block_height, turret_colour, 0, 1)
wall_list.add(new_wall)
if cell == 't':
new_wall = WallTurret(x, y, block_width, block_height, turret_colour, 0, -1)
wall_list.add(new_wall)
if cell == '>':
new_wall = WallTurret(x, y, block_width, block_height, turret_colour, -1, 0)
wall_list.add(new_wall)
if cell == '<':
new_wall = WallTurret(x, y, block_width, block_height, turret_colour, 1, 0)
wall_list.add(new_wall)
x += block_width
y += block_height
all_sprites_list.add(wall_list)
all_sprites_list.add(solid_wall_list)
all_sprites_list.add(river_wall_list)
class Character(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def go_to_level_beginning(self):
for y in xrange(0, height, 16):
found = False
for x in xrange(0, width, 16):
self.rect.x = x
self.rect.y = y
if len(pygame.sprite.spritecollide(self, wall_list, False)) == 0:
found = True
break
if found:
break
def update(self):
if len(pygame.sprite.spritecollide(self, wall_list, False)) > 0:
self.go_to_level_beginning()
if len(pygame.sprite.spritecollide(self, bullet_list, False)) > 0:
self.go_to_level_beginning()
if len(pygame.sprite.spritecollide(self, river_wall_list, False)) > 0:
speed = 1
if len(pygame.sprite.spritecollide(self, river_wall_list, False)) == 0:
speed = 3
if is_key_down(pygame.K_UP):
self.rect.y -= speed
if len(pygame.sprite.spritecollide(self, wall_list, False)) > 0:
self.rect.y += speed
if is_key_down(pygame.K_DOWN):
self.rect.y += speed
if len(pygame.sprite.spritecollide(self, wall_list, False)) > 0:
self.rect.y -= speed
if is_key_down(pygame.K_LEFT):
self.rect.x -= speed
if len(pygame.sprite.spritecollide(self, wall_list, False)) > 0:
self.rect.x += speed
if is_key_down(pygame.K_RIGHT):
self.rect.x += speed
if len(pygame.sprite.spritecollide(self, wall_list, False)) > 0:
self.rect.x -= speed
if len(pygame.sprite.spritecollide(self, solid_wall_list, False)) > 0:
global level_number
level_number += 1
load_map()
self.go_to_level_beginning()
character = Character(80, 75, 7, 7, black)
load_map()
ideal_ticks = 60
clock = pygame.time.Clock()
ticks = 0
delta = 1
while True:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
keys_down.append(event.key)
elif event.type == pygame.KEYUP:
keys_down.remove(event.key)
if is_key_down(pygame.K_ESCAPE):
sys.exit()
all_sprites_list.update()
character.update()
bullet_list.update()
all_sprites_list.draw(screen)
screen.blit(character.image, character.rect)
for bullet in bullet_list.sprites():
screen.blit(bullet.image, bullet.rect)
pygame.display.update()
ticks = clock.tick(ideal_ticks)
delta = ticks / float(ideal_ticks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment