Skip to content

Instantly share code, notes, and snippets.

@iminurnamez
Created April 21, 2018 13:55
Show Gist options
  • Save iminurnamez/a3ee1e7cb15c6e1b5d110be1b0d62357 to your computer and use it in GitHub Desktop.
Save iminurnamez/a3ee1e7cb15c6e1b5d110be1b0d62357 to your computer and use it in GitHub Desktop.
# Game Project Assignment comment in progress by Julia Luu
import math
import simplegui
# constants
#-------------------------#
CANVAS_WIDTH = 1000
CANVAS_HEIGHT = 550
BUTTON_WIDTH = 200
BUTTON_HEIGHT = 100
GROUND_POS = 500
# images
IMG1 = simplegui.load_image("http://pixelartmaker.com/art/72a20f98441aba0.png")
IMG2 = simplegui.load_image("http://pixelartmaker.com/art/72a20f98441aba0.png")
#IMG2 = simplegui.load_image("https://pre00.deviantart.net/d136/th/pre/i/2012/340/2/5/png_rat_4_by_moonglowlilly-d5n8235.png")
BACKGROUND = simplegui.load_image("https://img00.deviantart.net/8dd7/i/2012/217/1/b/custom_background_drop_by_mikamori-d59vb3x.png")
BACKGROUND2 = simplegui.load_image("https://archive-media-0.nyafuu.org/wg/image/1473/88/1473884891676.png")
BG_SPRITE = simplegui.load_image("https://i.imgur.com/gZI7pRE.png")
SHIELD = simplegui.load_image("http://www.stickpng.com/assets/images/580b585b2edbce24c47b26d8.png")
SWORD = simplegui.load_image("https://orig00.deviantart.net/5f2b/f/2014/145/3/d/medieval_heavy_sword_pixel_art_by_husseinhorack-d7jnld5.png")
HELP_BUTTON = simplegui.load_image("http://pixelartmaker.com/art/7015cbd649b3b18.png")
START_BUTTON = simplegui.load_image("http://makepixelart.com/peoplepods/files/images/727825.original.png")
JUMP_SPEED = 12
ACCEL = 1
#-----------------#
# Booleans
#----------#
shield1 = False
shield2 = False
start_up_menu = True
help_menu = False
game_running = False
game_ended = False
#-----------------#
# infinite loop to wait for image to load
#------------------------------------------#
while IMG1.get_width() == 0 and IMG2.get_width():
pass
# Dictionary of image dimensions (width & height)
#---------------------------------#
CHARACTER_SIZES = {IMG1: [590,470], IMG2: [590, 470]} #IMG2: [999, 799],
CONSTANT_SIZES = {BACKGROUND: [900, 369], BACKGROUND2: [1920, 1080], HELP_BUTTON: [350, 150],
START_BUTTON: [256, 128], SHIELD:[1600, 1600], SWORD: [179, 332]}
# animation sizes + centres
ANIMATIONS_SIZES = {BG_SPRITE: [10240, 10635]}
#----------------------------------#
# Initial state of game
def new_game():
global player1, player2, start, start_butt, help_butt
player1 = Character([650, 200], [0,0], 100, IMG1)
player2 = Character([150,100], [0,0], 100, IMG2)
start = Start_Menu()
start_butt = Button(START_BUTTON, (400,400),
CONSTANT_SIZES[START_BUTTON][0], CONSTANT_SIZES[START_BUTTON][1])
help_butt = Button(HELP_BUTTON, (140,340),
CONSTANT_SIZES[HELP_BUTTON][0], CONSTANT_SIZES[HELP_BUTTON][1])
class Rect(object):
def __init__(self, center, size):
self.width, self.height = size
self.set_pos_by_center(center)
def rect_collide(self, other):
if all((self.x < other.x + other.width,
self.x + self.width > other.x,
self.y < other.y + other.height,
self.y + self.height > other.y)):
return True
def point_collide(self, pos):
x, y = pos
if self.x < x < self.x + self.width and self.y < y < self.y + self.height:
return True
def get_corners(self):
#topleft, topright, bottomright, bottomleft
return [(self.x, self.y), (self.x + self.width, self.y),
(self.x + self.width, self.y + self.height),
(self.x, self.y + self.height)]
def set_pos_by_center(self, center):
self.x = center[0] - self.width // 2
self.y = center[1] - self.height // 2
def draw(self, canvas, color="white"):
canvas.draw_polygon(self.get_corners(), 10, color)
class Character:
def __init__(self, position, velocity, radius, image):
self.pos = position
self.vel = velocity #perma variable
self.rad = radius
self.image = image
self.health = 20
size = self.image.get_width(), self.image.get_height()
self.rect = Rect(self.pos, size)
def draw(self, canvas, color):
center_x = CHARACTER_SIZES[self.image][0]/2
center_y = CHARACTER_SIZES[self.image][1]/2
canvas.draw_image(self.image,
(center_x, center_y),
CHARACTER_SIZES[self.image],
self.pos,
(self.rad*5, self.rad*5))
canvas.draw_circle(self.pos, self.rad, 1, "white",)
self.rect.draw(canvas, color)
def jump(self):
if self.pos[1] >= GROUND_POS - self.rect.height // 2:
self.vel[1] = -JUMP_SPEED
self.pos[1] -= JUMP_SPEED
def update(self):
for i in range(2):
self.pos[i] += self.vel[i]
#self.pos[1] += self.vel[1]
if self.pos[1] <= GROUND_POS - self.rect.height // 2:
self.vel[1] += ACCEL
else:
self.pos[1] = GROUND_POS - self.rect.height // 2
self.vel[1] = 0
self.pos[0] %= CANVAS_WIDTH
self.rect.set_pos_by_center(self.pos)
# checks to see if images have collided
def has_collided(self, other_object):
dist = distance(self.pos, other_object.pos)
# Less than the sum of the two radii means collision has occured
return dist <= self.rad + other_object.rad
def draw_attack(self, canvas):
canvas.draw_image()
def draw_shield(self, canvas):
cntr_x = CONSTANT_SIZES[SHIELD][0]/2
cntr_y = CONSTANT_SIZES[SHIELD][1]/2
canvas.draw_image(SHIELD, (cntr_x,cntr_y), CONSTANT_SIZES[SHIELD],
self.pos, (self.rad*3, self.rad*3))
def health_status_p1(self, canvas):
canvas.draw_text(str(self.health), (0,85), 100, 'White')
def health_status_p2(self, canvas):
canvas.draw_text(str(self.health), (900,85), 100, 'White')
class Button:
def __init__(self, image, location, width, height):
self.image = image
self.loc = location
self.width = width
self.height = height
def draw(self, canvas):
canvas.draw_image(self.image,
(CONSTANT_SIZES[self.image][0]/2, CONSTANT_SIZES[self.image][1]/2),
CONSTANT_SIZES[self.image],
self.loc,
(BUTTON_WIDTH, BUTTON_HEIGHT))
# selection method for buttons
def is_selected(self, click_pos):
in_x = abs(click_pos[0] - self.loc[0]) < self.width/3
in_y = abs(click_pos[1] - self.loc[1]) < self.height/3
return in_x and in_y
class Start_Menu():
def draw_title(self, canvas):
canvas.draw_text('Temporary Title for Game',
(24,120), 75, 'Yellow')
def draw_background(self, canvas):
background2_cntrx = CONSTANT_SIZES[BACKGROUND2][0]/2
background2_cntry = CONSTANT_SIZES[BACKGROUND2][1]/2
canvas2_cntrx = CANVAS_WIDTH/2
canvas2_cntry = CANVAS_HEIGHT/2
canvas.draw_image(BACKGROUND2, (background2_cntrx, background2_cntry),
(CONSTANT_SIZES[BACKGROUND2][0], CONSTANT_SIZES[BACKGROUND2][1]),
(canvas2_cntrx, canvas2_cntry), (CANVAS_WIDTH, CANVAS_HEIGHT))
# function for distance - determines collisions
def distance(pos1, pos2):
x_diff = pos2[0] - pos1[0]
y_diff = pos2[1] - pos1[1]
# pythagorean rule to calculate the distance
dist = math.sqrt(x_diff ** 2 + y_diff ** 2)
return dist
# Movement
def keyup_handler(key):
global shield1, shield2
# player 1 movement released
if key == simplegui.KEY_MAP['left']:
player1.vel[0] = 0
elif key == simplegui.KEY_MAP['right']:
player1.vel[0] = 0
elif key == simplegui.KEY_MAP['up']:
player1.jump()
elif key == simplegui.KEY_MAP['l']:
if shield1 == True:
shield1 = False
# player 2 movement released
elif key == simplegui.KEY_MAP['a']:
player2.vel[0] = 0
elif key == simplegui.KEY_MAP['d']:
player2.vel[0] = 0
elif key == simplegui.KEY_MAP['w']:
player2.jump()
elif key == simplegui.KEY_MAP['b']:
if shield2 == True:
shield2 = False
# down means button is being pressed
def keydwn_handler(key):
global shield1, shield2
# player 1 movement dwn
if not shield1:
if key == simplegui.KEY_MAP['left']:
# negative value moves image to left
player1.vel[0] = -5
elif key == simplegui.KEY_MAP['right']:
player1.vel[0] = 5
elif key == simplegui.KEY_MAP['l']:
if not shield1:
shield1 = True
# player 2 movement dwn
if not shield2:
if key == simplegui.KEY_MAP['a']:
player2.vel[0] = -10
elif key == simplegui.KEY_MAP['d']:
player2.vel[0] = 10
elif key == simplegui.KEY_MAP['b']:
if not shield2:
shield2 = True
def mouse_click(click_pos):
global game_running
if game_running == False:
if start_butt.is_selected(click_pos):
game_running = not game_running
if help_butt.is_selected(click_pos):
pass
# Draw handler
def draw(canvas):
if game_running == True:
background_cntrx = CONSTANT_SIZES[BACKGROUND][0]/2
background_cntry = CONSTANT_SIZES[BACKGROUND][1]/2
canvas_cntrx = CANVAS_WIDTH/2
canvas_cntry = CANVAS_HEIGHT/2
canvas.draw_image(BACKGROUND, (background_cntrx, background_cntry),
(CONSTANT_SIZES[BACKGROUND][0], CONSTANT_SIZES[BACKGROUND][1]),
(canvas_cntrx, canvas_cntry), (CANVAS_WIDTH, CANVAS_HEIGHT))
# draws and updates characters
collided = player1.rect.rect_collide(player2.rect)
color = "green"
if collided:
color = "red"
player1.draw(canvas, color)
player2.draw(canvas, color)
player1.update()
player2.update()
# updates health of character
player1.health_status_p1(canvas)
player2.health_status_p2(canvas)
# draws shields
if shield1 == True:
player1.draw_shield(canvas)
if shield2 == True:
player2.draw_shield(canvas)
if player1.has_collided(player2):
print "ouch"
canvas.draw_line([0,500],[1000,500],2,'white')
else:
#start menu
start.draw_background(canvas)
start.draw_title(canvas)
start_butt.draw(canvas)
help_butt.draw(canvas)
new_game() # have to call function to work
# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("Sample", CANVAS_WIDTH, CANVAS_HEIGHT)
frame.set_draw_handler(draw)
frame.set_keyup_handler(keyup_handler)
frame.set_keydown_handler(keydwn_handler)
frame.set_mouseclick_handler(mouse_click)
# Start the frame animation
frame.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment