среда, 28 мая 2014 г.

Mini-project # 8 - RiceRocks (Asteroids)

An Introduction to Interactive Programming in Python

Mini-project description - RiceRocks (Asteroids)

For our last mini-project, we will complete the implementation of RiceRocks, an updated version of Asteroids,  that we began last week.  You may start with either your code or theprogram template which includes a full implementation of Spaceship and will be released immediately after the deadline for the Spaceship mini-project (by making the preceding link live).  If you start with your own code, you should add the splash screen image that you dismiss with a mouse click before starting this mini-project.  We strongly recommend using Chrome for this mini-project since Chrome's superior performance will become apparent when your program attempts to draw dozens of sprites.

Mini-project development process

At the end of this project, your game will have multiple rocks and multiple missiles.  You will lose a life if your ship collides with a rock and you will score points if your missile collides with a rock. You will keep track of the score and lives remaining and end the game at the proper time.  You may optionally add animated explosions when there is a collision.
Phase one -  Multiple rocks
For this phase, you will keep a set of rocks and spawn new rocks into this set.  This requires the following steps:
  1. Remove a_rock and replace it with rock_group. Initialize the rock group to an empty set. Modify your rock spawner to create a new rock (an instance of a Sprite object) and add it torock_group.
  2. Modify your rock spawner to limit the total number of rocks in the game at any one time. We suggest you limit it to 12. With too many rocks the game becomes less fun and the animation slows down significantly.
  3. Create a helper function process_sprite_group. This function should take a set and a canvas and call the update and draw methods for each sprite in the group.
  4. Call the process_sprite_group function on rock_group in the draw handler.
Phase two - Collisions
For this phase, you will detect collisions between the ship and a rock. Upon a collision, the rock should be destroyed and the player should lose a life. To implement ship-rock collisions, you need to do the following:
  1. Add a collide method to the Sprite class. This should take an other_object as an argument and return True if there is a collision or False otherwise. For now, this other object will always be your ship, but we want to be able to use this collide method to detect collisions with missiles later, as well. Collisions can be detected using the radius of the two objects. This requires you to implement methods get_position and get_radius on both the Sprite and Ship classes.
  2. Implement a group_collide helper function. This function should take a set group and an a sprite other_object and check for collisions between other_object and elements of the group. If there is a collision, the colliding object should be removed from the group. To avoid removing an object from a set that you are iterating over (which can cause you a serious debugging headache), iterate over a copy of the set created via set(group). This function should return True or False depending on whether there was a collision. Be sure to use the collide method from part 1 on the sprites in the group to accomplish this task.
  3. In the draw handler, use the group_collide helper to determine if the ship hit any of the rocks. If so, decrease the number of lives by one. Note that you could have negative lives at this point. Don't worry about that yet.
At this point, you should have a game of "dodge 'em". You can fly around trying to avoid the rocks!
Phase three - Missiles
For this phase, you will keep a set of missiles and spawn new missiles into this set when firing using the space bar. This requires the following steps:
  1. Remove a_missile and replace it with missile_group.  Initialize the missile group to an empty set.  Modify your shoot method of my_ship to create a new missile (an instance of the Sprite class) and add it to the missile_group. If you use our code, the firing sound should play automatically each time a missile is spawned.
  2. In the draw handler, use your helper function process_sprite_group to process missile_group. While you can now shoot multiple missiles, you will notice that they stick around forever. To fix this, we need to modify the Sprite class and the process_sprite_group function.
  3. In the update method of the Sprite class, increment the age of the sprite every time update is called. If the age is greater than or equal to the lifespan of the sprite, then we want to remove it. So, return False (meaning we want to keep it) if the age is less than the lifespan and True (meaning we want to remove it) otherwise.
  4. Modify process_sprite_group to check the return value of update for sprites. If it returns True, remove the sprite from the group. Again, you will want to iterate over a copy of the sprite group in process_sprite_group to avoid deleting from the same set over which you are iterating.
Phase four - Collisions revisited Now, we want to destroy rocks when they are hit by a missile. We can't quite use group_collide, because we want to check for collisions between two groups. All we need to do is add one more helper function:
  1. Implement a final helper function group_group_collide that takes two groups of objects as input. group_group_collide should iterate through the elements of a copy of the first group using a for-loop and then call group_collide with each of these elements on the second group. group_group_collide should return the number of elements in the first group that collide with the second group as well as delete these elements in the first group. You may find the discard method for sets to be helpful here.
  2. Call group_group_collide in the draw handler to detect missile/rock collisions. Increment the score by the number of missile collisions.
Phase five - Finish it off
You now have a mostly working version of RiceRocks!!! Let's add a few final touches.
  1. Add code to the draw handler such that, if the number of lives becomes 0, the game is reset and the splash screen appears. In particular, set the flag started to False, destroy all rocks and prevent any more rocks for spawning until the game is restarted.
  2. When the game starts/restarts, make sure the lives and the score are properly initialized. Start spawning rocks. Play/restart the background music loaded in the variablesoundtrack in the program template.
  3. When you spawn rocks, you want to make sure they are some distance away from the ship. Otherwise, you can die when a rock spawns on top of you, which isn't much fun. One simple way to achieve this effect to ignore a rock spawn event if the spawned rock is too close to the ship.
  4. Experiment with varying the velocity of rocks based on the score to make game play more difficult as the game progresses.
  5. Tweak any constants that you have to make the game play the way you want.
