#Import class library
import random
#Create the background using the stage library from codesters
#Disable walls so trees can pass through the stage
stage.set_background_color("snow")
stage.disable_all_walls()
#Create the rode in the center screen with 5 argurments
'''Set x and y axis to 0,0 then set size of the rectangle
width and height,then color'''
road = codesters.Rectangle(0,0,400,500,"lightcyan")
#Create an object names and set the text for speed and the score
#Create 4 parameters including text, x and y axis, along with color
speed_text = codesters.Text("Press Up to Speed Up",0,0,"black")
score_text = codesters.Text("Score: 0", 150,220,"royalblue")
#Create object for user with 3 arguments then access attributes of object
player = codesters.Sprite("car2",0,-200)
player.set_size(.5)
#Create two empty arrays list and create items for loop iteration
trees = []
obstacles = []
#Create object and integer variable and set speed to -5 to be explained tm
score = 0
speed = -5
#Create Boolean Variable which checks the gamestate of the program to stop game
game_over = False
#Create a function to run over and over again for scenary
def create_scenery():
''' With the use of a defintion and then a for loop control flow to
reiterate over a list of items x amt of times with the use of the random library'''
for row in range(10):
#Create tree number with random function to generate
tree_num = random.randint(1,7)
#Create random function for side of trees populate
#NEED to enclose in brackets for random choice
side = random.choice([-1,1])
#Create the tree itself use the use of codesters.Sprite class
'''Argument parameter with three different arguments. Need the sprite x,y axis
Implement bracket place holder to have computer generate tree by passing object
into format function (line 44) '''
#For x axis use mathematical expressions w/ x set coordinate times import side random funct(line 48)
'''For y axis use mathematical expressions with set y coordinate minus row in range, times 50
(staggers trees on y axis)'''
tree = codesters.Sprite("pinetree{}".format(tree_num),230*side,250-row*50)
#Change tree size attribute
tree.set_size(.3)
#Append trees to populate empty array
trees.append(tree)
#Create a for loop which iterates over the now populated array
for tree in trees:
tree.set_y_speed(speed)
#Now you have to call the function ( Must be outside ) End of function
create_scenery()
#Create a function header to speed the car up
def speed_up():
#Call global variable to access modifer in the function
#Local variables are limited to just the function it is located
global speed
#If condition check the speed value below a certain threshold (-8)
#If the speed is > -15, decrement the speed value which speeds up the car
if speed > -15:
speed -= 1
#Hide text with second control flow statement
if speed < -8:
speed_text.hide()
#Create event key and provide input which will call/trigger function
#Event key has two arguments input(up) and output(speed function)
stage.event_key("up", speed_up)
#Create function for slowing down
def brake():
global speed
#This if statement allows the user to slow down
if speed < -4:
speed += 1
if speed >= -8:
speed_text.show()
#Call function and create input(down) and output function(brake)
stage.event_key("down", brake)
#Create left mobility function for car
def left():
player.move_left(10)
stage.event_key("left", left)
# Create right mobility function for car
def right():
player.move_right(10)
stage.event_key("right", right)
#Create function for obstacles of the game
def create_obstacle():
#Have computer randomly choose between two sprite obsticles
image = random.choice(["penguin","car1"])
# Call obstacle from codesters.Sprite with 3 arguements (random sprite,x,y)(like line 43)
# X axis is a range from (180,180 to populate on either side)
sprite = codesters.Sprite(image, random.randint(-180,180),350)
# Set size of sprite
sprite.set_size(.5)
#Create and if condition for sprite rotation and speed variable
'''-5 is used so the obstacle is harder to dodge and call on
global variable speed for player'''
if image == "car1":
sprite.set_rotation(180)
sprite.set_y_speed(-5+speed)
#Set conditional statement for second variable
else:
sprite.set_y_speed(speed)
#Ref empty array list and set in argument parameter
obstacles.append(sprite)
#Create collision function to pass two parameters.
#For this instance the parameters are the car and the trees
def collision(player,hit_sprite):
#import global variables of speed, game_over
global speed, game_over
#If condition of running into either parameter then code that stops game.
#Able to create multiple logic test**
if hit_sprite in obstacles or hit_sprite in trees:
#Stop the full execution of the game
speed = 0
game_over = True #Change game overstate to True with Boolean variable
speed_text.show() #Populate text on screen when game_over is true
speed_text.set_text("Game Over!")
#Create collision animation
player.set_y_speed(-5)
player.turn_left(1000)
player.event_collision(collision)
#Create an update function and import score and speed variable
def update():
global score, speed
#Create calculation for speed import using if conditional
if speed < -8:
score += abs(speed+7) #call absolute value to increase score
score_text.set_text("Score: {}".format(score))#format requires {}
#Create for conditonal to repopulate trees after hitting the max y axis.
for tree in trees:
tree.set_y_speed(speed)
#Once reaches bottom, regenerate to top of y axis (300)
if tree.get_y() < -300:
tree.set_y(300)
#Create an update for the obstacles as game continues
for obs in obstacles:
if obs.get_image_name() == "car1":
obs.set_y_speed(-5+speed)
else:
obs.set_y_speed(speed)
#Create if condition to remove obstacle once it reaches bottom of screen
if obs.get_y() < -300:
obstacles.remove(obs)#.remove is opposite of append
#call the stage class and interval time of 0.1 seconds.
#need two aruguments, function and time of interval
stage.event_interval(update,.1)
# Create main function, always at the end of the code
def main ():
while not game_over:
create_obstacle()
stage.wait(random.uniform(.1,1.5))
main()
-
Run Code
-
-
Stop Running Code
-
Show Chart
-
Show Console
-
Codesters How To (opens in a new tab)