This commit is contained in:
phowell 2023-04-18 06:37:07 -07:00
parent 6807ddd96c
commit 562a8b3e26
1 changed files with 106 additions and 17 deletions

View File

@ -15,20 +15,38 @@
import simpy
import simpy, sys
import pygame
import random
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((1100, 750))
pygame.display.set_caption('School Simulation')
# Define the simulation environment
env = simpy.Environment()
num_students=50
num_teachers=3
num_rooms=4
num_courses=60
num_semesters = 20
# Define a custom event type
SEMESTER_END = pygame.USEREVENT + 1
# Define the resources (rooms and teachers)
rooms = [simpy.Resource(env, capacity=20) for i in range(10)]
teachers = [simpy.Resource(env, capacity=1) for i in range(30)]
rooms = [simpy.Resource(env, capacity=20) for i in range(num_rooms)]
teachers = [simpy.Resource(env, capacity=1) for i in range(num_teachers)]
# Define the courses and majors
majors = ['major1', 'major2', 'major3', 'major4', 'major5']
courses = {'course' + str(i): {'credits': 3} for i in range(50)}
courses = {'course' + str(i): {'credits': 3} for i in range(num_courses)}
current_semester = ''
students = []
# Define the student process
class Student:
@ -37,51 +55,122 @@ class Student:
self.id = id
self.major = major
self.completed_courses = []
def overview(self):
print("Student", self.id, "is majoring in", self.major)
print("Completed courses:", self.completed_courses, "\n")
def choose_courses(self):
# Implement a custom function to determine which courses the student chooses each semester
pass
# Choose a random number of courses (between 3 and 5)
num_courses = random.randint(3, 5)
# Randomly select the courses to take
chosen_courses = random.sample(list(courses.keys()), num_courses)
return chosen_courses
def attend_courses(self):
# Choose the courses to attend
chosen_courses = self.choose_courses()
# Request a room and teacher for each course
for course in chosen_courses:
with rooms[random.randint(0, 9)].request() as room_request:
with teachers[random.randint(0, 29)].request() as teacher_request:
with rooms[random.randint(0, num_rooms-1)].request() as room_request:
with teachers[random.randint(0, num_teachers-1)].request() as teacher_request:
# Simulate attending the course
yield self.env.timeout(1)
# Update the completed courses list
self.completed_courses.append(course)
# Define the game drawing function
def draw_game():
# Clear the screen
screen.fill((255, 255, 255))
# Draw the current semester number
font = pygame.font.Font(None, 36)
font2 = pygame.font.Font(None, 12)
text = font.render('Semester: ' + str(current_semester+1), True, (0, 0, 0))
screen.blit(text, (10, 10))
# Draw the number of completed courses for each student
box_width = 10
box_height = 10
y = 50
for student in students:
text = font2.render('Student ' + str(student.id) + ': ' + str(len(student.completed_courses)) + ' courses', True, (0, 0, 0))
screen.blit(text, (10, y))
# Draw the boxes for each course
x = 150
for course in courses:
if course in student.completed_courses:
box_color = (128, 128, 128) # Grey for completed courses
else:
box_color = (200, 200, 200) # White for incomplete courses
pygame.draw.rect(screen, box_color, (x, y, box_width, box_height))
x += box_width + 2 # Add some spacing between boxes
y += box_height + 2 # Add some spacing between rows
# Update the screen
pygame.display.update()
# Define the main simulation process
def run_simulation(env, num_students):
global current_semester, students
# Create the students
students = [Student(env, i, random.choice(majors)) for i in range(num_students)]
# Simulate for 4 years (8 semesters)
for semester in range(8):
for semester in range(num_semesters):
current_semester = semester
for student in students:
env.process(student.attend_courses())
yield env.timeout(16) # each semester is 16 weeks long
#student.overview()
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("School Simulation")
# Trigger a redraw of the game screen at the end of each semester
# Create a custom event and post it to the pygame event queue
custom_event = pygame.event.Event(SEMESTER_END, {'some_key': 'a', 'other_key': 'b'})
pygame.event.post(custom_event)
yield env.timeout(16) # each semester is 16 weeks long
print("Semester " + str(semester+1) + " is over")
# could update game state in separate fxn
# Draw the updated game state
draw_game()
pygame.display.update()
# Delay between semesters
pygame.time.wait(100)
# Start the simulation
env.process(run_simulation(env, 1000))
env.process(run_simulation(env, num_students))
env.run()
# Define a Pygame loop to visualize the simulation
while True:
for event in pygame.event.get():
if event.type == SEMESTER_END:
# Extract data from the event
some_value = event.some_key
other_value = event.other_key
# Update game state with the event data
#update_game_state(some_value, other_value)
draw_game()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
#screen.fill((255, 255, 255))
# Draw the simulation elements (e.g., students, rooms, teachers, courses)
# ...
pygame.display.update()
#draw_game()
#pygame.display.update()
"""