Congratulations! You have completed the assignment. Enjoy playing your game!!!
Bonus
The following will not be graded. Feel free to try this, but do not break any of the other game functionality. We strongly recommend that you save your work before doing this and keep track of it, so you can submit a working version of the first five phases if you end up breaking your game trying to add more features.
One thing that is missing in your game is explosions when things collide. We have provided a tiled explosion image that you can use to create animated explosions. To get things working, you will need to do a few things:
  1. In the draw method of the Sprite class, check if self.animated is True. If so, then choose the correct tile in the image based on the age. The image is tiled horizontally. Ifself.animated is False, it should continue to draw the sprite as before.
  2. Create an explosion_group global variable and initialize it to an empty set.
  3. In group_collide, if there is a collision, create a new explosion (an instance of the Sprite class)and add it to the explosion_group. Make sure that each explosion plays the explosion sound.
  4. In the draw handler, use process_sprite_group to process explosion_group.
You should now have explosions working!


SOLUTION
# implementation of Spaceship - program template for RiceRocks
import simplegui
import math
import random

# globals for user interface
WIDTH = 800
HEIGHT = 600
score = 0
lives = 3
time = 0
started = False
##############
rock_shift = 1
##############

class ImageInfo:
    def __init__(self, center, size, radius = 0, lifespan = None, animated = False):
        self.center = center
        self.size = size
        self.radius = radius
        if lifespan:
            self.lifespan = lifespan
        else:
            self.lifespan = float('inf')
        self.animated = animated

    def get_center(self):
        return self.center

    def get_size(self):
        return self.size

    def get_radius(self):
        return self.radius

    def get_lifespan(self):
        return self.lifespan

    def get_animated(self):
        return self.animated

    
# art assets created by Kim Lathrop, may be freely re-used in non-commercial projects, please credit Kim
    
# debris images - debris1_brown.png, debris2_brown.png, debris3_brown.png, debris4_brown.png
#                 debris1_blue.png, debris2_blue.png, debris3_blue.png, debris4_blue.png, debris_blend.png
debris_info = ImageInfo([320, 240], [640, 480])
debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")

# nebula images - nebula_brown.png, nebula_blue.png
nebula_info = ImageInfo([400, 300], [800, 600])
nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.s2014.png")

# splash image
splash_info = ImageInfo([200, 150], [400, 300])
splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")

# ship image
ship_info = ImageInfo([45, 45], [90, 90], 35)
ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")

# missile image - shot1.png, shot2.png, shot3.png
missile_info = ImageInfo([5,5], [10, 10], 3, 50)
missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")

# asteroid images - asteroid_blue.png, asteroid_brown.png, asteroid_blend.png
asteroid_info = ImageInfo([45, 45], [90, 90], 40)
asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")

# animated explosion - explosion_orange.png, explosion_blue.png, explosion_blue2.png, explosion_alpha.png
explosion_info = ImageInfo([64, 64], [128, 128], 17, 24, True)
explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/explosion_alpha.png")

# sound assets purchased from sounddogs.com, please do not redistribute
# .ogg versions of sounds are also available, just replace .mp3 by .ogg
soundtrack = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/soundtrack.mp3")
missile_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/missile.mp3")
missile_sound.set_volume(.5)
ship_thrust_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust.mp3")
explosion_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/explosion.mp3")

# helper functions to handle transformations
def angle_to_vector(ang):
    return [math.cos(ang), math.sin(ang)]

def dist(p, q):
    return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)

#########################
def process_sprite_group(canvas, rock_group):
    for i in rock_group:
        i.draw(canvas)
        if i.update():
            rock_group.remove(i)
            
def group_collide(group, other_object):
    num_col = 0
    for i in set(group):
        if i.collide(other_object):
            group.remove(i)
            num_col += 1
    return num_col

def group_group_collide(rock_group, missile_group):
    num_col=0
    for i in set(rock_group): 
        tmp_col=group_collide(missile_group, i)
        if tmp_col:
            num_col+=tmp_col
            rock_group.remove(i)
    return num_col
#########################
# Ship class
class Ship:

    def __init__(self, pos, vel, angle, image, info):
        self.pos = [pos[0], pos[1]]
        self.vel = [vel[0], vel[1]]
        self.thrust = False
        self.angle = angle
        self.angle_vel = 0
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        
    def draw(self,canvas):
        if self.thrust:
            canvas.draw_image(self.image, [self.image_center[0] + self.image_size[0], self.image_center[1]] , self.image_size,
                              self.pos, self.image_size, self.angle)
        else:
            canvas.draw_image(self.image, self.image_center, self.image_size,
                              self.pos, self.image_size, self.angle)
        # canvas.draw_circle(self.pos, self.radius, 1, "White", "White")

    def update(self):
        # update angle
        self.angle += self.angle_vel
        
        # update position
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT

        # update velocity
        if self.thrust:
            acc = angle_to_vector(self.angle)
            self.vel[0] += acc[0] * .1
            self.vel[1] += acc[1] * .1
            
        self.vel[0] *= .99
        self.vel[1] *= .99

    def set_thrust(self, on):
        self.thrust = on
        if on:
            ship_thrust_sound.rewind()
            ship_thrust_sound.play()
        else:
            ship_thrust_sound.pause()
       
    def increment_angle_vel(self):
        self.angle_vel += .04
        
    def decrement_angle_vel(self):
        self.angle_vel -= .04
        
    def shoot(self):
        #global a_missile
        global missile_group 
        forward = angle_to_vector(self.angle)
        missile_pos = [self.pos[0] + self.radius * forward[0], self.pos[1] + self.radius * forward[1]]
        missile_vel = [self.vel[0] + 6 * forward[0], self.vel[1] + 6 * forward[1]]
        #a_missile = Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound)
        missile_group.append(Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound))
###########################
    def get_position(self):
        return self.pos
    
    def get_radius(self):
        return self.radius
###########################
    
    
# Sprite class
class Sprite:
    def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None):
        self.pos = [pos[0], pos[1]]
        self.vel = [vel[0], vel[1]]
        self.angle = ang
        self.angle_vel = ang_vel
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        self.lifespan = info.get_lifespan()
        self.animated = info.get_animated()
        self.age = 0
        if sound:
            sound.rewind()
            sound.play()
   
    def draw(self, canvas):
        if self.animated:
            temp_center=[self.image_center[0]+self.age*self.image_size[0],self.image_center[1]]
            canvas.draw_image(self.image, temp_center, self.image_size, self.pos, self.image_size,self.angle)
        else:
            canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)

    def update(self):
        # update angle
        self.angle += self.angle_vel
        
        # update position
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
###########################
        self.age += 1
        if self.age > self.lifespan:
            return True
        else:
            return False
        
###########################
    def get_position(self):
        return self.pos
    
    def get_radius(self):
        return self.radius
    
    def collide(self,other_object):
        #global explosion_group
        if dist(self.get_position(), other_object.get_position()) < self.get_radius() + other_object.get_radius():
            foward = angle_to_vector(self.angle)
            temp_pos = [self.pos[0] + self.get_radius(), self.pos[1] + self.get_radius()]
            a_explosion = Sprite(temp_pos, [0,0], 0, 0, explosion_image, explosion_info, explosion_sound)
            explosion_group.append(a_explosion)
            return True
        else:
            return False
        
        def decrement_angle_vel(self):
            self.angle_vel -= 0.08
        def increment_angle_vel(self):
            self.angle_vel += 0.08
##########################
  
        
# key handlers to control ship   
def keydown(key):
    if key == simplegui.KEY_MAP['left']:
        my_ship.decrement_angle_vel()
    elif key == simplegui.KEY_MAP['right']:
        my_ship.increment_angle_vel()
    elif key == simplegui.KEY_MAP['up']:
        my_ship.set_thrust(True)
    elif key == simplegui.KEY_MAP['space']:
        my_ship.shoot()
        
def keyup(key):
    if key == simplegui.KEY_MAP['left']:
        my_ship.increment_angle_vel()
    elif key == simplegui.KEY_MAP['right']:
        my_ship.decrement_angle_vel()
    elif key == simplegui.KEY_MAP['up']:
        my_ship.set_thrust(False)
        
# mouseclick handlers that reset UI and conditions whether splash image is drawn
def click(pos):
    global started, my_ship, lives, score, rock_shift
    center = [WIDTH / 2, HEIGHT / 2]
    size = splash_info.get_size()
    inwidth = (center[0] - size[0] / 2) < pos[0] < (center[0] + size[0] / 2)
    inheight = (center[1] - size[1] / 2) < pos[1] < (center[1] + size[1] / 2)
    if (not started) and inwidth and inheight:
        started = True
        my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
        lives=3
        score=0
        rock_shift = 1
        soundtrack.rewind()
        soundtrack.play()

def draw(canvas):
    global time, started, rock_group, explosion_group, missile_group, lives, score 
    
    # animiate background
    time += 1
    wtime = (time / 4) % WIDTH
    center = debris_info.get_center()
    size = debris_info.get_size()
    canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT])
    canvas.draw_image(debris_image, center, size, (wtime - WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))
    canvas.draw_image(debris_image, center, size, (wtime + WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))

    # draw UI
    canvas.draw_text("Lives", [50, 50], 22, "White")
    canvas.draw_text("Score", [680, 50], 22, "White")
    canvas.draw_text(str(lives), [50, 80], 22, "White")
    canvas.draw_text(str(score), [680, 80], 22, "White")

    # draw ship and sprites
    my_ship.draw(canvas)
    #a_rock.draw(canvas)
    process_sprite_group(canvas, rock_group)
    process_sprite_group(canvas, explosion_group)
    #a_missile.draw(canvas)
    process_sprite_group(canvas, missile_group)
    
    # update ship and sprites
    my_ship.update()
    #a_rock.update()
    #a_missile.update()

    # draw splash screen if not started
    if not started:
        canvas.draw_image(splash_image, splash_info.get_center(), 
                          splash_info.get_size(), [WIDTH / 2, HEIGHT / 2], 
                          splash_info.get_size())
##################################
    if group_collide(rock_group, my_ship):
            lives -= 1

    if lives == 0:
        started = False
        rock_group = []
        explosion_group = []

    if group_group_collide(rock_group, missile_group):
        score += 1        
    

##################################

# timer handler that spawns a rock    
def rock_spawner():
#    global a_rock
    global rock_group, rock_shift
#    rock_pos = [random.randrange(0, WIDTH), random.randrange(0, HEIGHT)]
#    rock_vel = [random.random() * .6 - .3, random.random() * .6 - .3]
#    rock_avel = random.random() * .2 - .1
#    a_rock = Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image, asteroid_info)
    
    rock_shift *= 1.05
    if len(rock_group) >= 12 or not started:
        return
    rock_pos = [random.randrange(0, WIDTH), random.randrange(0, HEIGHT)]
    while dist(my_ship.get_position(), rock_pos) < random.randrange(50, 200):
        rock_pos=[random.randrange(0, WIDTH), random.randrange(0, HEIGHT)]
#    rock_vel = [random.random() * .6 - .3, random.random() * .6 - .3]
    rock_vel = [rock_shift * random.random() * .6 - .3, rock_shift * random.random() * .6 - .3]
    rock_avel = random.random() * .2 - .1
    rock_group.append( Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image, asteroid_info))
            
# initialize stuff
frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)

# initialize ship and two sprites
my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
#a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, .1, asteroid_image, asteroid_info)
rock_group = []
explosion_group=[]
#a_missile = Sprite([2 * WIDTH / 3, 2 * HEIGHT / 3], [-1,1], 0, 0, missile_image, missile_info, missile_sound)
missile_group=[]

# register handlers
frame.set_keyup_handler(keyup)
frame.set_keydown_handler(keydown)
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw)

timer = simplegui.create_timer(1000.0, rock_spawner)

# get things rolling
timer.start()
frame.start()

