diff --git a/Lab 01 - First Program/lab_01.py b/Lab 01 - First Program/lab_01.py index e69de29bb..4fa353956 100644 --- a/Lab 01 - First Program/lab_01.py +++ b/Lab 01 - First Program/lab_01.py @@ -0,0 +1,14 @@ +firstName = str("Rob") +lastName = str('Mixon') +age = int(30) +# cant use backticks for string interpolation +print(type(firstName)) +print(type(age)) +print(f'Hello, my name is {firstName} {lastName}.') +print(f'"I am \n {age} \n years \n old."') +print("""I really + wish + that I + could use backticks....""") +print('Where are the semicolons????') + diff --git a/Lab 02 - Draw a Picture/lab_02.py b/Lab 02 - Draw a Picture/lab_02.py index e69de29bb..415c38673 100644 --- a/Lab 02 - Draw a Picture/lab_02.py +++ b/Lab 02 - Draw a Picture/lab_02.py @@ -0,0 +1,62 @@ +import arcade + +arcade.open_window(800, 800, "landscape") + +#background color +arcade.set_background_color((27, 27, 27)) + +arcade.start_render() + +#Start Drawing +#Ground +arcade.draw_lrtb_rectangle_filled(0, 799, 300, 0, (26, 36, 33)) + +#House +arcade.draw_lrtb_rectangle_filled(300, 700, 500, 250, arcade.color.JET) + +#Chimney +arcade.draw_lrtb_rectangle_filled(600, 650, 625, 500, (20, 20, 20)) + +#Roof +arcade.draw_triangle_filled(300, 500, 500, 600, 700, 500, (43, 43, 43)) + +#Door +arcade.draw_lrtb_rectangle_filled(600, 650, 350, 250, arcade.color.DARK_BROWN) + +#Door Handle +arcade.draw_circle_filled(610, 290, 3, arcade.color.BLACK) + +#Door Window +arcade.draw_ellipse_outline(625, 320, 30, 50, arcade.color.BLACK) + +#Light From House +arcade.draw_ellipse_filled(625, 320, 28, 48, arcade.color.DARK_YELLOW) +arcade.draw_rectangle_filled(420, 320, 123, 48, arcade.color.DARK_YELLOW) +arcade.draw_rectangle_filled(420, 450, 58, 58, arcade.color.DARK_YELLOW) +arcade.draw_rectangle_filled(625, 450, 58, 58, arcade.color.DARK_YELLOW) + +#Window at Ground Level +arcade.draw_rectangle_outline(420, 320, 125, 50, arcade.color.BLACK, 2) + +#Upper Windows +arcade.draw_rectangle_outline(420, 450, 60, 60, arcade.color.BLACK, 2) +arcade.draw_rectangle_outline(625, 450, 60, 60, arcade.color.BLACK, 2) +arcade.draw_line(390, 450, 450, 450, arcade.color.BLACK, 2) +arcade.draw_line(595, 450, 655, 450, arcade.color.BLACK, 2) + +#moon +arcade.draw_circle_filled(100, 650, 50, arcade.color.GHOST_WHITE) +arcade.draw_circle_filled(120, 650, 40, (27, 27, 27)) + +#stars +point_list = ((165, 495), (200, 760), (250, 700), (270, 650), (400, 670), + (450, 780), (490, 620), (510, 730), (525, 700), (560, 630), + (600, 760), (610, 690), (650, 650), (690, 730), (700, 700), + (724, 665), (740, 615), (770, 720), (775, 705), (180, 770)) +arcade.draw_points(point_list, arcade.color.GHOST_WHITE, 2) + +# Finish drawing +arcade.finish_render() + +# Keep the window up until someone closes it. +arcade.run() \ No newline at end of file diff --git a/Lab 03 - Draw Using Functions/lab_03.py b/Lab 03 - Draw Using Functions/lab_03.py index e69de29bb..7fd9b3603 100644 --- a/Lab 03 - Draw Using Functions/lab_03.py +++ b/Lab 03 - Draw Using Functions/lab_03.py @@ -0,0 +1,84 @@ +import arcade +import random +from arcade.examples.sprite_explosion_particles import PARTICLE_FADE_RATE + +Screen_Width = 1000 +Screen_Height = 800 +star_coordinate_list = [] + +def draw_light_from_house(): + arcade.draw_ellipse_filled(625, 320, 28, 48, arcade.color.DARK_YELLOW) + arcade.draw_rectangle_filled(420, 320, 123, 48, arcade.color.DARK_YELLOW) + +def draw_upper_windows(x, y): + arcade.draw_rectangle_filled(x, y, 58, 58, arcade.color.DARK_YELLOW) + arcade.draw_rectangle_outline(x, y, 60, 60, arcade.color.BLACK, 2) + arcade.draw_line(x - 30, y, x + 30, y, arcade.color.BLACK, 2) + arcade.draw_line(x, y - 30, x, y + 30, arcade.color.BLACK, 2) + +def draw_house(): + arcade.draw_lrtb_rectangle_filled(300, 700, 500, 250, arcade.color.JET) + arcade.draw_lrtb_rectangle_filled(600, 650, 625, 500, (20, 20, 20)) + arcade.draw_triangle_filled(300, 500, 500, 600, 700, 500, (43, 43, 43)) + arcade.draw_lrtb_rectangle_filled(600, 650, 350, 250, arcade.color.DARK_BROWN) + arcade.draw_circle_filled(610, 290, 3, arcade.color.BLACK) + arcade.draw_ellipse_outline(625, 320, 30, 50, arcade.color.BLACK) + arcade.draw_rectangle_outline(420, 320, 125, 50, arcade.color.BLACK, 2) + +def stars(point_list): + arcade.draw_points(point_list, arcade.color.GHOST_WHITE, 2) + +def moon(x, y): + arcade.draw_circle_filled(x, y, 50, arcade.color.GHOST_WHITE) + arcade.draw_circle_filled(x + 20, y, 40, (27, 27, 27)) + +def ground(): + arcade.draw_lrtb_rectangle_filled(0, 999, 300, 0, (26, 36, 33)) + +def tree(x, y): + arcade.draw_rectangle_filled(x, y, 30, 60, (69, 41, 19)) + arcade.draw_triangle_filled(x - 30, y + 30, x, y + 70, x + 30, y + 30, arcade.color.DARK_GREEN) + arcade.draw_triangle_filled(x - 30, y + 60, x, y + 100, x + 30, y + 60, arcade.color.DARK_GREEN) + arcade.draw_triangle_filled(x - 30, y + 90, x, y + 130, x + 30, y + 90, arcade.color.DARK_GREEN) + +def main(): + arcade.open_window(Screen_Width, Screen_Height, "landscape") + + # background color + arcade.set_background_color((27, 27, 27)) + + arcade.start_render() + + # Start Drawing + ground() + + for x in range(0, 100): + x_stars = random.randint(0, 1000) + y_stars = random.randint(400, 800) + x_and_y_object = (x_stars, y_stars) + star_coordinate_list.append(x_and_y_object) + + stars(star_coordinate_list) + + # House + draw_house() + + draw_light_from_house() + draw_upper_windows(390, 450) + draw_upper_windows(505, 450) + draw_upper_windows(620, 450) + moon(100, 650) + + tree(50, 290) + tree(120, 290) + tree(850, 290) + tree(920, 290) + + # Finish drawing + arcade.finish_render() + + # Keep the window up until someone closes it. + arcade.run() + +# calling the main function +main() diff --git a/Lab 04 - Camel/lab_04.py b/Lab 04 - Camel/lab_04.py index e69de29bb..20431e20b 100644 --- a/Lab 04 - Camel/lab_04.py +++ b/Lab 04 - Camel/lab_04.py @@ -0,0 +1,180 @@ +# import statements +import random + + +# natives move up function +def natives_move_up(): return random.randrange(0, 14) + +# full speed function +def full_speed(): return random.randrange(10, 20) + +# moderate speed function +def moderate_speed(): return random.randrange(5, 12) + +# camel tiredness function +def camel_tiredness_function(): return random.randrange(1, 3) + +# oasis chance function +def oasis_chance(): return random.randrange(1, 20) + +# defining main function +def main(): + # variables + done = False + miles_traveled = 190 + thirst = 0 + camel_tiredness = 0 + natives_traveled = -20 + canteen = 3 + oasis = random.randrange(1, 20) + + # Initial statement + print("Welcome to Camel!") + print("You have stolen a camel to make your way across the great Mobi desert.") + print("The natives want their camel back and are chasing you down! Survive your") + print("desert trek and out run the natives.") + print("") + + # starting while loop + while not done: + print("A. Drink from your canteen.") + print("B. Ahead moderate speed.") + print("C. Ahead full speed.") + print("D. Stop for the night.") + print("E. Status check.") + print("Q. Quit.") + print("") + user_choice = input("What is your choice? ") + print("") + + # quit statement + if user_choice.upper() == "Q": + print("Y: Yes") + print("N: No") + print("") + user_quit_choice = input("What is your choice? ") + + # quit check + if user_quit_choice.upper() == "Y": + print("You have exited the game") + done = True + + # status check + elif user_choice.upper() == "E": + print("Miles traveled:", miles_traveled) + print("Drinks left in canteen:", canteen) + print("The natives are", miles_traveled - natives_traveled, "miles behind you!") + print("") + + # stop for the night + elif user_choice.upper() == "D": + camel_tiredness = 0 + natives_move = natives_move_up() + natives_traveled = natives_traveled + natives_move + print("The camel is happy! The natives move up", natives_move, "miles.") + print("") + + # full speed + elif user_choice.upper() == "C": + full_speed_user = full_speed() + full_speed_natives = natives_move_up() + miles_traveled = miles_traveled + full_speed_user + natives_traveled = natives_traveled + full_speed_natives + camel_tiredness = camel_tiredness + camel_tiredness_function() + thirst = thirst + 1 + full_speed_oasis = oasis_chance() + print("You have traveled", full_speed_user, "miles.") + print("Camel tiredness:", camel_tiredness) + print("The natives move up", full_speed_natives, "miles.") + if oasis == full_speed_oasis: + canteen = 3 + thirst = 0 + camel_tiredness = 0 + print("You found an oasis!") + print("Drinks left in canteen:", canteen) + print("Thirst:", thirst) + print("Camel tiredness:", camel_tiredness) + print("") + else: + print("") + + # moderate speed + elif user_choice.upper() == "B": + moderate_speed_user = moderate_speed() + moderate_speed_natives = natives_move_up() + miles_traveled = miles_traveled + moderate_speed_user + natives_traveled = natives_traveled + moderate_speed_natives + camel_tiredness = camel_tiredness + 1 + thirst = thirst + 1 + moderate_speed_oasis = oasis_chance() + print("You have traveled", moderate_speed_user, "miles.") + print("Camel tiredness:", camel_tiredness) + print("The natives move up", moderate_speed_natives, "miles.") + if oasis == moderate_speed_oasis: + canteen = 3 + thirst = 0 + camel_tiredness = 0 + print("You found an oasis!") + print("Drinks left in canteen:", canteen) + print("Thirst:", thirst) + print("Camel tiredness:", camel_tiredness) + print("") + else: + print("") + + # Drink from canteen + elif user_choice.upper() == "A": + if canteen != 0: + print("You take a drink from your canteen.") + canteen = canteen - 1 + thirst = 0 + print("") + else: + print("You have no more water!") + print("") + + # you are thirsty + if 4 <= thirst < 6: + print("You are thirsty!") + print("") + + # you died of thirst + elif thirst >= 6: + print("You died of thirsty!") + print("") + done = True + + if not done: + # your camel is getting tired + if 5 <= camel_tiredness < 8: + print("Your camel is getting tired.") + print("") + + # your camel is dead + elif camel_tiredness >= 8: + print("Your camel is dead.") + print("") + done = True + + if not done: + # if the natives have caught up + if natives_traveled >= miles_traveled: + print("The natives have caught you.") + print("") + done = True + + # the natives are getting close + elif natives_traveled >= miles_traveled - 10: + print("The natives are getting close!") + print("") + + if not done: + # winning statement + if miles_traveled >= 200: + print("You won and got the camel across the desert! Congrats on being alive!") + print("") + done = True + + +# calling the main function +main() diff --git a/Lab 05 - Loopy Lab/lab_05.py b/Lab 05 - Loopy Lab/lab_05.py index e69de29bb..136fdc677 100644 --- a/Lab 05 - Loopy Lab/lab_05.py +++ b/Lab 05 - Loopy Lab/lab_05.py @@ -0,0 +1,116 @@ +import arcade + + +def draw_section_outlines(): + # Draw squares on bottom + arcade.draw_rectangle_outline(150, 150, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(450, 150, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(750, 150, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(1050, 150, 300, 300, arcade.color.BLACK) + + # Draw squares on top + arcade.draw_rectangle_outline(150, 450, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(450, 450, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(750, 450, 300, 300, arcade.color.BLACK) + arcade.draw_rectangle_outline(1050, 450, 300, 300, arcade.color.BLACK) + + +def draw_section_1(): + for row in range(30): + for column in range(30): + x = (row * 10) + 5 + y = (column * 10) + 5 + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_2(): + for row in range(30): + for column in range(30): + x = (row * 10) + 305 + y = (column * 10) + 5 + if (x - 5) % 20: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.BLACK) + else: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_3(): + for row in range(30): + for column in range(30): + x = (row * 10) + 605 + y = (column * 10) + 5 + if (y - 5) % 20: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.BLACK) + else: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_4(): + for row in range(30): + for column in range(30): + x = (row * 10) + 905 + y = (column * 10) + 5 + if (y - 5) % 20 | (x - 5) % 20: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.BLACK) + else: + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_5(): + for row in range(30): + for column in range(row): + x = (row * 10) + 5 + y = (column * 10) + 305 + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_6(): + for row in range(30): + for column in range(30 - row): + x = (row * 10) + 305 + y = (column * 10) + 305 + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_7(): + for column in range(30): + for row in range(column + 1): + x = (row * 10) + 605 + y = (column * 10) + 305 + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def draw_section_8(): + for column in range(32): + for row in range(32 - column): + x = (row * -10) + 1205 + y = (column * -10) + 605 + arcade.draw_rectangle_filled(x, y, 5, 5, arcade.color.WHITE) + + +def main(): + # Create a window + arcade.open_window(1200, 600, "Lab 05 - Loopy Lab") + arcade.set_background_color(arcade.color.AIR_FORCE_BLUE) + + arcade.start_render() + + # Draw the outlines for the sections + draw_section_outlines() + + # Draw the sections + draw_section_1() + draw_section_2() + draw_section_3() + draw_section_4() + draw_section_5() + draw_section_6() + draw_section_7() + draw_section_8() + + arcade.finish_render() + + arcade.run() + + +main() diff --git a/Lab 06 - Text Adventure/lab_06.py b/Lab 06 - Text Adventure/lab_06.py index e69de29bb..2451ea8af 100644 --- a/Lab 06 - Text Adventure/lab_06.py +++ b/Lab 06 - Text Adventure/lab_06.py @@ -0,0 +1,278 @@ +# Define a class called room and adding constructor function +class Room: + def __init__(self, description, north, south, east, west): + self.description = str(description) + self.north = north + self.south = south + self.east = east + self.west = west + + +# Defining the main function +def main(): + # Creating an empty array + room_list = [] + done = False + + # Creating rooms with attributes + room0 = Room( + "It's dark. The only light is a candle on a desk next to a bed. To your east is a door. All other sides are " + "walls.", + None, None, 1, None) + room1 = Room( + "You have entered a long hallway with a chandelier above you. To your west you have a door to the dimly lit " + "bedroom. \nTo your east is what looks to be the dining room. The hallway continues north. There is an exit " + "to the south through a window.", + 4, None, 2, 0) + room2 = Room( + "You are in a dining room with a magnificent wooden table set for 12. You may go west to go back to the " + "hallway or \nnorth to go into the kitchen.", + 5, None, None, 1) + room3 = Room( + "You are in a brightly lit bedroom with two beds. There is a window to the north and west. To the south is" + " a wall. \nYou have a wooden door to the east.", + None, None, 4, None) + room4 = Room( + "You have entered the north side of the hallway. To the south is the rest of the hallway with a beautiful " + "chandelier. \nThere is a brightly lit bedroom to the west, a door to what seems to be the kitchen to your " + "east and a set of double \ndoors to the north.", + 6, 1, 5, 3) + room5 = Room( + "You are in the kitchen and its Hot! Someone must be cooking up a feast but no one is around. A door to the " + "south seems to \nlead you to the dining room. A door to the west leads back to the hallway.", + None, 2, None, 4) + room6 = Room( + "You are on a balcony overlooking a beautiful garden. There seems to be no way down. To your South is a set of" + "\ndouble doors back into the house.", + None, 4, None, None) + + # Append rooms to room_list + room_list.append(room0) + room_list.append(room1) + room_list.append(room2) + room_list.append(room3) + room_list.append(room4) + room_list.append(room5) + room_list.append(room6) + + # Set starting room + current_room = room_list[0] + + # Create while loop to loop through the game until player is done + while not done: + # room 1 start + if current_room == room_list[0]: + print() + print(room_list[0].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 1 end + + # room 2 start + if current_room == room_list[1]: + print() + print(room_list[1].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + print() + user_choice = input("Are you sure you want to exit through the window?") + if user_choice.lower() == "y" or user_choice.lower() == "yes": + print("You jumped out of the window... goodbye!") + done = True + else: + print("You returned inside.") + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 2 end + + # room 3 start + if current_room == room_list[2]: + print() + print(room_list[2].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 3 end + + # room 4 start + if current_room == room_list[3]: + print() + print(room_list[3].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 4 end + + # room 5 start + if current_room == room_list[4]: + print() + print(room_list[4].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 5 end + + # room 6 start + if current_room == room_list[5]: + print() + print(room_list[5].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 6 end + + # room 7 start + if current_room == room_list[6]: + print() + print(room_list[6].description) + print("You may say \"go\" with a direction to change areas.") + user_choice = input("Which way would you like to go? ") + if user_choice.lower() == "go n" or user_choice.lower() == "go north": + if current_room.north is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.north] + elif user_choice.lower() == "go s" or user_choice.lower() == "go south": + if current_room.south is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.south] + elif user_choice.lower() == "go e" or user_choice.lower() == "go east": + if current_room.east is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.east] + elif user_choice.lower() == "go w" or user_choice.lower() == "go west": + if current_room.west is None: + print("You cant go that way.") + else: + current_room = room_list[current_room.west] + else: + print("I do not understand that command, please try again.") + # room 7 end + + +# initiate main function and start the program +main() diff --git a/Lab 07 - User Control/lab_07.py b/Lab 07 - User Control/lab_07.py index e69de29bb..8e1fe9554 100644 --- a/Lab 07 - User Control/lab_07.py +++ b/Lab 07 - User Control/lab_07.py @@ -0,0 +1,224 @@ +""" Lab 7 - User Control """ +import arcade + +# --- Constants --- +SCREEN_WIDTH = 1000 +SCREEN_HEIGHT = 800 +# faster gives cool ghost like graphics behind the models +MOVEMENT_SPEED = 4 + + +# draw light for the windows function +def draw_light_from_house(): + arcade.draw_ellipse_filled(625, 320, 28, 48, arcade.color.DARK_YELLOW) + arcade.draw_rectangle_filled(420, 320, 123, 48, arcade.color.DARK_YELLOW) + + +# draw upper windows function +def draw_upper_windows(x, y): + arcade.draw_rectangle_filled(x, y, 58, 58, arcade.color.DARK_YELLOW) + arcade.draw_rectangle_outline(x, y, 60, 60, arcade.color.BLACK, 2) + arcade.draw_line(x - 30, y, x + 30, y, arcade.color.BLACK, 2) + arcade.draw_line(x, y - 30, x, y + 30, arcade.color.BLACK, 2) + + +# draw house function +def draw_house(): + arcade.draw_lrtb_rectangle_filled(300, 700, 500, 250, arcade.color.JET) + arcade.draw_lrtb_rectangle_filled(600, 650, 625, 500, (20, 20, 20)) + arcade.draw_triangle_filled(300, 500, 500, 600, 700, 500, (43, 43, 43)) + arcade.draw_lrtb_rectangle_filled(600, 650, 350, 250, arcade.color.DARK_BROWN) + arcade.draw_circle_filled(610, 290, 3, arcade.color.BLACK) + arcade.draw_ellipse_outline(625, 320, 30, 50, arcade.color.BLACK) + arcade.draw_rectangle_outline(420, 320, 125, 50, arcade.color.BLACK, 2) + + +# draw moon function +def moon(x, y): + arcade.draw_circle_filled(x, y, 50, arcade.color.GHOST_WHITE) + arcade.draw_circle_filled(x + 20, y, 40, (27, 27, 27)) + + +# draw ground function +def ground(): + arcade.draw_lrtb_rectangle_filled(0, 999, 300, 0, (26, 36, 33)) + + +# draw trees function +def tree(x, y): + arcade.draw_rectangle_filled(x, y, 30, 60, (69, 41, 19)) + arcade.draw_triangle_filled(x - 30, y + 30, x, y + 70, x + 30, y + 30, arcade.color.DARK_GREEN) + arcade.draw_triangle_filled(x - 30, y + 60, x, y + 100, x + 30, y + 60, arcade.color.DARK_GREEN) + arcade.draw_triangle_filled(x - 30, y + 90, x, y + 130, x + 30, y + 90, arcade.color.DARK_GREEN) + + +class PacMan: + def __init__(self, position_x, position_y, change_x, change_y, color): + # Take the parameters of the init function above, + # and create instance variables out of them. + self.position_x = position_x + self.position_y = position_y + self.change_x = change_x + self.change_y = change_y + self.color = color + + def draw(self): + # Draw the pacman. + arcade.draw_circle_filled(self.position_x, self.position_y, 25, self.color) + arcade.draw_triangle_filled(self.position_x, self.position_y, self.position_x + 20, self.position_y + 18, + self.position_x + 20, self.position_y - 18, arcade.color.BLACK) + arcade.draw_triangle_filled(self.position_x + 27, self.position_y, self.position_x + 20, self.position_y + 18, + self.position_x + 20, self.position_y - 18, arcade.color.BLACK) + arcade.draw_circle_filled(self.position_x + 5, self.position_y + 17, 3, arcade.color.BLACK) + + def update(self): + # Move the pacman + self.position_y += self.change_y + self.position_x += self.change_x + + +class Ghost: + def __init__(self, position_x, position_y, change_x, change_y, color): + # Take the parameters of the init function above, + # and create instance variables out of them. + self.position_x = position_x + self.position_y = position_y + self.change_x = change_x + self.change_y = change_y + self.color = color + + # edge sound + self.edge_sound = arcade.load_sound(":resources:sounds/hit4.wav") + + def draw(self): + # Draw the Ghost. + arcade.draw_rectangle_filled(self.position_x, self.position_y, 30, 30, self.color) + arcade.draw_circle_filled(self.position_x + 5, self.position_y + 5, 2, arcade.color.BLACK) + arcade.draw_circle_filled(self.position_x - 5, self.position_y + 5, 2, arcade.color.BLACK) + + def update(self): + # Move the Ghost + self.position_y += self.change_y + self.position_x += self.change_x + + # edge control, so we don't fly off the screen as well as edge sounds... the more the merrier. + if self.position_x < 15: + arcade.play_sound(self.edge_sound) + self.position_x = 15 + + if self.position_x > SCREEN_WIDTH - 15: + arcade.play_sound(self.edge_sound) + self.position_x = SCREEN_WIDTH - 15 + + if self.position_y < 15: + arcade.play_sound(self.edge_sound) + self.position_y = 15 + + if self.position_y > SCREEN_HEIGHT - 15: + arcade.play_sound(self.edge_sound) + self.position_y = SCREEN_HEIGHT - 15 + + +class MyGame(arcade.Window): + """ Our Custom Window Class""" + + def __init__(self): + """ Initializer """ + + # Call the parent class initializer + super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Lab 7 - User Control - Ghost") + + # Make the mouse disappear when it is over the window. + # So we just see our object, not the pointer. + self.set_mouse_visible(False) + + # Create our Ghosts + self.ghost1 = Ghost(200, 200, 0, 0, arcade.color.LIGHT_BLUE) + self.pacman = PacMan(400, 200, 0, 0, arcade.color.YELLOW) + + # click sound for the mouse + self.click_sound = arcade.load_sound(":resources:sounds/laser5.wav") + + # background color for the program + arcade.set_background_color((27, 27, 27)) + + # draw all the stuff! + def on_draw(self): + arcade.start_render() + + # Start Drawing + # draw ground + ground() + + # draw House + draw_house() + + # draw light + draw_light_from_house() + + # draw windows + draw_upper_windows(390, 450) + draw_upper_windows(505, 450) + draw_upper_windows(620, 450) + + # draw moon + moon(100, 650) + + # draw trees + tree(50, 290) + tree(120, 290) + tree(850, 290) + tree(920, 290) + + # draw the mobile characters + self.ghost1.draw() + self.pacman.draw() + + # update the characters + def update(self, delta_time): + self.ghost1.update() + self.pacman.update() + + # on keypress move the ghost + def on_key_press(self, key, modifiers): + """ Called whenever the user presses a key. """ + if key == arcade.key.LEFT: + self.ghost1.change_x = -MOVEMENT_SPEED + elif key == arcade.key.RIGHT: + self.ghost1.change_x = MOVEMENT_SPEED + elif key == arcade.key.UP: + self.ghost1.change_y = MOVEMENT_SPEED + elif key == arcade.key.DOWN: + self.ghost1.change_y = -MOVEMENT_SPEED + + # on key release stop moving the ghost + def on_key_release(self, key, modifiers): + """ Called whenever a user releases a key. """ + if key == arcade.key.LEFT or key == arcade.key.RIGHT: + self.ghost1.change_x = 0 + elif key == arcade.key.UP or key == arcade.key.DOWN: + self.ghost1.change_y = 0 + + # mouse controls for the pacman + def on_mouse_motion(self, x, y, dx, dy): + """ Called to update our objects. + Happens approximately 60 times per second.""" + self.pacman.position_x = x + self.pacman.position_y = y + + # mouse click controls to enact sound effects + def on_mouse_press(self, x, y, button, modifiers): + if button == arcade.MOUSE_BUTTON_LEFT: + arcade.play_sound(self.click_sound) + elif button == arcade.MOUSE_BUTTON_RIGHT: + arcade.play_sound(self.click_sound) + + +# throw everything into main to get called to start the program +def main(): + MyGame() + arcade.run() + + +# start the program +main() diff --git a/Lab 08 - Sprites/lab_08.py b/Lab 08 - Sprites/lab_08.py index e69de29bb..25a075517 100644 --- a/Lab 08 - Sprites/lab_08.py +++ b/Lab 08 - Sprites/lab_08.py @@ -0,0 +1,214 @@ +""" Sprite Sample Program """ + +import random +import arcade + +# --- Constants --- +SPRITE_SCALING_PLAYER = 0.5 +SPRITE_SCALING_BAD_OBJ = 0.3 +SPRITE_SCALING_GOOD_OBJ = 0.2 +COIN_COUNT = 50 +ROCK_COUNT = 20 + +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 + + +# Good Sprite class +class GoodSprite(arcade.Sprite): + def reset_pos(self): + + # Reset the coin to a random spot above the screen + self.center_y = random.randrange(SCREEN_HEIGHT + 20, + SCREEN_HEIGHT + 100) + self.center_x = random.randrange(SCREEN_WIDTH) + + def update(self): + + # Move the coin + self.center_y -= 1 + + # See if the coin has fallen off the bottom of the screen. + # If so, reset it. + if self.top < 0: + self.reset_pos() + + self.angle += 1 + + # If we rotate past 360, reset it back a turn. + if self.angle > 359: + self.angle -= 360 + + +# Bad Sprite class +class BadSprite(arcade.Sprite): + def __init__(self, filename, sprite_scaling): + + super().__init__(filename, sprite_scaling) + + self.change_x = 0 + self.change_y = 0 + + def update(self): + + # Move the coin + self.center_x += self.change_x + self.center_y += self.change_y + + # If we are out-of-bounds, then 'bounce' + if self.left < 0: + self.change_x *= -1 + + if self.right > SCREEN_WIDTH: + self.change_x *= -1 + + if self.bottom < 0: + self.change_y *= -1 + + if self.top > SCREEN_HEIGHT: + self.change_y *= -1 + + +class MyGame(arcade.Window): + """ Our custom Window Class""" + + def __init__(self): + """ Initializer """ + # Call the parent class initializer + super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Sprite Example") + + # good sprite sound + self.goodSpriteSound = arcade.load_sound(":resources:sounds/coin3.wav") + + # bad sprite sound + self.badSpriteSound = arcade.load_sound(":resources:sounds/gameover5.wav") + + # Variables that will hold sprite lists + self.player_list = None + self.coin_list = None + self.bad_sprite_list = None + + # Set up the player info + self.player_sprite = None + self.score = 0 + + # Don't show the mouse cursor + self.set_mouse_visible(False) + + arcade.set_background_color(arcade.color.BLACK) + + def setup(self): + """ Set up the game and initialize the variables. """ + + # Sprite lists + self.player_list = arcade.SpriteList() + self.coin_list = arcade.SpriteList() + self.bad_sprite_list = arcade.SpriteList() + + # Score + self.score = 0 + + # Set up the player + self.player_sprite = arcade.Sprite(":resources:images/animated_characters/male_person/malePerson_jump.png", + SPRITE_SCALING_PLAYER) + self.player_sprite.center_x = 50 + self.player_sprite.center_y = 50 + self.player_list.append(self.player_sprite) + + # Create the coins // good sprites + for i in range(COIN_COUNT): + # Create the coin instance + coin = GoodSprite(":resources:images/items/coinGold.png", SPRITE_SCALING_GOOD_OBJ) + + # Position the coin + coin.center_x = random.randrange(SCREEN_WIDTH) + coin.center_y = random.randrange(SCREEN_HEIGHT) + + # Add the coin to the lists + self.coin_list.append(coin) + + # Create the rocks // Bad sprites + for i in range(ROCK_COUNT): + # Create the rock instance + rock = BadSprite(":resources:images/space_shooter/meteorGrey_med1.png", SPRITE_SCALING_BAD_OBJ) + + # Position the rock + rock.center_x = random.randrange(SCREEN_WIDTH) + rock.center_y = random.randrange(SCREEN_HEIGHT) + rock.change_x = random.randrange(-3, 4) + rock.change_y = random.randrange(-3, 4) + + # Add the coin to the lists + self.bad_sprite_list.append(rock) + + def on_draw(self): + """ Draw everything """ + arcade.start_render() + self.coin_list.draw() + self.player_list.draw() + self.bad_sprite_list.draw() + + # Put the text on the screen. + output = f"Score: {self.score}" + arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14) + + if len(self.coin_list) == 0: + # Draw Game Over + arcade.draw_text("GAME OVER", 325, 350, arcade.color.WHITE, 20) + # End Game Sound + + def on_mouse_motion(self, x, y, dx, dy): + """ Handle Mouse Motion """ + + # Move the center of the player sprite to match the mouse x, y + self.player_sprite.center_x = x + self.player_sprite.center_y = y + + # Freeze Player + if len(self.coin_list) == 0: + self.player_sprite.center_x = 400 + self.player_sprite.center_y = 300 + + def update(self, delta_time): + """ Movement and game logic """ + + # Call update on all sprites (The sprites don't do much in this + # example though.) + self.coin_list.update() + self.bad_sprite_list.update() + + # Generate a list of all good sprites that collided with the player. + coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, + self.coin_list) + + # Generate a list of all bad sprites that collided with the player. + bad_sprite_hit_list = arcade.check_for_collision_with_list(self.player_sprite, + self.bad_sprite_list) + + # Loop through each colliding sprite, remove it, and add to the score. + # Good sprite loop + for coin in coins_hit_list: + arcade.play_sound(self.goodSpriteSound) + coin.remove_from_sprite_lists() + self.score += 10 + + # Bad sprite loop + for rock in bad_sprite_hit_list: + arcade.play_sound(self.badSpriteSound) + rock.remove_from_sprite_lists() + self.score -= 25 + + if len(self.coin_list) == 0: + for rock in self.bad_sprite_list: + rock.remove_from_sprite_lists() + + +def main(): + """ Main method """ + window = MyGame() + window.setup() + arcade.run() + + +if __name__ == "__main__": + main() diff --git a/Lab 09 - Sprites and Walls/lab_09.py b/Lab 09 - Sprites and Walls/lab_09.py index e69de29bb..295471e6b 100644 --- a/Lab 09 - Sprites and Walls/lab_09.py +++ b/Lab 09 - Sprites and Walls/lab_09.py @@ -0,0 +1,250 @@ +import random +import arcade +from pyglet.math import Vec2 + +SPRITE_SCALING = 0.5 +COIN_SCALING = 0.3 + +DEFAULT_SCREEN_WIDTH = 800 +DEFAULT_SCREEN_HEIGHT = 600 +SCREEN_TITLE = "SPRITE LOCKED IN A BOX" + +# coin number / anymore, and it will crash system :( +Number_Of_Coins = 100 + +# How many pixels to keep as a minimum margin between the character +# and the edge of the screen. +VIEWPORT_MARGIN = 150 + +# How fast the camera pans to the player. 1.0 is instant. +CAMERA_SPEED = 0.3 + +# How fast the character moves +PLAYER_MOVEMENT_SPEED = 7 + + +class MyGame(arcade.Window): + """ Main application class. """ + + def __init__(self, width, height, title): + """ + Initializer + """ + super().__init__(width, height, title, resizable=True) + + # good sprite sound + self.goodSpriteSound = arcade.load_sound(":resources:sounds/coin3.wav") + + # Sprite lists + self.score = None + self.player_list = None + self.wall_list = None + self.coin_list = None + + # Set up the player + self.player_sprite = None + + # Physics engine so we don't run into walls. + self.physics_engine = None + + # Track the current state of what key is pressed + self.left_pressed = False + self.right_pressed = False + self.up_pressed = False + self.down_pressed = False + + # Create the cameras. One for the GUI, one for the sprites. + # We scroll the 'sprite world' but not the GUI. + self.camera_sprites = arcade.Camera(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT) + self.camera_gui = arcade.Camera(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT) + + def setup(self): + """ Set up the game and initialize the variables. """ + + # Sprite lists + self.player_list = arcade.SpriteList() + self.wall_list = arcade.SpriteList() + self.coin_list = arcade.SpriteList() + + # Set up the player + self.player_sprite = arcade.Sprite(":resources:images/animated_characters/male_adventurer" + "/maleAdventurer_walk0.png", + scale=0.4) + self.player_sprite.center_x = 256 + self.player_sprite.center_y = 512 + self.player_list.append(self.player_sprite) + + # score + self.score = 0 + + # -- Set up the walls + # set up the outer border + # y loops the bottom and the top of the area we want to block in + for y in (0, 1536): + # loop through each box across + for x in range(0, 1600, 64): + wall = arcade.Sprite(":resources:images/tiles/stoneCenter.png", SPRITE_SCALING) + wall.left = x + wall.bottom = y + self.wall_list.append(wall) + + # create the left and right limits + for x in (0, 1536): + # loop each box + for y in range(64, 1600, 64): + wall = arcade.Sprite(":resources:images/tiles/stoneCenter.png", SPRITE_SCALING) + wall.left = x + wall.bottom = y + self.wall_list.append(wall) + + # set up random inside boxes + for x in range(200, 1536, 210): + for y in range(200, 1450, 64): + if random.randrange(3) > 0: + wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING) + wall.center_x = x + wall.center_y = y + self.wall_list.append(wall) + + # set up coins + # make sure they aren't in the walls / boxes + # very effective but inefficient / keeps crashing computer with to many coins due to complexity + for i in range(Number_Of_Coins): + # Coin instance / prob should have been less generic + coin_placed_successfully = False + coin = arcade.Sprite(":resources:images/items/coinGold.png", COIN_SCALING) + + # while loop to check and see if coin is not in box / wall and keep trying until successful + while not coin_placed_successfully: + # Position the coin + coin.center_x = random.randrange(1600) + coin.center_y = random.randrange(1600) + + # See if the coin is hitting a wall + wall_hit_list = arcade.check_for_collision_with_list(coin, self.wall_list) + + # See if the coin is hitting another coin / didn't think of this on my own but super smart + coin_hit_list = arcade.check_for_collision_with_list(coin, self.coin_list) + + # winner winner chicken dinner! + if len(wall_hit_list) == 0 and len(coin_hit_list) == 0: + # It is! + coin_placed_successfully = True + + # Add the coin to the lists + self.coin_list.append(coin) + + self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list) + + # Set the background color + arcade.set_background_color(arcade.color.DARK_GREEN) + + def on_draw(self): + """ Render the screen. """ + + # This command has to happen before we start drawing + self.clear() + + # Select the camera we'll use to draw all our sprites + self.camera_sprites.use() + + # Draw all the sprites. + self.wall_list.draw() + self.player_list.draw() + self.coin_list.draw() + + # Select the (un-scrolled) camera for our GUI + self.camera_gui.use() + # draw the score with the un-scrolled camera + arcade.draw_text(f"Score: {self.score}", 10, 10, arcade.color.WHITE, 24) + + def on_key_press(self, key, modifiers): + """Called whenever a key is pressed. """ + + if key == arcade.key.UP: + self.up_pressed = True + elif key == arcade.key.DOWN: + self.down_pressed = True + elif key == arcade.key.LEFT: + self.left_pressed = True + elif key == arcade.key.RIGHT: + self.right_pressed = True + + def on_key_release(self, key, modifiers): + """Called when the user releases a key. """ + + if key == arcade.key.UP: + self.up_pressed = False + elif key == arcade.key.DOWN: + self.down_pressed = False + elif key == arcade.key.LEFT: + self.left_pressed = False + elif key == arcade.key.RIGHT: + self.right_pressed = False + + def on_update(self, delta_time): + """ Movement and game logic """ + self.coin_list.update() + + # Calculate speed based on the keys pressed + self.player_sprite.change_x = 0 + self.player_sprite.change_y = 0 + + # Generate a list of all good sprites that collided with the player. + coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, + + self.coin_list) + # Loop through each colliding sprite, remove it, and add to the score. + # Good sprite loop + for coin in coins_hit_list: + arcade.play_sound(self.goodSpriteSound) + coin.remove_from_sprite_lists() + self.score += 10 + + if self.up_pressed and not self.down_pressed: + self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED + elif self.down_pressed and not self.up_pressed: + self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED + if self.left_pressed and not self.right_pressed: + self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED + elif self.right_pressed and not self.left_pressed: + self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED + + # Call update on all sprites (The sprites don't do much in this + # example though.) + self.physics_engine.update() + + # Scroll the screen to the player + self.scroll_to_player() + + def scroll_to_player(self): + """ + Scroll the window to the player. + + if CAMERA_SPEED is 1, the camera will immediately move to the desired position. + Anything between 0 and 1 will have the camera move to the location with a smoother + pan. + """ + + position = Vec2(self.player_sprite.center_x - self.width / 2, + self.player_sprite.center_y - self.height / 2) + self.camera_sprites.move_to(position, CAMERA_SPEED) + + def on_resize(self, width, height): + """ + Resize window + Handle the user grabbing the edge and resizing the window. + """ + self.camera_sprites.resize(int(width), int(height)) + self.camera_gui.resize(int(width), int(height)) + + +def main(): + """ Main function """ + window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE) + window.setup() + arcade.run() + + +if __name__ == "__main__": + main() diff --git a/Lab 10 - Spell Check/lab_10.py b/Lab 10 - Spell Check/lab_10.py index e69de29bb..c02207f3d 100644 --- a/Lab 10 - Spell Check/lab_10.py +++ b/Lab 10 - Spell Check/lab_10.py @@ -0,0 +1,113 @@ +import re + + +# split lines into singular words with regex +def split_line(line): + return re.findall('[A-Za-z]+(?:\'[A-Za-z]+)?', line) + + +def main(): + # Open the file, and store it in a variable + dictionary_file = open("dictionary.txt") + + # instantiate empty list to store dictionary words in + dictionary_list = [] + # for loop through dictionary file and push each word to the list + for line in dictionary_file: + # remove whitespace + line = line.strip() + dictionary_list.append(line) + + # close file + dictionary_file.close() + + # track line number + line_num = 0 + + # step 8 to know what search method we are doing + print("--- Linear Search ---") + + # open file and store in variable to iterate through + file = open("AliceInWonderLand200.txt") + + # for loop through file + for line in file: + + # increase line number + line_num += 1 + + # strip whitespace off line variable + line = line.strip() + + # call split_line function to separate the words apart + word_list = split_line(line) + + # for loop through words in the line + for word in word_list: + + # beginning of the list + current_list_position = 0 + + # upper case word to compare + upperWord = word.upper() + + # loop through entire list until you find the word + while current_list_position < len(dictionary_list) and dictionary_list[current_list_position] != upperWord: + # go to next list variable + current_list_position += 1 + + # if we cant find the word in the dictionary then print the line and misspelled word + if upperWord not in dictionary_list: + print("line", line_num, "possible misspelled word:", word) + + dictionary_file.close() + + # print out what search method we are using + print("") + print("--- Binary Search ---") + + # open file and store in variable to iterate through + file = open("AliceInWonderLand200.txt") + + # reset line_num before looping through new file + line_num = 0 + + # for loop through file + for line in file: + + # increase line number + line_num += 1 + + # strip whitespace off line variable + line = line.strip() + + # call split_line function to separate the words apart + word_list = split_line(line) + + # for loop through words in the line + for word in word_list: + + # upper case word to compare + upperWord = word.upper() + + lower_bound = 0 + upper_bound = len(dictionary_list) - 1 + while lower_bound <= upper_bound: + middle_pos = (lower_bound + upper_bound) // 2 + if dictionary_list[middle_pos] == upperWord: + break + elif dictionary_list[middle_pos] < upperWord: + lower_bound = middle_pos + 1 + else: + upper_bound = middle_pos - 1 + else: + + # if we cant find the word in the dictionary then print the line and misspelled word + print("line", line_num, "possible misspelled word:", word) + + # close out file + file.close() + + +# call the main function to fire it all off +main() diff --git a/Lab 12 - Final Lab/Constants.py b/Lab 12 - Final Lab/Constants.py new file mode 100644 index 000000000..d5c49769f --- /dev/null +++ b/Lab 12 - Final Lab/Constants.py @@ -0,0 +1,27 @@ +# title for game +title = "Definitely Not Space Invaders" + +# scaling for png +playerScaling = 0.4 +enemyScaling = 0.4 +laserScaling = 0.6 + +# screen sizing and edge control +screenWidth = 800 +screenHeight = 600 +enemyVerticalMargin = 10 + +# enemy controls +enemySpeed = 1 +enemyMoveDownAmount = 30 + +# player controls +maxPlayerBullets = 5 + +# bullet controls +playerBulletSpeed = 3 +enemyBulletSpeed = 3 + +# game state +gameOver = 1 +playGame = 0 diff --git a/Lab 12 - Final Lab/InstructionPage.png b/Lab 12 - Final Lab/InstructionPage.png new file mode 100644 index 000000000..43564611d Binary files /dev/null and b/Lab 12 - Final Lab/InstructionPage.png differ diff --git a/Lab 12 - Final Lab/part_12.py b/Lab 12 - Final Lab/part_12.py index e69de29bb..ebd3f901c 100644 --- a/Lab 12 - Final Lab/part_12.py +++ b/Lab 12 - Final Lab/part_12.py @@ -0,0 +1,445 @@ +import random +import arcade +import Constants + +# Boilerplate code came from https://api.arcade.academy/en/latest/examples/slime_invaders.html +# This margin controls how close the enemy gets to the left or right side +# before reversing direction. +rightEnemyBorder = Constants.screenWidth - Constants.enemyVerticalMargin +leftEnemyBorder = Constants.enemyVerticalMargin + + +# title screen +class TitleView(arcade.View): + + def on_show_view(self): + # runs once we switch to this view + arcade.set_background_color((27, 27, 27)) + arcade.set_viewport(0, self.window.width, 0, self.window.height) + + def on_draw(self): + # clear out anything else like old views + self.clear() + + # Draw the view + arcade.draw_text("This is Definitely Not Space Invaders....", self.window.width / 2, self.window.height / 2, + arcade.color.WHITE, font_size=30, anchor_x="center") + arcade.draw_text("Click to Continue", self.window.width / 2, self.window.height / 2 - 75, + arcade.color.WHITE, font_size=20, anchor_x="center") + + # go to next screen through mouse click + def on_mouse_press(self, _x, _y, _button, _modifiers): + # show instructions next + self.window.show_view(InstructionView()) + + +# instruction view that's called from title view +class InstructionView(arcade.View): + + # runs once we switch to this view + def on_show_view(self): + arcade.set_viewport(0, self.window.width, 0, self.window.height) + + def on_draw(self): + # clear out anything else like old views + self.clear() + # Draw the view + instructionPage = arcade.load_texture("InstructionPage.png") + arcade.draw_lrwh_rectangle_textured(0, 0, Constants.screenWidth, Constants.screenHeight, instructionPage) + + def on_mouse_press(self, _x, _y, _button, _modifiers): + """ If the user presses the mouse button, start the game. """ + game_view = GameView() + game_view.setup() + self.window.show_view(game_view) + + +# Game view +class GameView(arcade.View): + + def __init__(self): + # Call the parent class initializer + super().__init__() + + # Variables that will hold sprite lists + self.player_list = None + self.enemy_list = None + self.player_bullet_list = None + self.enemy_bullet_list = None + self.shield_list = None + + # level variable + self.level = 1 + + # life variable + self.lives = 5 + + # Textures for the enemy + self.enemy_textures = None + + # State of the game + self.game_state = Constants.playGame + + # Set up the player info + self.player_sprite = None + self.score = 0 + + # Enemy movement + self.enemy_change_x = - Constants.enemySpeed + + # Don't show the mouse cursor + self.window.set_mouse_visible(False) + + # Load sounds. Sounds from arcade + self.gun_sound = arcade.load_sound(":resources:sounds/laser1.ogg") + self.hit_sound = arcade.load_sound(":resources:sounds/hit3.wav") + self.player_hit = arcade.load_sound(":resources:sounds/hurt1.wav") + self.lost_game = arcade.load_sound(":resources:sounds/lose2.wav") + self.level_up = arcade.load_sound(":resources:sounds/upgrade4.wav") + + def setup_levels(self): + # Load the textures for the enemies, coming to you from Kenney.nl + # enemy image from kenney.nl https://kenney.nl/assets/space-shooter-extension + texture1 = arcade.load_texture("spaceShips_001.png") + texture2 = arcade.load_texture("spaceShips_002.png") + texture3 = arcade.load_texture("spaceShips_003.png") + texture4 = arcade.load_texture("spaceShips_004.png") + texture5 = arcade.load_texture("spaceShips_005.png") + self.enemy_textures = [texture1, texture2, texture3, texture4, texture5] + + # Create rows and columns of enemies + x_count = 6 + x_start = 380 + x_spacing = 60 + y_count = 4 + y_start = 420 + y_spacing = 40 + + # randomize the enemy ships + randomEnemy = random.randint(0, 4) + + for x in range(x_start, x_spacing * x_count + x_start, x_spacing): + for y in range(y_start, y_spacing * y_count + y_start, y_spacing): + # Create the enemy instance + enemy = arcade.Sprite() + enemy.scale = Constants.enemyScaling + enemy.texture = self.enemy_textures[randomEnemy] + + # Position the enemy + enemy.center_x = x + enemy.center_y = y + + # Add the enemy to the lists + self.enemy_list.append(enemy) + + def make_shield(self, x_start): + # shield function + # just a bunch of little sprites stuck together + shield_block_width = 5 + shield_block_height = 10 + shield_width_count = 20 + shield_height_count = 5 + y_start = 150 + for x in range(x_start, + x_start + shield_width_count * shield_block_width, + shield_block_width): + for y in range(y_start, + y_start + shield_height_count * shield_block_height, + shield_block_height): + shield_sprite = arcade.SpriteSolidColor(shield_block_width, + shield_block_height, + arcade.color.RED) + shield_sprite.center_x = x + shield_sprite.center_y = y + self.shield_list.append(shield_sprite) + + def setup(self): + # Initial setup of the game and when we restart the game + self.game_state = Constants.playGame + + # Sprite lists + self.player_list = arcade.SpriteList() + self.enemy_list = arcade.SpriteList() + self.player_bullet_list = arcade.SpriteList() + self.enemy_bullet_list = arcade.SpriteList() + self.shield_list = arcade.SpriteList(is_static=True) + + # Set up the player + self.score = 0 + + # Image from kenney.nl https://kenney.nl/assets/space-shooter-extension + self.player_sprite = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", + Constants.playerScaling) + self.player_sprite.center_x = 50 + self.player_sprite.center_y = 40 + self.player_list.append(self.player_sprite) + + # Make each of the shields initially + for x in range(75, 800, 190): + self.make_shield(x) + + # Set the background color + arcade.set_background_color(arcade.color.BLACK) + + # setup the initial level + self.setup_levels() + + def on_draw(self): + # This command has to happen before we start drawing + self.clear() + + # Draw all the sprites. + self.enemy_list.draw() + self.player_bullet_list.draw() + self.enemy_bullet_list.draw() + self.shield_list.draw() + self.player_list.draw() + + # Render the score + arcade.draw_text(f"Score: {self.score}", 5, 540, arcade.color.WHITE, 14) + + # Render the players lives + arcade.draw_text(f"lives: {self.lives}", 5, 560, arcade.color.WHITE, 14) + + # Render the level + arcade.draw_text(f"Level: {self.level}", 5, 580, arcade.color.WHITE, 14) + + # Draw game over + if self.game_state == Constants.gameOver: + self.clear() + arcade.set_background_color(arcade.csscolor.BLACK) + arcade.draw_text("GAME OVER", 175, 325, arcade.color.WHITE, 55) + arcade.draw_text("Click to Restart", 300, 275, arcade.color.WHITE, 24) + self.set_mouse_visible(True) + + def on_mouse_motion(self, x, y, dx, dy): + # Don't move the player if the game is over + if self.game_state == Constants.gameOver: + return + + self.player_sprite.center_x = x + + def on_mouse_press(self, x, y, button, modifiers): + # Only allow the user so many bullets on screen at a time + if len(self.player_bullet_list) < Constants.maxPlayerBullets + (self.level * 0.4): + # Gun sound + arcade.play_sound(self.gun_sound) + + # Create a bullet + bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", Constants.laserScaling) + + # Rotate image + bullet.angle = 90 + + # Give the bullet a speed + bullet.change_y = Constants.playerBulletSpeed + (self.level * 0.1) + + # Position the bullet + bullet.center_x = self.player_sprite.center_x + bullet.bottom = self.player_sprite.top + + # Add the bullet to the appropriate lists + self.player_bullet_list.append(bullet) + + # if game is over restart game with click + if self.game_state == Constants.gameOver: + game_view = GameView() + game_view.setup() + self.window.show_view(game_view) + + def update_enemies(self): + + # Move the enemy vertically + for enemy in self.enemy_list: + enemy.center_x += self.enemy_change_x + + # Check every enemy to see if any hit the edge. If so, reverse the + # direction and flag to move down. + move_down = False + for enemy in self.enemy_list: + if enemy.right > rightEnemyBorder and self.enemy_change_x > 0: + self.enemy_change_x *= -1 + move_down = True + if enemy.left < leftEnemyBorder and self.enemy_change_x < 0: + self.enemy_change_x *= -1 + move_down = True + + # if we hit the edge then we move down + if move_down: + # Yes + for enemy in self.enemy_list: + # Move enemy down + enemy.center_y -= Constants.enemyMoveDownAmount + + def allow_enemies_to_fire(self): + # Track which x values have had a chance to fire a bullet. + # Since enemy list is build from the bottom up, we can use + # this to only allow the bottom row to fire. + x_spawn = [] + for enemy in self.enemy_list: + # Adjust the chance depending on the number of enemies. Fewer + # enemies, more likely to fire. + chance = 4 + len(self.enemy_list) * 4 + + # Fire if we roll a zero, and no one else in this column has had + # a chance to fire. + if random.randrange(chance) == 0 and enemy.center_x not in x_spawn: + # Create a bullet + bullet = arcade.Sprite(":resources:images/space_shooter/laserRed01.png", Constants.laserScaling) + + # Angle down. + bullet.angle = 180 + + # Give the bullet a speed + bullet.change_y = -Constants.enemyBulletSpeed - (self.level * 0.3) + + # Position the bullet so its top id right below the enemy + bullet.center_x = enemy.center_x + bullet.top = enemy.bottom + + # Add the bullet to the appropriate list + self.enemy_bullet_list.append(bullet) + + # Ok, this column has had a chance to fire. Add to list so we don't + # try it again this frame. + x_spawn.append(enemy.center_x) + + def process_enemy_bullets(self): + + # Move the bullets + self.enemy_bullet_list.update() + + # Loop through each bullet + for bullet in self.enemy_bullet_list: + # Check this bullet to see if it hit a shield + hit_list = arcade.check_for_collision_with_list(bullet, self.shield_list) + + # If it did, get rid of the bullet and shield blocks + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + for shield in hit_list: + shield.remove_from_sprite_lists() + continue + + # See if the player got hit with a bullet + playerHitList = arcade.check_for_collision_with_list(bullet, self.player_list) + + # if so remove a life + if len(playerHitList) > 0: + bullet.remove_from_sprite_lists() + arcade.play_sound(self.player_hit) + self.lives -= 1 + + # if lives reach zero end game + if self.lives == 0: + arcade.play_sound(self.lost_game) + self.game_state = Constants.gameOver + + # If the bullet falls off the screen get rid of it + if bullet.top < 0: + bullet.remove_from_sprite_lists() + + def processEnemyHittingObjects(self): + # Loop through each enemy + for enemy in self.enemy_list: + # Check this enemy to see if it hit a shield + shieldHitList = arcade.check_for_collision_with_list(enemy, self.shield_list) + + # If it did, get rid of the shield blocks + if len(shieldHitList) > 0: + for shield in shieldHitList: + shield.remove_from_sprite_lists() + continue + + # See if the player got hit with an enemy + playerHitList = arcade.check_for_collision_with_list(enemy, self.player_list) + + # if they collide with player + if len(playerHitList) > 0: + enemy.remove_from_sprite_lists() + arcade.play_sound(self.player_hit) + self.lives -= 1 + + if self.lives == 0: + arcade.play_sound(self.lost_game) + self.game_state = Constants.gameOver + + def process_player_bullets(self): + + # Move the bullets + self.player_bullet_list.update() + + # Loop through each bullet + for bullet in self.player_bullet_list: + + # Check this bullet to see if it hit a enemy + hit_list = arcade.check_for_collision_with_list(bullet, self.shield_list) + # If it did, get rid of the bullet + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + for shield in hit_list: + shield.remove_from_sprite_lists() + continue + + # Check this bullet to see if it hit a enemy + hit_list = arcade.check_for_collision_with_list(bullet, self.enemy_list) + + # If it did, get rid of the bullet + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + + # For every enemy we hit, add to the score and remove the enemy + for enemy in hit_list: + enemy.remove_from_sprite_lists() + self.score += 10 + + # Hit Sound + arcade.play_sound(self.hit_sound) + + # If the bullet flies off-screen, remove it. + if bullet.bottom > Constants.screenHeight: + bullet.remove_from_sprite_lists() + + def on_update(self, delta_time): + """ Movement and game logic """ + + if self.game_state == Constants.gameOver: + return + + self.update_enemies() + self.allow_enemies_to_fire() + self.process_enemy_bullets() + self.process_player_bullets() + self.processEnemyHittingObjects() + + if len(self.enemy_list) == 0: + # increase level + self.level += 1 + + # play level up sound + arcade.play_sound(self.level_up) + + # clear lasers + self.player_bullet_list = arcade.SpriteList() + self.enemy_bullet_list = arcade.SpriteList() + + # Remake shields + for x in range(75, 800, 190): + self.make_shield(x) + + self.setup_levels() + + def set_mouse_visible(self, param): + pass + + +# what is initially called to start the game +def main(): + window = arcade.Window(Constants.screenWidth, Constants.screenHeight, Constants.title) + window.show_view(TitleView()) + arcade.run() + + +# make sure main is called and not something else +if __name__ == "__main__": + main() diff --git a/Lab 12 - Final Lab/spaceShips_001.png b/Lab 12 - Final Lab/spaceShips_001.png new file mode 100644 index 000000000..cc6f240f7 Binary files /dev/null and b/Lab 12 - Final Lab/spaceShips_001.png differ diff --git a/Lab 12 - Final Lab/spaceShips_002.png b/Lab 12 - Final Lab/spaceShips_002.png new file mode 100644 index 000000000..4499e9637 Binary files /dev/null and b/Lab 12 - Final Lab/spaceShips_002.png differ diff --git a/Lab 12 - Final Lab/spaceShips_003.png b/Lab 12 - Final Lab/spaceShips_003.png new file mode 100644 index 000000000..637c5a66f Binary files /dev/null and b/Lab 12 - Final Lab/spaceShips_003.png differ diff --git a/Lab 12 - Final Lab/spaceShips_004.png b/Lab 12 - Final Lab/spaceShips_004.png new file mode 100644 index 000000000..9e9c73afe Binary files /dev/null and b/Lab 12 - Final Lab/spaceShips_004.png differ diff --git a/Lab 12 - Final Lab/spaceShips_005.png b/Lab 12 - Final Lab/spaceShips_005.png new file mode 100644 index 000000000..22b7d3c06 Binary files /dev/null and b/Lab 12 - Final Lab/spaceShips_005.png differ diff --git a/Scratch Work/drawing_samples.py b/Scratch Work/drawing_samples.py new file mode 100644 index 000000000..9a58a873c --- /dev/null +++ b/Scratch Work/drawing_samples.py @@ -0,0 +1,82 @@ +"""" +This is a sample program to show how to draw using the Python programming +language and the Arcade library. +""" + +# Import the "arcade" library +import arcade + +# Open up a window. +# From the "arcade" library, use a function called "open_window" +# Set the window title to "Drawing Example" +# Set the dimensions (width and height) +arcade.open_window(600, 600, "Drawing Example") + +# Set the background color +arcade.set_background_color(arcade.csscolor.SKY_BLUE) + +# Get ready to draw +arcade.start_render() + +# Draw a rectangle +# Left of 0, right of 599 +# Top of 300, bottom of 0 +arcade.draw_lrtb_rectangle_filled(0, 599, 300, 0, arcade.csscolor.GREEN) + +# Tree trunk +arcade.draw_rectangle_filled(100, 320, 20, 60, arcade.csscolor.SIENNA) + +# Tree top +arcade.draw_circle_filled(100, 350, 30, arcade.csscolor.DARK_GREEN) + +# Another tree, with a trunk and ellipse for top +arcade.draw_rectangle_filled(200, 320, 20, 60, arcade.csscolor.SIENNA) +arcade.draw_ellipse_filled(200, 370, 60, 80, arcade.csscolor.DARK_GREEN) + +# Another tree, with a trunk and arc for top +# Arc is centered at (300, 340) with a width of 60 and height of 100. +# The starting angle is 0, and ending angle is 180. +arcade.draw_rectangle_filled(300, 320, 20, 60, arcade.csscolor.SIENNA) +arcade.draw_arc_filled(300, 340, 60, 100, arcade.csscolor.DARK_GREEN, 0, 180) + +# Another tree, with a trunk and triangle for top +# Triangle is made of these three points: +# (400, 400), (370, 320), (430, 320) +arcade.draw_rectangle_filled(400, 320, 20, 60, arcade.csscolor.SIENNA) +arcade.draw_triangle_filled(400, 400, 370, 320, 430, 320, arcade.csscolor.DARK_GREEN) + +# Draw a tree using a polygon with a list of points +arcade.draw_rectangle_filled(500, 320, 20, 60, arcade.csscolor.SIENNA) +arcade.draw_polygon_filled(((500, 400), + (480, 360), + (470, 320), + (530, 320), + (520, 360) + ), + arcade.csscolor.DARK_GREEN) + +# Draw a sun +arcade.draw_circle_filled(500, 550, 40, arcade.color.YELLOW) + +# Rays to the left, right, up, and down +arcade.draw_line(500, 550, 400, 550, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 600, 550, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 500, 450, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 500, 650, arcade.color.YELLOW, 3) + +# Diagonal rays +arcade.draw_line(500, 550, 550, 600, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 550, 500, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 450, 600, arcade.color.YELLOW, 3) +arcade.draw_line(500, 550, 450, 500, arcade.color.YELLOW, 3) + +# Draw text at (150, 230) with a font size of 24 pts. +arcade.draw_text("Arbor Day - Plant a Tree!", + 150, 230, + arcade.color.BLACK, 24) + +# Finish drawing +arcade.finish_render() + +# Keep the window up until someone closes it. +arcade.run() \ No newline at end of file diff --git a/Testing/Constants.py b/Testing/Constants.py new file mode 100644 index 000000000..d5c49769f --- /dev/null +++ b/Testing/Constants.py @@ -0,0 +1,27 @@ +# title for game +title = "Definitely Not Space Invaders" + +# scaling for png +playerScaling = 0.4 +enemyScaling = 0.4 +laserScaling = 0.6 + +# screen sizing and edge control +screenWidth = 800 +screenHeight = 600 +enemyVerticalMargin = 10 + +# enemy controls +enemySpeed = 1 +enemyMoveDownAmount = 30 + +# player controls +maxPlayerBullets = 5 + +# bullet controls +playerBulletSpeed = 3 +enemyBulletSpeed = 3 + +# game state +gameOver = 1 +playGame = 0 diff --git a/Testing/InstructionPage.png b/Testing/InstructionPage.png new file mode 100644 index 000000000..43564611d Binary files /dev/null and b/Testing/InstructionPage.png differ diff --git a/Testing/part_12.py b/Testing/part_12.py new file mode 100644 index 000000000..ebd3f901c --- /dev/null +++ b/Testing/part_12.py @@ -0,0 +1,445 @@ +import random +import arcade +import Constants + +# Boilerplate code came from https://api.arcade.academy/en/latest/examples/slime_invaders.html +# This margin controls how close the enemy gets to the left or right side +# before reversing direction. +rightEnemyBorder = Constants.screenWidth - Constants.enemyVerticalMargin +leftEnemyBorder = Constants.enemyVerticalMargin + + +# title screen +class TitleView(arcade.View): + + def on_show_view(self): + # runs once we switch to this view + arcade.set_background_color((27, 27, 27)) + arcade.set_viewport(0, self.window.width, 0, self.window.height) + + def on_draw(self): + # clear out anything else like old views + self.clear() + + # Draw the view + arcade.draw_text("This is Definitely Not Space Invaders....", self.window.width / 2, self.window.height / 2, + arcade.color.WHITE, font_size=30, anchor_x="center") + arcade.draw_text("Click to Continue", self.window.width / 2, self.window.height / 2 - 75, + arcade.color.WHITE, font_size=20, anchor_x="center") + + # go to next screen through mouse click + def on_mouse_press(self, _x, _y, _button, _modifiers): + # show instructions next + self.window.show_view(InstructionView()) + + +# instruction view that's called from title view +class InstructionView(arcade.View): + + # runs once we switch to this view + def on_show_view(self): + arcade.set_viewport(0, self.window.width, 0, self.window.height) + + def on_draw(self): + # clear out anything else like old views + self.clear() + # Draw the view + instructionPage = arcade.load_texture("InstructionPage.png") + arcade.draw_lrwh_rectangle_textured(0, 0, Constants.screenWidth, Constants.screenHeight, instructionPage) + + def on_mouse_press(self, _x, _y, _button, _modifiers): + """ If the user presses the mouse button, start the game. """ + game_view = GameView() + game_view.setup() + self.window.show_view(game_view) + + +# Game view +class GameView(arcade.View): + + def __init__(self): + # Call the parent class initializer + super().__init__() + + # Variables that will hold sprite lists + self.player_list = None + self.enemy_list = None + self.player_bullet_list = None + self.enemy_bullet_list = None + self.shield_list = None + + # level variable + self.level = 1 + + # life variable + self.lives = 5 + + # Textures for the enemy + self.enemy_textures = None + + # State of the game + self.game_state = Constants.playGame + + # Set up the player info + self.player_sprite = None + self.score = 0 + + # Enemy movement + self.enemy_change_x = - Constants.enemySpeed + + # Don't show the mouse cursor + self.window.set_mouse_visible(False) + + # Load sounds. Sounds from arcade + self.gun_sound = arcade.load_sound(":resources:sounds/laser1.ogg") + self.hit_sound = arcade.load_sound(":resources:sounds/hit3.wav") + self.player_hit = arcade.load_sound(":resources:sounds/hurt1.wav") + self.lost_game = arcade.load_sound(":resources:sounds/lose2.wav") + self.level_up = arcade.load_sound(":resources:sounds/upgrade4.wav") + + def setup_levels(self): + # Load the textures for the enemies, coming to you from Kenney.nl + # enemy image from kenney.nl https://kenney.nl/assets/space-shooter-extension + texture1 = arcade.load_texture("spaceShips_001.png") + texture2 = arcade.load_texture("spaceShips_002.png") + texture3 = arcade.load_texture("spaceShips_003.png") + texture4 = arcade.load_texture("spaceShips_004.png") + texture5 = arcade.load_texture("spaceShips_005.png") + self.enemy_textures = [texture1, texture2, texture3, texture4, texture5] + + # Create rows and columns of enemies + x_count = 6 + x_start = 380 + x_spacing = 60 + y_count = 4 + y_start = 420 + y_spacing = 40 + + # randomize the enemy ships + randomEnemy = random.randint(0, 4) + + for x in range(x_start, x_spacing * x_count + x_start, x_spacing): + for y in range(y_start, y_spacing * y_count + y_start, y_spacing): + # Create the enemy instance + enemy = arcade.Sprite() + enemy.scale = Constants.enemyScaling + enemy.texture = self.enemy_textures[randomEnemy] + + # Position the enemy + enemy.center_x = x + enemy.center_y = y + + # Add the enemy to the lists + self.enemy_list.append(enemy) + + def make_shield(self, x_start): + # shield function + # just a bunch of little sprites stuck together + shield_block_width = 5 + shield_block_height = 10 + shield_width_count = 20 + shield_height_count = 5 + y_start = 150 + for x in range(x_start, + x_start + shield_width_count * shield_block_width, + shield_block_width): + for y in range(y_start, + y_start + shield_height_count * shield_block_height, + shield_block_height): + shield_sprite = arcade.SpriteSolidColor(shield_block_width, + shield_block_height, + arcade.color.RED) + shield_sprite.center_x = x + shield_sprite.center_y = y + self.shield_list.append(shield_sprite) + + def setup(self): + # Initial setup of the game and when we restart the game + self.game_state = Constants.playGame + + # Sprite lists + self.player_list = arcade.SpriteList() + self.enemy_list = arcade.SpriteList() + self.player_bullet_list = arcade.SpriteList() + self.enemy_bullet_list = arcade.SpriteList() + self.shield_list = arcade.SpriteList(is_static=True) + + # Set up the player + self.score = 0 + + # Image from kenney.nl https://kenney.nl/assets/space-shooter-extension + self.player_sprite = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", + Constants.playerScaling) + self.player_sprite.center_x = 50 + self.player_sprite.center_y = 40 + self.player_list.append(self.player_sprite) + + # Make each of the shields initially + for x in range(75, 800, 190): + self.make_shield(x) + + # Set the background color + arcade.set_background_color(arcade.color.BLACK) + + # setup the initial level + self.setup_levels() + + def on_draw(self): + # This command has to happen before we start drawing + self.clear() + + # Draw all the sprites. + self.enemy_list.draw() + self.player_bullet_list.draw() + self.enemy_bullet_list.draw() + self.shield_list.draw() + self.player_list.draw() + + # Render the score + arcade.draw_text(f"Score: {self.score}", 5, 540, arcade.color.WHITE, 14) + + # Render the players lives + arcade.draw_text(f"lives: {self.lives}", 5, 560, arcade.color.WHITE, 14) + + # Render the level + arcade.draw_text(f"Level: {self.level}", 5, 580, arcade.color.WHITE, 14) + + # Draw game over + if self.game_state == Constants.gameOver: + self.clear() + arcade.set_background_color(arcade.csscolor.BLACK) + arcade.draw_text("GAME OVER", 175, 325, arcade.color.WHITE, 55) + arcade.draw_text("Click to Restart", 300, 275, arcade.color.WHITE, 24) + self.set_mouse_visible(True) + + def on_mouse_motion(self, x, y, dx, dy): + # Don't move the player if the game is over + if self.game_state == Constants.gameOver: + return + + self.player_sprite.center_x = x + + def on_mouse_press(self, x, y, button, modifiers): + # Only allow the user so many bullets on screen at a time + if len(self.player_bullet_list) < Constants.maxPlayerBullets + (self.level * 0.4): + # Gun sound + arcade.play_sound(self.gun_sound) + + # Create a bullet + bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", Constants.laserScaling) + + # Rotate image + bullet.angle = 90 + + # Give the bullet a speed + bullet.change_y = Constants.playerBulletSpeed + (self.level * 0.1) + + # Position the bullet + bullet.center_x = self.player_sprite.center_x + bullet.bottom = self.player_sprite.top + + # Add the bullet to the appropriate lists + self.player_bullet_list.append(bullet) + + # if game is over restart game with click + if self.game_state == Constants.gameOver: + game_view = GameView() + game_view.setup() + self.window.show_view(game_view) + + def update_enemies(self): + + # Move the enemy vertically + for enemy in self.enemy_list: + enemy.center_x += self.enemy_change_x + + # Check every enemy to see if any hit the edge. If so, reverse the + # direction and flag to move down. + move_down = False + for enemy in self.enemy_list: + if enemy.right > rightEnemyBorder and self.enemy_change_x > 0: + self.enemy_change_x *= -1 + move_down = True + if enemy.left < leftEnemyBorder and self.enemy_change_x < 0: + self.enemy_change_x *= -1 + move_down = True + + # if we hit the edge then we move down + if move_down: + # Yes + for enemy in self.enemy_list: + # Move enemy down + enemy.center_y -= Constants.enemyMoveDownAmount + + def allow_enemies_to_fire(self): + # Track which x values have had a chance to fire a bullet. + # Since enemy list is build from the bottom up, we can use + # this to only allow the bottom row to fire. + x_spawn = [] + for enemy in self.enemy_list: + # Adjust the chance depending on the number of enemies. Fewer + # enemies, more likely to fire. + chance = 4 + len(self.enemy_list) * 4 + + # Fire if we roll a zero, and no one else in this column has had + # a chance to fire. + if random.randrange(chance) == 0 and enemy.center_x not in x_spawn: + # Create a bullet + bullet = arcade.Sprite(":resources:images/space_shooter/laserRed01.png", Constants.laserScaling) + + # Angle down. + bullet.angle = 180 + + # Give the bullet a speed + bullet.change_y = -Constants.enemyBulletSpeed - (self.level * 0.3) + + # Position the bullet so its top id right below the enemy + bullet.center_x = enemy.center_x + bullet.top = enemy.bottom + + # Add the bullet to the appropriate list + self.enemy_bullet_list.append(bullet) + + # Ok, this column has had a chance to fire. Add to list so we don't + # try it again this frame. + x_spawn.append(enemy.center_x) + + def process_enemy_bullets(self): + + # Move the bullets + self.enemy_bullet_list.update() + + # Loop through each bullet + for bullet in self.enemy_bullet_list: + # Check this bullet to see if it hit a shield + hit_list = arcade.check_for_collision_with_list(bullet, self.shield_list) + + # If it did, get rid of the bullet and shield blocks + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + for shield in hit_list: + shield.remove_from_sprite_lists() + continue + + # See if the player got hit with a bullet + playerHitList = arcade.check_for_collision_with_list(bullet, self.player_list) + + # if so remove a life + if len(playerHitList) > 0: + bullet.remove_from_sprite_lists() + arcade.play_sound(self.player_hit) + self.lives -= 1 + + # if lives reach zero end game + if self.lives == 0: + arcade.play_sound(self.lost_game) + self.game_state = Constants.gameOver + + # If the bullet falls off the screen get rid of it + if bullet.top < 0: + bullet.remove_from_sprite_lists() + + def processEnemyHittingObjects(self): + # Loop through each enemy + for enemy in self.enemy_list: + # Check this enemy to see if it hit a shield + shieldHitList = arcade.check_for_collision_with_list(enemy, self.shield_list) + + # If it did, get rid of the shield blocks + if len(shieldHitList) > 0: + for shield in shieldHitList: + shield.remove_from_sprite_lists() + continue + + # See if the player got hit with an enemy + playerHitList = arcade.check_for_collision_with_list(enemy, self.player_list) + + # if they collide with player + if len(playerHitList) > 0: + enemy.remove_from_sprite_lists() + arcade.play_sound(self.player_hit) + self.lives -= 1 + + if self.lives == 0: + arcade.play_sound(self.lost_game) + self.game_state = Constants.gameOver + + def process_player_bullets(self): + + # Move the bullets + self.player_bullet_list.update() + + # Loop through each bullet + for bullet in self.player_bullet_list: + + # Check this bullet to see if it hit a enemy + hit_list = arcade.check_for_collision_with_list(bullet, self.shield_list) + # If it did, get rid of the bullet + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + for shield in hit_list: + shield.remove_from_sprite_lists() + continue + + # Check this bullet to see if it hit a enemy + hit_list = arcade.check_for_collision_with_list(bullet, self.enemy_list) + + # If it did, get rid of the bullet + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + + # For every enemy we hit, add to the score and remove the enemy + for enemy in hit_list: + enemy.remove_from_sprite_lists() + self.score += 10 + + # Hit Sound + arcade.play_sound(self.hit_sound) + + # If the bullet flies off-screen, remove it. + if bullet.bottom > Constants.screenHeight: + bullet.remove_from_sprite_lists() + + def on_update(self, delta_time): + """ Movement and game logic """ + + if self.game_state == Constants.gameOver: + return + + self.update_enemies() + self.allow_enemies_to_fire() + self.process_enemy_bullets() + self.process_player_bullets() + self.processEnemyHittingObjects() + + if len(self.enemy_list) == 0: + # increase level + self.level += 1 + + # play level up sound + arcade.play_sound(self.level_up) + + # clear lasers + self.player_bullet_list = arcade.SpriteList() + self.enemy_bullet_list = arcade.SpriteList() + + # Remake shields + for x in range(75, 800, 190): + self.make_shield(x) + + self.setup_levels() + + def set_mouse_visible(self, param): + pass + + +# what is initially called to start the game +def main(): + window = arcade.Window(Constants.screenWidth, Constants.screenHeight, Constants.title) + window.show_view(TitleView()) + arcade.run() + + +# make sure main is called and not something else +if __name__ == "__main__": + main() diff --git a/Testing/spaceShips_001.png b/Testing/spaceShips_001.png new file mode 100644 index 000000000..cc6f240f7 Binary files /dev/null and b/Testing/spaceShips_001.png differ diff --git a/Testing/spaceShips_002.png b/Testing/spaceShips_002.png new file mode 100644 index 000000000..4499e9637 Binary files /dev/null and b/Testing/spaceShips_002.png differ diff --git a/Testing/spaceShips_003.png b/Testing/spaceShips_003.png new file mode 100644 index 000000000..637c5a66f Binary files /dev/null and b/Testing/spaceShips_003.png differ diff --git a/Testing/spaceShips_004.png b/Testing/spaceShips_004.png new file mode 100644 index 000000000..9e9c73afe Binary files /dev/null and b/Testing/spaceShips_004.png differ diff --git a/Testing/spaceShips_005.png b/Testing/spaceShips_005.png new file mode 100644 index 000000000..22b7d3c06 Binary files /dev/null and b/Testing/spaceShips_005.png differ diff --git a/Testing/test.py b/Testing/test.py deleted file mode 100644 index e69de29bb..000000000