-
Notifications
You must be signed in to change notification settings - Fork 8
Turn in the Mini Project 4 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 36 commits
b70ae97
c40af24
086c735
104e084
2cc94cd
d61d10c
b2a41e0
a913dc0
8486919
bbf1fd4
b80dbf4
262f5c1
7fdce31
4273f42
558de66
20379d8
db39c83
d73f897
95155e0
16cbf96
a11db98
741f559
d8be63f
7e115a0
f49c50c
0a42179
6fd3c15
1fae333
4e5d19a
6b59ba4
d5c9fd7
1d851ea
6086d69
ef209e0
a2e05c5
3cb86bd
b2326b7
9a12fb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """ | ||
| defining class country | ||
| Subeen Kim | ||
| """ | ||
|
|
||
| class country: | ||
|
|
||
| def __init__(self, max_pop, infected_pop=0, infection_rate=1.1, center_x=100, center_y=200, radius=30): | ||
| self.max_pop = max_pop | ||
| self.infected_pop = infected_pop | ||
| self.infection_rate = infection_rate | ||
| self.center_x = center_x | ||
| self.center_y = center_y | ||
| self.radius = radius | ||
|
|
||
| def number_infection(self, click): | ||
| """ click gets True of False from the mouse click """ | ||
| while click: | ||
| self.infected_pop = self.infected_pop*self.infection_rate | ||
| if self.infected_pop >= self.max_pop: | ||
| break | ||
| return int(self.infected_pop) | ||
|
|
||
| Egypt = country(1) | ||
| Egypt.max_pop = 100 | ||
| number_infection(Egypt, True) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| import os | ||
| import pygame | ||
| import random | ||
|
|
||
| BLACK = (0, 0, 0) | ||
| RED = (255, 0, 0) | ||
| GREEN = (0, 255, 0) | ||
| BLUE = (0, 0, 255) | ||
|
|
||
|
|
||
| pygame.init() | ||
| font = pygame.font.SysFont('Consolas', 20) | ||
|
|
||
| class Country: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class could be put into its own file and imported. |
||
| """ | ||
| Each country has its maximum population without any infected people, | ||
| before we click the country in the beginning of the game. | ||
| """ | ||
| def __init__(self, x, y, max_pop, radius=50, color=RED, infected_pop=0, infected_rate=1.1, dead_pop=0, death_rate=1, airborne_rate=0): | ||
| """ | ||
| Position and color of each country are defined. | ||
| Population(Infected, death, toal), and Rate(Infection, death, airborne) are defined. | ||
| """ | ||
| self.initial_pos = (x, y) | ||
| self.x = x | ||
| self.y = y | ||
| self.color = color | ||
| self.radius = radius | ||
| self.infected_pop = infected_pop | ||
| self.infected_rate = infected_rate | ||
| self.max_pop = max_pop | ||
| self.dead_pop = dead_pop | ||
| self.death_rate = death_rate | ||
| self.airborne_rate = airborne_rate | ||
|
|
||
| def infected_ratio(self): | ||
| """ | ||
| The ratio of infection (population of infected people / toal population of a country) | ||
| """ | ||
| if self.max_pop != 0: | ||
| return int(self.infected_pop) / self.max_pop | ||
| else: | ||
| return 1 | ||
|
|
||
| def death(self): | ||
| """ | ||
| A part of the infected population would be passed away. | ||
| Then the infected population and maximum population will be reduced as many as the number of people death. | ||
| """ | ||
| death_pop = 0 | ||
| alive_pop = self.max_pop | ||
| if self.infected_ratio() > 0.10: | ||
| if self.infected_pop > 10: | ||
| death_pop = int(self.death_rate*self.infected_pop*(random.random()/15)) | ||
| else: | ||
| if self.max_pop >= 1: | ||
| death_pop = 1 | ||
| else: | ||
| death_pop = 0 | ||
|
|
||
| self.infected_pop = self.infected_pop - death_pop | ||
| self.max_pop -= death_pop | ||
| self.dead_pop += death_pop | ||
|
|
||
| def step(self): | ||
| """ | ||
| If infection starts (infect_pop changed into unity), | ||
| then the infection starts with the certain rate | ||
| """ | ||
| if self.infected_pop < self.max_pop: | ||
| self.infected_pop = self.infected_pop * self.infected_rate | ||
| if self.infected_pop >= self.max_pop: | ||
| self.infected_pop = self.max_pop | ||
|
|
||
| """return integer part of infected population""" | ||
| # return int(self.infected_pop) | ||
|
|
||
| """return probability""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For comments that are inside a function and describe the inside view how some lines of code work (as opposed to docstrings, that are at the top and describe the outside view of what the whole function does), use |
||
| return self.infected_ratio() | ||
|
|
||
|
|
||
| def draw(self): | ||
| """ draws the location of the country as a circle """ | ||
| pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), | ||
| self.radius) | ||
|
|
||
| def contains_pt(self, pt): | ||
| """ countains points function to see if mouseclicks are | ||
| within the circles of the country """ | ||
| x, y = pt | ||
| if not self.x - self.radius < x < self.x + self.radius: | ||
| return False | ||
| if not self.y - self.radius < y < self.y + self.radius: | ||
| return False | ||
| return True | ||
|
|
||
| def propagation(self, other): | ||
| """ | ||
| The pathogen can propagate to other countries with certain probability. | ||
| """ | ||
| if self.infected_pop >= 1 and other.infected_pop == 0: | ||
| if random.random() <= self.infected_ratio()/10: | ||
| other.infected_pop = 1 | ||
|
|
||
|
|
||
| background_color = (255,255,255) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually, global variables are defined before any other code is written - but this doesn't affect the rest of your code in any way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention, global variables are capitalized e.g. |
||
| width, height = 640, 480 | ||
|
|
||
| screen = pygame.display.set_mode((width, height)) | ||
| pygame.display.set_caption('Plague Simulation') | ||
| screen.fill(background_color) | ||
|
|
||
| intro = True | ||
|
|
||
| while intro: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to put the game intro, main loop, and ending is separate functions that you can call at the appropriate times. This helps with code organization. |
||
| """ Intro of the game gives the intro screen with | ||
| upgrade instructions as well as how to start the game """ | ||
|
|
||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| quit() | ||
| if event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_c: | ||
| intro = False | ||
| running = True | ||
| if event.key == pygame.K_q: | ||
| pygame.quit() | ||
| quit() | ||
| """ Renders the text where it tells the user to | ||
| press c to start the game, and gives instructions on how to upgrade the game""" | ||
|
|
||
| basicfont = pygame.font.SysFont(None, 20) | ||
| text = basicfont.render('Welcome To A Plague Simulation! Press C to Start, Click on a country to start your infection', True, (0, 0, 0), (255, 255, 255)) | ||
| textrect = text.get_rect() | ||
| textrect.centerx = screen.get_rect().centerx | ||
| textrect.centery = screen.get_rect().centery | ||
| screen.blit(text, textrect) | ||
| screen.blit(basicfont.render('For Upgrades, Press I to increase infection rate, K to increase kill rate, and A to increase airborne rate' , True, (0, 0, 0), (255, 255, 255)), (0, 300 )) | ||
|
|
||
| pygame.display.update() | ||
|
|
||
| screen = pygame.display.set_mode((640, 480)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this setup code could go into a |
||
|
|
||
| """ Herein, three countries are defined """ | ||
| country1 = Country(200, 120, 200, radius=60) | ||
| country2 = Country(500, 180, 200, radius=40, color=BLUE) | ||
| country3 = Country(320, 360, 200, radius=50, color=GREEN) | ||
| countries = [country1, country2, country3] | ||
| country_pop_index = country1 | ||
| total_pop = 0 | ||
|
|
||
| Time = 0 | ||
| time = 0 | ||
| Upgrade_Point = 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By convention, variables whose values change (variables that are not constants) are all lowercase. |
||
| infectionindex = 1 | ||
| """ This is the counter to allow you to | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need multiline comments here - inline comments will suffice. |
||
| click on a country and place a pathogen""" | ||
| clock = pygame.time.Clock() | ||
|
|
||
| upgrades = 0 | ||
|
|
||
| """ Pressing C will officially start the game running our | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main game loop should be inside its own function. |
||
| game function with time""" | ||
|
|
||
| running = True | ||
| while running: # forever -- until user clicks in close box | ||
| for event in pygame.event.get(): | ||
| if event.type == pygame.MOUSEBUTTONDOWN: | ||
| for country in countries: | ||
| """ start of the game, clicking the first country | ||
| will place the first pathogen in the country """ | ||
| if country.contains_pt(pygame.mouse.get_pos()): | ||
| if infectionindex == 1: | ||
| country.infected_pop = country.infected_pop + 1 | ||
| infectionindex = infectionindex - 1 | ||
| country_pop_index = country | ||
| """now our pathogen embarks on infection!""" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use inline comments (like this: |
||
|
|
||
| """Upgrade functions that cost more and more as upgrades increase""" | ||
| if event.type == pygame.KEYDOWN: | ||
| #Upgrade infection rate | ||
| if event.key == pygame.K_i: | ||
| if Upgrade_Point > upgrades**2: | ||
| for country in countries: | ||
| country.infected_rate = country.infected_rate * 1.15 | ||
| Upgrade_Point = Upgrade_Point - upgrades**2 | ||
| upgrades = upgrades + 1 | ||
| print (country.infected_rate) | ||
|
|
||
| #increase kill rate | ||
| if event.key == pygame.K_k: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot of this code could be refactored so it's easier to read.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One guideline is the number of lines of a section of code, and how many different things it's doing. When you find yourself labeling a subsection as to what it does, this is a sign that this section of code could be pulled out into a separate function. Doing this gives you something to name, and document, and potentially test. |
||
| if Upgrade_Point > upgrades**2: | ||
| for country in countries: | ||
| country.death_rate = country.death_rate * 1.15 | ||
| Upgrade_Point = Upgrade_Point - upgrades**2 | ||
| upgrades = upgrades + 1 | ||
| print (country.death_rate) | ||
| #increase airborne capacity | ||
| if event.key == pygame.K_a: | ||
| if Upgrade_Point > upgrades**2: | ||
| for country in countries: | ||
| country.airborne_rate = country.airborne_rate + 0.15 | ||
| Upgrade_Point = Upgrade_Point - upgrades**2 | ||
| upgrades = upgrades + 1 | ||
| print (country.airborne_rate) | ||
|
|
||
| """Modify Time + XXXX to modify the speed of the game.""" | ||
| if pygame.time.get_ticks() > (Time + 1000): | ||
| Time = pygame.time.get_ticks() | ||
| if all(country.max_pop == 0 for country in countries) == True: | ||
| running = False | ||
| endscreen = True | ||
| #print ('For each country: (infected ratio, total population)', (country1.infected_ratio(),country1.max_pop), (country2.infected_ratio(),country2.max_pop), (country3.infected_ratio(),country3.max_pop)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest removing print statements and logs before submitting pull requests (in the future) |
||
| Total_infected = 0 | ||
|
|
||
| """ | ||
| Infection types: step, death, propagation | ||
| Upgrade Point is given at every step. | ||
| """ | ||
| for country in countries: | ||
| country.step() | ||
| country.death() | ||
| Total_infected += (country.infected_pop + country.dead_pop) | ||
| if infectionindex == 0: | ||
| if country.max_pop !=0: | ||
| if pygame.time.get_ticks() > (time + 2000): | ||
| time = pygame.time.get_ticks() | ||
| Upgrade_Point += random.randint(1,3) | ||
| for other in countries: | ||
| if country != other: | ||
| country.propagation(other) | ||
|
|
||
| if event.type == pygame.QUIT: | ||
| running = False | ||
|
|
||
| screen.fill(BLACK) # erases screen | ||
| for country in countries: | ||
| country.draw() | ||
|
|
||
| """ | ||
| the number of infected, dead, and total population is displayed whenever we click certain country | ||
| """ | ||
| screen.blit(font.render('Infected:%.2d'%(country_pop_index.infected_pop) + ' ' +'Dead:%.2d'%(country_pop_index.dead_pop) + ' '+ 'Alive:%.2d'%(country_pop_index.max_pop) +' '+'Upgrade Point:%.2d'%(Upgrade_Point) , True, (0, 255, 255)), (0, 440)) | ||
| screen.blit(font.render('Current Upgrades:%.2d'%(upgrades), True, (0, 255, 255)), (400, 400)) | ||
| pygame.display.update() # updates real screen from staged screen | ||
|
|
||
|
|
||
| while endscreen: | ||
| screen.fill(background_color) | ||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| quit() | ||
|
|
||
| if event.type == pygame.KEYDOWN: | ||
| if event.key == pygame.K_q: | ||
| pygame.quit() | ||
| quit() | ||
|
|
||
|
|
||
| text = basicfont.render('Congradulations you have killed everyone! Press Q to end the game.', True, (0, 0, 0), (255, 255, 255)) | ||
| textrect = text.get_rect() | ||
| textrect.centerx = screen.get_rect().centerx | ||
| textrect.centery = screen.get_rect().centery | ||
| screen.blit(text, textrect) | ||
|
|
||
| pygame.display.update() | ||
|
|
||
| pygame.quit() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By convention, class names are capitalized (
Country).