The code reference (CodeSkulptor )

Mini-project # 7 - Spaceship

An Introduction to Interactive Programming in Python

Mini-project description - Spaceship

In our last two mini-projects, we will build a 2D space game RiceRocks that is inspired by the classic arcade game Asteroids (1979). Asteroids is a relatively simple game by today's standards, but was still immensely popular during its time. (Joe spent countless quarters playing it.) In the game, the player controls a spaceship via four buttons: two buttons that rotate the spaceship clockwise or counterclockwise (independent of its current velocity), a thrust button that accelerates the ship in its forward direction and a fire button that shoots missiles. Large asteroids spawn randomly on the screen with random velocities. The player's goal is to destroy these asteroids before they strike the player's ship. In the arcade version, a large rock hit by a missile split into several fast moving small asteroids that themselves must be destroyed. Occasionally, a flying saucer also crosses the screen and attempts to destroy the player's spaceship. Searching for "asteroids arcade" yields links to multiple versions of Asteroids that are available on the web (including an updated version by Atari, the original creator of Asteroids).

Mini-project development process

For this mini-project, you will implement a working spaceship plus add a single asteroid and a single missile. We have provided art for your game so its look and feel is that of a more modern game. You should begin by loading the program template.The program template includes all necessary image and audio files. Unfortunately, no audio format is supported by all major browsers so we have decided to provided sounds in the mp3 format which is supported by Chrome (but not by Firefox on some systems). (ogg versions are also available.) We highly recommend using Chrome for the last two weeks of the class. We have found that Chrome typically has better performance on games with more substantial drawing requirements and standardization on a common browser will make peer assessing projects more reliable.
Phase one - Spaceship
In this phase, you will implement the control scheme for the spaceship.This includes a complete Spaceship class and the appropriate keyboard handlers to control the spaceship. Your spaceship should behave as follows:
  • The left and right arrows should control the orientation of your spaceship. While the left arrow is held down, your spaceship should turn counter-clockwise. While the right arrow is down, your spaceship should turn clockwise. When neither key is down, your ship should maintain its orientation. You will need to pick some reasonable angular velocity at which your ship should turn.
  • The up arrow should control the thrusters of your spaceship. The thrusters should be on when the up arrow is down and off when it is up. When the thrusters are on, you should draw the ship with thrust flames. When the thrusters are off, you should draw the ship without thrust flames.
  • When thrusting, the ship should accelerate in the direction of its forward vector. This vector can be computed from the orientation/angle of the ship using the provided helper functionangle_to_vector. You will need to experiment with scaling each component of this acceleration vector to generate a reasonable acceleration.
  • Remember that while the ship accelerates in its forward direction, but the ship always moves in the direction of its velocity vector. Being able to accelerate in a direction different than the direction that you are moving is a hallmark of Asteroids.
  • Your ship should always experience some amount of friction. (Yeah, we know, "Why is there friction in the vacuum of space?". Just trust us there is in this game.) This choice means that the velocity should always be multiplied by a constant factor less than one to slow the ship down. It will then come to a stop eventually after you stop the thrusters.
Now, implement these behaviors above in order. Each step should require just a few lines of code. Here are some hints:
  1. Modify the draw method for the Ship class to draw the ship image (without thrust flames) instead of a circle. This method should incorporate the ship's position and angle. Note that the angle should be in radians, not degrees. Since a call to the ship's draw method already exists in the draw handler, you should now see the ship image. Experiment with different positions and angles for the ship.
  2. Implement an initial version of the update method for the ship. This version should update the position of the ship based on its velocity. Since a call to the update method also already exists in the draw handler, the ship should move in response to different initial velocities.
  3. Modify the update method for the ship to increment its angle by its angular velocity.
  4. Make your ship turn in response to the left/right arrow keys. Add keydown and keyup handlers that check the left and right arrow keys. Add methods to the Ship class to increment and decrement the angular velocity by a fixed amount. (There is some flexibility in how you structure these methods.) Call these methods in the keyboard handlers appropriately and verify that you can turn your ship as you expect.
  5. Modify the keyboard handlers to turn the ship's thrusters on/off. Add a method to the Ship class to turn the thrusters on/off (you can make it take a Boolean argument which is True or False to decide if they should be on or off).
  6. Modify the ship's draw method to draw the thrust image when it is on. (The ship image is tiled and contains both images of the ship.)
  7. Modify the ship's thrust method to play the thrust sound when the thrust is on. Rewind the sound when the thrust turns off.
  8. Add code to the ship's update method to use the given helper function angle_to_vector to compute the forward vector pointing in the direction the ship is facing based on the ship's angle.
  9. Next, add code to the ship's update method to accelerate the ship in the direction of this forward vector when the ship is thrusting. You will need to update the velocity vector by a small fraction of the forward acceleration vector so that the ship does not accelerate too fast.
  10. Then, modify the ship's update method such that the ship's position wraps around the screen when it goes off the edge (use modular arithmetic!).
  11. Up to this point, your ship will never slow down. Finally, add friction to the ship's update method as shown in the "Acceleration and Friction" video by multiplying each component of the velocity by a number slightly less than 1 during each update.
You should now have a ship that flies around the screen,as you would like for RiceRocks. Adjust the constants as you would like to get it to fly how you want.
Phase two - Rocks
To implement rocks, we will use the provided Sprite class. Note that the update method for the sprite will be very similar to the update method for the ship. The primary difference is that the ship's velocity and rotation are controlled by keys, whereas sprites have these set randomly when they are created. Rocks should screen wrap in the same manner as the ship.
In the template, the global variable a_rock is created at the start with zero velocity. Instead, we want to create version of a_rock once every second in the timer handler. Next week, we will add multiple rocks. This week, the ship will not die if it hits a rock. We'll add that next week. To implement rocks, we suggest the following:
  1. Complete the Sprite class (as shown in the "Sprite class" video) by modifying the draw handler to draw the actual image and the update handler to make the sprite move and rotate. Rocks do not accelerate or experience friction, so the sprite update method should be simpler than the ship update method. Test this by giving a_rock different starting parameters and ensuring it behaves as you expect.
  2. Implement the timer handler rock_spawner. In particular, set a_rock to be a new rock on every tick. (Don't forget to declare a_rock as a global in the timer handler.) Choose a velocity, position, and angular velocity randomly for the rock. You will want to tweak the ranges of these random numbers, as that will affect how fun the game is to play. Make sure you generated rocks that spin in both directions and, likewise, move in all directions.
Phase three - Missiles
To implement missiles, we will use the same sprite class as for rocks. Missiles will always have a zero angular velocity. They will also have a lifespan (they should disappear after a certain amount of time or you will eventually have missiles all over the place), but we will ignore that this week. Also, for now, we will only allow a single missile and it will not yet blow up rocks. We'll add more next week.
Your missile should be created when you press the spacebar, not on a timer like rocks. They should screen wrap just as the ship and rocks do. Otherwise, the process is very similar:
  1. Add a shoot method to your ship class. This should spawn a new missile (for now just replace the old missile in a_missile). The missile's initial position should be the tip of your ship's "cannon". Its velocity should be the sum of the ship's velocity and a multiple of the ship's forward vector.
  2. Modify the keydown handler to call this shoot method when the spacebar is pressed.
  3. Make sure that the missile sound is passed to the sprite initializer so that the shooting sound is played whenever you shoot a missile.
Phase four - User interface
Our user interface for RiceRocks simply shows the number of lives remaining and the score. This week neither of those elements ever change, but they will next week. Add code to the draw event handler to draw these on the canvas. Use the lives and score global variables as the current lives remaining and score.


SOLUTION
# program template for Spaceship
import simplegui
import math
import random

# globals for user interface
WIDTH = 800
HEIGHT = 600
score = 0
lives = 3
time = 0.5
ANGLE_VEL_INC = 0.02
angle_vel = 0


class ImageInfo:
    def __init__(self, center, size, radius = 0, lifespan = None, animated = False):
        self.center = center
        self.size = size
        self.radius = radius
        if lifespan:
            self.lifespan = lifespan
        else:
            self.lifespan = float('inf')
        self.animated = animated

    def get_center(self):
        return self.center

    def get_size(self):
        return self.size

    def get_radius(self):
        return self.radius

    def get_lifespan(self):
        return self.lifespan

    def get_animated(self):
        return self.animated

    
# art assets created by Kim Lathrop, may be freely re-used in non-commercial projects, please credit Kim
    
# debris images - debris1_brown.png, debris2_brown.png, debris3_brown.png, debris4_brown.png
#                 debris1_blue.png, debris2_blue.png, debris3_blue.png, debris4_blue.png, debris_blend.png
debris_info = ImageInfo([320, 240], [640, 480])
debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")

# nebula images - nebula_brown.png, nebula_blue.png
nebula_info = ImageInfo([400, 300], [800, 600])
nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.s2014.png")

# splash image
splash_info = ImageInfo([200, 150], [400, 300])
splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")

# ship image
ship_info = ImageInfo([45, 45], [90, 90], 35)
ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")

# missile image - shot1.png, shot2.png, shot3.png
missile_info = ImageInfo([5,5], [10, 10], 3, 50)
missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")

# asteroid images - asteroid_blue.png, asteroid_brown.png, asteroid_blend.png
asteroid_info = ImageInfo([45, 45], [90, 90], 40)
asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")

# animated explosion - explosion_orange.png, explosion_blue.png, explosion_blue2.png, explosion_alpha.png
explosion_info = ImageInfo([64, 64], [128, 128], 17, 24, True)
explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/explosion_alpha.png")

# sound assets purchased from sounddogs.com, please do not redistribute
soundtrack = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/soundtrack.mp3")
missile_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/missile.mp3")
missile_sound.set_volume(.5)
ship_thrust_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust.mp3")
explosion_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/explosion.mp3")

# helper functions to handle transformations
def angle_to_vector(ang):
    return [math.cos(ang), math.sin(ang)]

def dist(p,q):
    return math.sqrt((p[0] - q[0]) ** 2+(p[1] - q[1]) ** 2)

##############################################################
# Ship class
class Ship:
    def __init__(self, pos, vel, angle, image, info):
        self.pos = [pos[0],pos[1]]
        self.vel = [vel[0],vel[1]]
        self.thrust = False
        self.angle = angle
        self.angle_vel = 0
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        
        
    def draw(self,canvas):
        #canvas.draw_circle(self.pos, self.radius, 1, "White", "White")
        if self.thrust:
            canvas.draw_image(self.image, [self.image_center[0] + self.image_size[0], self.image_center[1]], self.image_size, self.pos, self.image_size, self.angle)
        else:
            canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)

    def update(self):
        self.angle += self.angle_vel
        
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
        
        
        if self.thrust:
            acc = angle_to_vector(self.angle)
            self.vel[0] += acc[0] * .1
            self.vel[1] += acc[1] * .1
                  
        self.vel[0] *= .99
        self.vel[1] *= .99
     
    def set_thrust(self, on):
        self.thrust = on
        if on:
            ship_thrust_sound.rewind()
            ship_thrust_sound.play()
        else:
            ship_thrust_sound.pause()
            
    def increment_angle_vel(self):
        self.angle_vel += ANGLE_VEL_INC
        
    def decrement_angle_vel(self):
        self.angle_vel -= ANGLE_VEL_INC
        
    def shoot(self):
        global a_missile
        forward = angle_to_vector(self.angle)
        missile_pos = [self.pos[0] + self.radius * forward[0],
                       self.pos[1] + self.radius * forward[1]]
        missile_vel = [self.vel[0] + 6 * forward[0],
                       self.vel[1] + 6 * forward[1]]
        a_missile = Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound)
            
##################################################################################    
# Sprite class
class Sprite:
    def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None):
        self.pos = [pos[0],pos[1]]
        self.vel = [vel[0],vel[1]]
        self.angle = ang
        self.angle_vel = ang_vel
        self.image = image
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.radius = info.get_radius()
        self.lifespan = info.get_lifespan()
        self.animated = info.get_animated()
        self.age = 0
        if sound:
            sound.rewind()
            sound.play()
   
    def draw(self, canvas):
        #canvas.draw_circle(self.pos, self.radius, 1, "Red", "Red")
        canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)
    
    def update(self):
        self.angle += self.angle_vel
        
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT 
        

##################################################################################           
def draw(canvas):
    global time
    
    # animiate background
    time += 1
    wtime = (time / 4) % WIDTH
    center = debris_info.get_center()
    size = debris_info.get_size()
    canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT])
    canvas.draw_image(debris_image, center, size, (wtime - WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))
    canvas.draw_image(debris_image, center, size, (wtime + WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT))

    # draw ship and sprites
    my_ship.draw(canvas)
    a_rock.draw(canvas)
    a_missile.draw(canvas)
    
    # update ship and sprites
    my_ship.update()
    a_rock.update()
    a_missile.update()
    
    
    canvas.draw_text('Lives: '+str(lives), (30, 30), 24, "White")
    canvas.draw_text('Score: '+str(score), (700, 30), 24, "White")
##################################################################################
# define keyhandlers to control firing_angle
def keydown(key):
    global angle_vel, thrust
    if simplegui.KEY_MAP["left"] == key:
        my_ship.decrement_angle_vel()
    elif simplegui.KEY_MAP["right"] == key:
        my_ship.increment_angle_vel()
    elif simplegui.KEY_MAP["up"] == key:
        my_ship.set_thrust(True)
    elif key == simplegui.KEY_MAP['space']:
        my_ship.shoot()
    
def keyup(key):
    global angle_vel, thrust
    if simplegui.KEY_MAP["left"] == key:
        my_ship.increment_angle_vel()
    elif simplegui.KEY_MAP["right"] == key:
        my_ship.decrement_angle_vel()
    elif simplegui.KEY_MAP["up"] == key:
        my_ship.set_thrust(False)
        
####################################################################################    
# timer handler that spawns a rock    
def rock_spawner():
    global a_rock
    rock_pos = [random.randrange(WIDTH), random.randrange(HEIGHT)]
    rock_vel = [random.random(), random.random()]
    rock_avel = random.random() * .2 - .1
    a_rock = Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image, asteroid_info)
#####################################################################################

# initialize frame
frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)

# initialize ship and two sprites
my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, 0, asteroid_image, asteroid_info)
a_missile = Sprite([2 * WIDTH / 3, 2 * HEIGHT / 3], [-1,1], 0, 0, missile_image, missile_info, missile_sound)

# register handlers
frame.set_draw_handler(draw)

frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)

timer = simplegui.create_timer(1000.0, rock_spawner)

# get things rolling
timer.start()
frame.start()

The code reference (CodeSkulptor )

Mini-project # 6 - Blackjack

An Introduction to Interactive Programming in Python

Mini-project description - Blackjack

