Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Last active August 29, 2015 14:20
Show Gist options
  • Save alberthdev/98d050280434084be9e1 to your computer and use it in GitHub Desktop.
Save alberthdev/98d050280434084be9e1 to your computer and use it in GitHub Desktop.
Learn Python - Sample Game Code
# Learn Python - Sample Game Code
# --------------------------------
# Analyze the game code below. Once you are ready, you can begin answering questions.
# You may (and are encouraged to) refer to the code below when answering questions.
#
# Questions:
# 1) Label the code. What is what? What does what? Be as specific as possible!
# 2) Assume that we run the following line: raise GameException("BogusError", "This is bogus.")
# Assuming that we do not catch it, what will the error look like?
# 3) Explain what clearScreenBuffer() does in the GameScreen class. What is happening? Be specific.
# 4) Explain what renderScreen() does in the GameScreen class. What is happening? Be specific.
# A) (BONUS!) What class is the Sprite object inheriting from? Be specific and explain!
# B) (BONUS!) What does the "os" line in showScreen() in the GameScreen class do?
# C) (BONUS!) Explain, in detail, what the code does overall.
#
# FINAL QUESTION:
# Assume that this entire file is called "gamecore.py". Write a program that uses this module to
# simulate a game. You should use AWSD for controls (i.e. "a" for left, "w" for up, "s" for down",
# and "d" for right). A single space should cause the player to attack the monster, but only if
# the player is next to the monster. A single letter "q" should exit the game. You should use
# raw_input() to get input for controlling.
#
# Make this into a real game - refresh the screen every time, and print out any stats you think
# are necessary. Make it playable!
import os
class GameException (Exception):
def __init__ (self, error_type, error):
self.error_type = error_type
self.error = error
def __str__ (self):
return ( "GameException: [" + str(self.error_type) + "] " + str(self.error) )
class GameScreen(object):
def __init__(self, screen_res):
self.resolution = screen_res
self.screen_buffer = ""
self.screen_elements = []
def clearScreen(self):
self.screen_elements = []
def clearScreenBuffer(self):
self.screen_buffer = ((" " * self.resolution[0]) + "\n") * self.resolution[1]
def addSprite(self, sprite_obj):
if sprite_obj is not None:
self.screen_elements.append(sprite_obj)
else:
raise GameException("GameScreen", "Sprite object can not be None!")
def setScreenResolution(self, res_x, res_y):
if (res_x <= 0) or (res_y <= 0):
raise GameException("GameScreen", "Coordinates can not be zero or negative!")
self.resolution[0] = res_x
self.resolution[1] = res_y
def renderScreen(self):
self.clearScreenBuffer()
for sprite in self.screen_elements:
sprite_x, sprite_y = sprite.getPosition()
if sprite_x > self.resolution[0] or sprite_y > self.resolution[1]:
raise GameException("GameScreen", "Sprite coordinates are out of bounds!")
screen_buffer_index = (sprite_x - 1) + ((sprite_y - 1) * self.resolution[1]) + ((sprite_y - 1) * 1)
tmp_screen_buffer = self.screen_buffer[:screen_buffer_index] + \
sprite.getSprite() + self.screen_buffer[screen_buffer_index+1:]
if len(tmp_screen_buffer) != len(self.screen_buffer):
raise GameException("GameScreen", "BUG - screen buffer update length different! (temp %i vs old %i)" % (len(tmp_screen_buffer), len(self.screen_buffer)))
self.screen_buffer = tmp_screen_buffer
def showScreen(self):
os.system('cls' if os.name == 'nt' else 'clear')
print self.screen_buffer
class Sprite(object):
def __init__(self, pos_array, sprite_char):
self.position = pos_array
self.character = sprite_char
def moveX(self, diff_x):
self.position[0] += diff_x
def moveY(self, diff_y):
self.position[1] += diff_y
def getPosition(self):
return self.position
def setPosition(self, x, y):
self.position = [x, y]
def getSprite(self):
return self.character
class Monster(Sprite):
def __init__(self, pos_array, monster_name, hp):
Sprite.__init__(self, pos_array, "%")
self.name = monster_name
self.hp = hp
def killMonster(self):
self.character = "x"
def getMonsterName(self):
return self.name
def weakenMonster(self, pts):
self.hp = self.hp - pts
class Player(Sprite):
def __init__(self, pos_array, player_name, hp):
super(Monster, self).__init__(pos_array, "^")
self.name = player_name
self.hp = hp
def killPlayer(self):
self.character = "x"
def getPlayerName(self):
return self.name
def hurtPlayer(self, pts):
self.hp -= pts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment