#Name: Machi Dima
#disable the edges of field of app
stage.disable_all_walls()
#backgroung color:black
stage.set_background_color("black")
#Set up safe zones as rectangle objects
def setup_safe_zones():
#for loop that repeats rectangle objects
for x in range(5):
safe_zone = codesters.Rectangle(-240 + (x * 100), 0, 20, 500, "grey")
safe_zone = codesters.Rectangle(240, 0, 20, 500, "gold")
setup_safe_zones()
###############################################################################
#Create variables for player, trains, boolean still_playing,display, and score
trains = []
player = codesters.Circle(-240, 0, 18, "red")
still_playing = True
display = codesters.Text(" ", 0,0, "red")
score = 0
scoreDisplay = codesters.Text("Score: 0", 9, 220, "gold")
#Monte provided a coin and x_sprite images from url
coin = "http://opengameart.org/sites/default/files/styles/medium/public/Coin1.png"
x_sprite = "http://images3.wikia.nocookie.net/__cb20100127205260/conearth/images/4/43/Red_X_alt.png"
#output you will get when the UP arrow is pressed
def up_key():
player.move_up(20)
stage.event_key("up", up_key)
#function that handles when DOWN arrow is pressed
def down_key():
player.move_down(20)
stage.event_key("down", down_key)
#function that handles when RIGHT arrow is pressed
def right_key():
player.move_right(20)
stage.event_key("right", right_key)
#function that handles when LEFT arrow is pressed
def left_key():
player.move_left(20)
stage.event_key("left", left_key)
#collision between coin and the train
def player_collision(sprite, hit_sprite):
#global variable accesed by all boolean still_playing and int score
global still_playing, score
if hit_sprite.get_image_name("http://opengameart.org/sites/default/files/styles/medium/public/Coin1.png"):
score += 1
scoreDisplay.set_text("Score {}".format(score))
stage.remove_sprite(hit_sprite)
#else if the object hit is white color than boolean value still_playing to false, GAME OVER!
elif hit_sprite.get_color() == "white":
splat = codesters.Sprite(x_sprite, player.get_x(), player.get_y())
splat.set_size(0.5)
still_playing = False
stage.remove_sprite(player)
display.set_text("OOPS!! PLAY AGAIN!")
elif hit_sprite.get_color() == "gold":
still_playing = False
display.set_text("CONGRATULATION! YOU DID IT!!!")
player.event_collision(player_collision)
#function that pulls the objects
def create_train(x, y, direction):
#instentiate the train array
train = []
#define the train speed to random 1-5
trainSpeed = random.randint(1,5)
#for loop that randomly appends train objects to train array
for i in range(random.randint(1,5)):
#creates rectangle objects and assigns them to trainCar
trainCar = codesters.Rectangle(x, y -(60*i*direction), 15, 50, "white")
#sets the random speed to the trainCar object
trainCar.speed = direction * trainSpeed
#Sets y speed
trainCar.set_y_speed(trainCar.speed)
#join(append) trainCar to array train
train.append(trainCar)
#attach train to array trains
trains.append(train)
#initializes trains objects
def init_trains():
#for loop that calls create_train funstion by giving random direction
for x in range(1,24):
#nested condition to check is whether the value doesn't equal 0
if x % 5 is not 0:
#random way to go either UP or DOWN
direction = random.choice([-1,1])
#call create_train function
create_train(-240 + (x * 20), direction * -300, direction)
#Reseting the train
def reset_train(train, lastCar, direction):
# create x and y variables
x = lastCar.get_x()
y = -300 * direction
#for loop that checks if train object is out of stage
for trainCar in train:
#remove train sprite off the stage
stage.remove_sprite(trainCar)
#Remove train object from train array
trains.remove(train)
#create another train object
create_train(x,y, direction)
#handels the coins creation
def init_coins():
#for loop that creates random 10 to 20 coins
for x in range(random.randint(10,20)):
sprite = codesters.Sprite(coin, -220 + (random.randint(0,23) * 20), -220 + (random.randint(0, 23) * 20))
sprite.set_size(0.7)
#main function
def main():
#Starting the coins and trains by calling each function
init_coins()
init_trains()
#While loop that checks boolean value of still_playing
while still_playing:
#for loop that goes through every train object on trains array
for train in trains:
#get last object of array
lastTrainCar = train[len(train)-1]
#get the direction
direction = lastTrainCar.speed / abs(lastTrainCar.speed)
#if direction 1(up) and is out of stage
if direction is 1 and lastTrainCar.get_y() > 300:
#call reset_train function with parameters tran array last object and direction
reset_train(train, lastTrainCar, direction)
#if direction 1(up) and is out of stage
elif direction is -1 and lastTrainCar.get_y() < -300:
#call reset_train function with parameters tran array last object and direction
reset_train(train, lastTrainCar, direction)
#wait 1 sec to display a new trains
stage.wait(1)
#if function that calls main function
if __name__ is "__main__":
main()