Blackjack is a simple, popular card game that is played in many casinos. Cards in Blackjack have the following values: an ace may be valued as either 1 or 11 (player's choice), face cards (kings, queens and jacks) are valued at 10 and the value of the remaining cards corresponds to their number. During a round of Blackjack, the players plays against a dealer with the goal of building a hand (a collection of cards) whose cards have a total value that is higher than the value of the dealer's hand, but not over 21.  (A round of Blackjack is also sometimes referred to as a hand.)
The game logic for our simplified version of Blackjack is as follows. The player and the dealer are each dealt two cards initially with one of the dealer's cards being dealt faced down (hishole card). The player may then ask for the dealer to repeatedly "hit" his hand by dealing him another card. If, at any point, the value of the player's hand exceeds 21, the player is "busted" and loses immediately. At any point prior to busting, the player may "stand" and the dealer will then hit his hand until the value of his hand is 17 or more. (For the dealer, aces count as 11 unless it causes the dealer's hand to bust). If the dealer busts, the player wins. Otherwise, the player and dealer then compare the values of their hands and the hand with the higher value wins. The dealer wins ties in our version.

Mini-project development process

We suggest you develop your Blackjack game in two phases. The first phase will concentrate on implementing the basic logic of Blackjack while the second phase will focus on building a more full-featured version. In phase one, you will use buttons to control the game and print the state of the game to the console using print statements. In the second phase, you will replace the print statements by drawing images and text on the canvas and add some extra game logic.
In phase one, we will provide testing templates for four of the steps. The templates are designed to check whether your class implementations work correctly. You should copy your class definition into the testing template and compare the console output generated by running the template with the provided output. If the output matches, it is likely that your implementation of the class is correct. DO NOT PROCEED TO THE NEXT STEP UNTIL YOUR CODE WORKS WITH THE PROVIDED TESTING TEMPLATE. Debugging code that uses incorrectly implemented classes is extremely difficult. Avoid this problem by using our provided testing templates.
Phase one
  1. Download the program template for this mini-project and review the class definition for the Card class. This class is already implemented so your task is to familiarize yourself with the code. Start by pasting the Card class definition into the provided testing template and verifying that our implementation works as expected.
  2. Implement the methods __init__, __str__, add_card for the Hand class. We suggest modeling a hand as a list of cards. For help in implementing the __str__ method for hands, refer back to practice exercise number four from last week. Remember to use the string method for cards to convert each card object into a string. Once you have implemented theHand class, test it using the provided testing template.
  3. Implement the methods for the Deck class listed in the mini-project template. We suggest modeling a deck of cards as list of cards. You can generate this list using a pair of nestedfor loops or a list comprehension. Remember to use the Card initializer to create your cards. Use random.shuffle() to shuffle this deck of cards. Once you have implemented theDeck class, test your Deck class using the provided testing template. Remember that the deck is randomized after shuffling, so the output of the testing template should match the output in the comments in form but not in exact value.
  4. Implement the handler for a "Deal" button that shuffles the deck and deals the two cards to both the dealer and the player. The event handler deal for this button should shuffle the deck (stored as a global variable), create new player and dealer hands (stored as global variables), and add two cards to each hand. To transfer a card from the deck to a hand, you should use the deal_card method of the Deck class and the add_card method of Hand class in combination. The resulting hands should be printed to the console with an appropriate message indicating which hand is which.
  5. Implement the get_value method for the Hand class. You should use the provided VALUE dictionary to look up the value of a single card in conjunction with the logic explained in the video lecture for this project to compute the value of a hand. Once you have implemented the get_value method, test it using the provided testing template 
  6. Implement the handler for a "Hit" button. If the value of the hand is less than or equal to 21, clicking this button adds an extra card to player's hand. If the value exceeds 21 after being hit, print "You have busted".
  7. Implement the handler for a "Stand" button. If the player has busted, remind the player that they have busted. Otherwise, repeatedly hit the dealer until his hand has value 17 or more (using a while loop). If the dealer busts, let the player know. Otherwise, compare the value of the player's and dealer's hands. If the value of the player's hand is less than or equal to the dealer's hand, the dealer wins. Otherwise the player has won. Remember the dealer wins ties in our version.
In our version of Blackjack, a hand is automatically dealt to the player and dealer when the program starts. In particular, the program template includes a call to the deal() function during initialization. At this point, we would suggest testing your implementation of Blackjack extensively.
Phase two
In the second phase of your implementation, you will add five features. For those involving drawing with global variables, remember to initialize these variables to appropriate values (like creating empty hands for the player and dealer) just before starting the frame.  
  1. Implement your own draw method for the Hand class using the draw method of the Card class. We suggest drawing a hand as a horizontal sequence of cards where the parameter pos is the position of the upper left corner of the leftmost card. To simplify your code, you may assume that only the first five cards of a player's hand need to be visible on the canvas.
  2. Replace printing in the console by drawing text messages on the canvas. We suggest adding a global outcome string that is drawn in the draw handler using draw_text. These messages should prompt the player to take some require action and have a form similar to "Hit or stand?" and "New deal?". Also, draw the title of the game, "Blackjack", somewhere on the canvas.
  3. Add logic using the global variable in_play that keeps track of whether the player's hand is still being played. If the round is still in play, you should draw an image of the back of a card (provided in the template) over the dealer's first (hole) card to hide it. Once the round is over, the dealer's hole card should be displayed.
  4. Add a score counter that keeps track of wins and losses for your Blackjack session. In the simplest case (see our demo), the program displays wins minus losses. However, you are welcome to implement a more sophisticated betting/scoring system.
  5. Modify the logic for the "Deal" button to create and shuffle a new deck (or restock and shuffle an existing deck) each time the "Deal" button is clicked. This change avoids the situation where the deck becomes empty during play.
  6. Finally, modify the deal function such that, if the "Deal" button is clicked during the middle of a round, the program reports that the player lost the round and updates the score appropriately.
Congratulations! You have just built Blackjack. To wrap things up, please review the demo of our version of Blackjack in the Blackjack video lecture to ensure that your version has full functionality.


SOLUTION
# Mini-project #6 - Blackjack

import simplegui
import random

# load card sprite - 949x392 - source: jfitz.com
CARD_SIZE = (73, 98)
CARD_CENTER = (36.5, 49)
card_images = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/cards.jfitz.png")

CARD_BACK_SIZE = (71, 96)
CARD_BACK_CENTER = (35.5, 48)
card_back = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/card_back.png")    

# initialize some useful global variables
in_play = False
outcome = ""
score = 20 
stavka = 1
no_money = False
player_win = 0
dealer_win = 0
player_lost = 0
dealer_lost = 0

# define globals for cards
SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}


# define card class
class Card:
    def __init__(self, suit, rank):
        if (suit in SUITS) and (rank in RANKS):
            self.suit = suit
            self.rank = rank
        else:
            self.suit = None
            self.rank = None
            print "Invalid card: ", suit, rank

    def __str__(self):
        return self.suit + self.rank

    def get_suit(self):
        return self.suit

    def get_rank(self):
        return self.rank

    def draw(self, canvas, pos):
        card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank), 
                    CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
        canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
        
# define hand class
class Hand:
    def __init__(self):
        self.card = []
        self.value= 0 # create Hand object

    def __str__(self):
        return str(self.get_value()) # return a string representation of a hand

    def add_card(self, card):
        self.card.append(card) # add a card object to a hand

    def get_value(self):
        # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
        self.value = 0 # compute the value of the hand, see Blackjack video
        for i in self.card:
            self.value += VALUES[i[1]]
        for i in self.card:
            if VALUES[i[1]] == 1 and self.value <= 11:
                self.value += 10
        return self.value
    
    def get_cards(self):
        return self.card
   
    def draw(self, canvas, pos):
        position = pos # draw a hand on the canvas, use the draw method for cards
        for cards in self.card:
            card = Card(cards[0], cards[1])
            card.draw(canvas, position) 
            position[0] += 100
     
    def busted(self):
        if self.get_value() > 21:
            return True
        else:
            return False
        
        
# define deck class 
class Deck:
    def __init__(self):
        self.deck = [] # create a Deck object
        for i in range(len(SUITS)):
            for j in range(len(RANKS)):
                self.deck.append([SUITS[i], RANKS[j]])    

    def shuffle(self):
        # shuffle the deck 
        random.shuffle(self.deck)    # use random.shuffle()

    def deal_card(self):
        return self.deck.pop(0) # deal a card object from the deck
    
    def __str__(self):
        pass # return a string representing the deck

deck = 0
aCard = []
player_hand = Hand()
dealer_hand = Hand()

def init(): 
    global deck, aCard, player_hand, dealer_hand, outcome
    deck = Deck()
    deck.shuffle()
    player_hand = Hand()
    dealer_hand = Hand()
    
    for i in range(2):
        player_hand.add_card(deck.deal_card())
        dealer_hand.add_card(deck.deal_card())
    
    outcome = ""
    player_win = 0
    dealer_win = 0
    
def check_cash():
    global no_money
    if score <= 0:
        no_money = True
    else:
        no_money = False
    return no_money
    
def change_stavka(new_stavka):
    global stavka, in_play, score
    if int(new_stavka) <= score and not in_play:
        stavka = int(new_stavka)

def up_stavka():
    global stavka
    if not in_play:
        stavka += 1
        if stavka <= score:
            change_stavka(stavka)
        else:
            stavka = score

def down_stavka():
    global stavka
    if not in_play:
        stavka -= 1
        if stavka > 0:
            change_stavka(stavka)
        else:
            stavka = 1
    
#define event handlers for buttons
def deal():
    global outcome, in_play, score
    if not no_money and stavka <= score:
        init()
        score -= stavka
        in_play = True
    # your code goes here
    
def reset_money():
    global no_money, score, stavka, dealer_win, player_win, dealer_lost, player_lost
    
    no_money = False
    score = 20
    stavka = 1
    player_win = 0
    dealer_win = 0
    player_lost = 0
    dealer_lost = 0    
    deal()     

def hit():
    global player_hand, deck, outcome, in_play, score, stavka, dealer_win, player_win, dealer_lost, player_lost # replace with your code below
    if in_play:
        if not player_hand.busted():
            player_hand.add_card(deck.deal_card())
            if player_hand.busted():
                outcome = "You are busted. Dealer wins!"
                score -= stavka
                in_play = False
                player_lost += 1
                dealer_win += 1
 
    # if the hand is in play, hit the player
   
    # if busted, assign a message to outcome, update in_play and score
       
def stand():
    global player_hand, dealer_hand, outcome, in_play, score, stavka, dealer_win, player_win, dealer_lost, player_lost # replace with your code below
    if in_play:
        while dealer_hand.get_value() < 17:# if hand is in play, repeatedly hit dealer until his hand has value 17 or more
            dealer_hand.add_card(deck.deal_card())   
        if not player_hand.busted() and not dealer_hand.busted():# assign a message to outcome, update in_play and score
            if player_hand.get_value() > dealer_hand.get_value():
                outcome = "You win!"
                score += stavka
                player_win += 1
                dealer_lost += 1
            else:
                outcome = "Dealer wins!"
                score -= stavka
                dealer_win += 1
                player_lost += 1
            in_play = False
        elif player_hand.busted():
            outcome = "Dealer wins!"
            score -= stavka
            dealer_win += 1
            player_lost += 1
            in_play = False
        elif dealer_hand.busted():
            outcome = "Dealer busted. You win!"
            score += 2*stavka
            player_win += 1
            dealer_lost += 1
            in_play = False

# draw handler    
def draw(canvas):
    # test to make sure that card.draw works, replace with your code below
    global player_hand, dealer_hand, in_play
    #card = Card("S", "A")
    #card.draw(canvas, [300, 300])
    player_hand.draw(canvas, [80, 350])
    dealer_hand.draw(canvas, [80, 150])
    if in_play:
        canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [80+CARD_BACK_SIZE[0] // 2, 150+CARD_BACK_SIZE[1] // 2], CARD_BACK_SIZE)
        canvas.draw_text("Select: Hit or Stand",[380,300], 22, "Maroon")
    else:
        canvas.draw_text("Value: "+ str(dealer_hand), [90, 310], 15, "white")
        canvas.draw_text("New Deal??",[380, 300], 22, "Maroon")
        if check_cash():
            canvas.draw_text("Out of cash", [80, 90], 22, "RED")

    canvas.draw_text("BlackJack", [250, 100], 40, "White")
    canvas.draw_text("Dealer's hand", [90, 290], 20, "Navy")
    canvas.draw_text("Player's hand", [90, 490], 20, "Blue")
    canvas.draw_text("Value: "+ str(player_hand), [90, 510], 15, "white")
    canvas.draw_text(outcome, [80, 550], 36, "Orange")
    canvas.draw_text("Remaining money: "+ str(score), [80, 30], 15, "Yellow")
    canvas.draw_text("Your bet: "+ str(stavka), [80, 60], 15, "Yellow")
    canvas.draw_text("Dealer wins: "+ str(dealer_win), [80, 140], 15, "White")
    canvas.draw_text("Player wins: "+ str(player_win), [80, 340], 15, "White")
    canvas.draw_text("Dealer lost: "+ str(dealer_lost), [180, 140], 15, "White")
    canvas.draw_text("Player lost: "+ str(player_lost), [180, 340], 15, "White")
    
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("Green")

#create buttons and canvas callback
frame.add_button("Deal", deal, 100)
frame.add_button("Hit",  hit, 100)
frame.add_button("Stand", stand, 100)
frame.add_button("Reset", reset_money, 100)
frame.add_button("Up bet (+1)", up_stavka, 100)
frame.add_button("Down bet (-1)", down_stavka, 100)
frame.set_draw_handler(draw)

# get things rolling
deal()
frame.start()


# remember to review the gradic rubric

The code reference (CodeSkulptor )