def run(self): self.screen.blit(self.imgBg, (0, 0)) while not self.gameStatus == "Finished": #tick self.clock.tick(self.fps) #input self.getKeys() #update and draw if (self.gameStatus[2:] == "_Threw"): self.K_pos = self.getCollisions() print(self.K_pos) elif (self.gameStatus[2:] == "_Ready"): self.txtWind.draw(self.screen) self.txtForce.draw(self.screen) self.txtAngle.draw(self.screen) self.btnThrow.draw(self.screen) elif (self.gameStatus[2:] == "_Missed"): self.btnContinue.draw(self.screen) elif (self.gameStatus[2:] == "_Hit"): if self.score_added == False: if self.gameStatus[0:2] == "P1": self.P1_score += 1 elif self.gameStatus[0:2] == "P2": self.P2_score += 1 self.score_added = True PxWinsTxt = Textbox(self.gameStatus[0:2] + " Wins!", (200, 50)) WinCounterTxt = Textbox( str(self.P1_score) + " : " + str(self.P2_score), (200, 150)) PxWinsTxt.draw(self.screen) WinCounterTxt.draw(self.screen) self.btnContinue.draw(self.screen) ##NOT NEEDED IF NOT ONLINE? -> self.nextStatus() # self.screen.fill(0) # redraw background print("####") old_pos = self.K_pos #self.K_pos = self.Kuchen.get_pos() print(old_pos) self.screen.blit( self.imgBg, (old_pos[0] - self.K_size_x, old_pos[1] - self.K_size_y), pygame.Rect(old_pos[0] - self.K_size_x, old_pos[1] - self.K_size_y, 3 * self.K_size_x, 3 * self.K_size_y)) self.screen.blit(self.P1_sprite, self.P1_pos) self.screen.blit(self.P2_sprite, self.P2_pos) self.screen.blit(self.K_sprite, self.K_pos) self.txtForce = Textbox(str(self.throw_force), (self.screen_width // 2, 50)) self.txtAngle = Textbox(str(int(self.throw_angle)), (self.screen_width // 2, 100)) pygame.display.flip() pygame.quit() quit()
def game_intro(WIDTH, HEIGHT, screen, BLACK, WHITE, font, Star, clock): intro = True textObject = Textbox((WIDTH - 200) / 2, 340, 200, 24, 24, 20, True, "", (0, 191, 255), (255, 255, 255), (0, 191, 255)) while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: username = textObject.getName() intro = False return username textObject.handle_event(event) screen.fill(BLACK) # all_sprites_list.add(elementBox) textObject.update() textObject.draw(screen) menuText = font.render('Welcome to the Periodic Table Quiz', True, (WHITE)) menuTextW = menuText.get_width() screen.blit(menuText, ((WIDTH - menuTextW) / 2, 222)) # Background stars for i in range(1, 20): newStarx = random.randint(1, WIDTH) newStary = random.randint(1, HEIGHT) newstarIntro = Star(screen, WHITE, (newStarx, newStary), random.randint(1, 2), 20) pygame.display.update() clock.tick(15)
def game_over(font, WHITE, BLACK, WIDTH, HEIGHT, clock, screen, Star, username, score): ove = True textObject = Textbox((WIDTH - 200) / 2, 340, 200, 24, 24, 20, True, "", (0, 191, 255), (255, 255, 255), (0, 191, 255)) while ove: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: # Appending and reading from LeaderboardT.txt before the leaderboard function is called so data is ready to be displayed # a+ Opens file for both reading and appending file = open("leaderboardT.txt", "a+") file.write(str(username) + "," + (str(score)) + "\n") # the seek() method changes the current file position back to the top so it can be read from the begining again file.seek(0) # Numlines is used to find the range of how many elements need to be blitted later on in the leaderboard numLines = 0 leaderboardTuple = [] for line in file: data = line.split(",") usernameL = data[0] scoreL = data[1] scoreL = scoreL.strip('\n') leaderboardTuple.append((usernameL, int(scoreL))) numLines += 1 # Only the top 10 are displayed if numLines > 10: numLines = 10 print(Sort_Tuple(leaderboardTuple)) print(numLines) file.close() leaderboardTuple = Sort_Tuple(leaderboardTuple) game_leaderboard(font, WHITE, BLACK, WIDTH, HEIGHT, clock, screen, Star, username, score, leaderboardTuple, numLines) ove = False screen.fill(BLACK) overText = font.render('Game Over', True, (WHITE)) overTextW = overText.get_width() screen.blit(overText, ((WIDTH - overTextW) / 2, (HEIGHT) / 2)) for i in range(1, 20): newStarx = random.randint(1, WIDTH) newStary = random.randint(1, HEIGHT) newstarDifficult = Star(screen, WHITE, (newStarx, newStary), random.randint(1, 2), 20) pygame.display.update() clock.tick(15)
def main(t, lang, d): d.loadData() window_ = AppWindow(t) window_.lang = None teacher_table = Table(repr='teachertable') teacher_table_headers = [ 'Date', 'Check-In Time', 'Start Time', 'Check-Out Time', 'Confirm Time' ] window_.newFrame("First Frame", (1, 0)) window_.newFrame("Second Frame", (2, 0)) window_.newFrame("Third Frame", (2, 1)) window_.newFrame("Fourth Frame", (4, 1)) window_.newFrame("Fifth Frame", (3, 0)) window_.frames["Second Frame"].rowconfigure(0, weight=5, minsize=350) window_.frames["Second Frame"].columnconfigure(0, weight=5, minsize=630) window_.frames["Fifth Frame"].grid(columnspan=3) search_value = Textbox(text="Search", repr=None) search_options = Toggle_option( options=(('Barcode', 'bCode'),('First Name', 'firstName'), \ ('Last Name', 'lastName'), ('Chinese Name', 'chineseName'), \ ('Phone Number', 'phoneNumber')), repr=None) search_button = Buttonbox(text='Search', lang=window_.lang, repr='searchbutton') window_.frames["First Frame"].addWidget(search_value, (0, 0)) window_.frames["First Frame"].addWidget(search_options, (1, 0)) window_.frames["First Frame"].addWidget(search_button, (0, 1)) fward = Buttonbox(text='>> Next 30 >>', lang=window_.lang, repr='>>') bward = Buttonbox(text='<< Previous 30 <<', lang=window_.lang, repr='<<') blast = Buttonbox(text='>>> Last Page >>>', lang=window_.lang, repr='>>>') window_.frames["Fifth Frame"].addWidget(fward, (1, 1)) window_.frames["Fifth Frame"].addWidget(bward, (1, 0)) window_.frames["Fifth Frame"].addWidget(blast, (1, 2)) fward.config(width=17) bward.config(width=17) blast.config(width=17) fward.selfframe.grid(padx=2) bward.selfframe.grid(padx=2) blast.selfframe.grid(padx=2) #window_.frames["Second Frame"].addWidget(teacher_table, (2, 0)) sL, page_list = [], [] for s in d.studentList.values(): dp = s.datapoints sL.append([ dp['bCode'], dp['firstName'], dp['lastName'], dp['chineseName'], dp['dob'] ]) sL.sort() students_on_page = [] new_frame = Frame(window_.frames["Second Frame"]) for s in sL: students_on_page.append(s) if len(students_on_page) >= 15: teacher_table_on_page = Table(repr='teachertable') teacher_table_on_page.place(parent=new_frame, row=0, column=0) teacher_table_on_page.setData(headers=teacher_table_headers, data=students_on_page) page_list.append(new_frame) new_frame = Frame(window_.frames["Second Frame"]) students_on_page = [] elif s == sL[-1]: teacher_table_on_page = Table(repr='teachertable') teacher_table_on_page.place(parent=new_frame, row=0, column=0) teacher_table_on_page.setData(headers=teacher_table_headers, data=students_on_page) page_list.append(new_frame) page_list[0].grid() #sL.append(l) #if len(sL[-1]) == 0 and len(sL) != 1: sL.pop() window_.pNum = 1 def toPage(p): teacher_table.setData(headers=teacher_table_headers, data=sL[p]) teacher_table.canvas.config(width=700, height=350) teacher_table.set_width(1, 5, 14) def open_edit_window(pos): student_id = teacher_table.data[pos[0] - 1][0] editS2.main(window_.lang, d, i=student_id) for pos, cell in teacher_table.cells.items(): if pos[0] == 0: continue cell.config(bind=('<Double-Button-1>', lambda event, pos=pos: open_edit_window(pos))) def next(): if window_.pNum == len(sL) - 1: return toPage(window_.pNum + 1) window_.pNum = window_.pNum + 1 def previous(): if window_.pNum == 1: return toPage(window_.pNum - 1) window_.pNum = window_.pNum - 1 def last(): window_.pNum = len(sL) - 1 toPage(window_.pNum) ''' if len(sL[0]) > 15: toPage(1) fward.config(cmd=next) bward.config(cmd=previous) blast.config(cmd=lambda: toPage(len(sL) - 1)) first_page.config(cmd=lambda: toPage(0)) else: toPage(0) ''' def s(): window_.s = window_.search_value.getData() if search_options.stringvar.get() != 'bCode': sty = search_options.stringvar.get() sdp = window_.search_value.getData() sl = [] for s in d.studentList: dp = False if sty == 'phoneNumber': if d.studentList[s].datapoints['hPhone'] == sdp or \ d.studentList[s].datapoints['cPhone'] == sdp or \ d.studentList[s].datapoints['cPhone2'] == sdp: dp = d.studentList[s].datapoints elif d.studentList[s].datapoints[sty] == sdp: dp = d.studentList[s].datapoints if dp: sl.append([ dp['bCode'], dp['firstName'], dp['lastName'], dp['chineseName'] ]) if len(sl) == 0: student_does_not_exist(window_.lang) return window_.s = sl[0][0] if len(sl) > 1: sl.sort() window_.s = spicker(sl) if not window_.s: return editS2.main(window_.lang, d=d, top=True, i=window_.s) search_value.entry.bind("<Return>", lambda x: s()) search_button.config(cmd=s)
def __init__(self, ): self.fps = 60 self.screen_width = 1024 self.screen_height = 580 self.gravity = 0.1 # Online? self.online = False # Images # K is for Kuchen, P1 and P2 for Player 1 and Player 2 self.K_sprite = pygame.image.load("drawables/kuchen.png") self.P1_sprite = pygame.image.load("drawables/player1.png") self.P2_sprite = pygame.image.load("drawables/player2.png") # Sizes self.K_size_x = 10 self.K_size_y = 10 P1_size_x = 20 P1_size_y = 20 P2_size_x = 20 P2_size_y = 20 # Positions self.K_pos = (15, self.screen_height - 15) self.P1_pos = (0, self.screen_height - P1_size_y) self.P2_pos = (self.screen_width - P2_size_x, self.screen_height - P2_size_y) # Init of Items in the game self.Kuchen = KuchenClass(self.K_size_x, self.K_size_y, self.K_pos[0], self.K_pos[1]) self.Kuchen.set_v(0, 0) self.Player1 = Item(P1_size_x, P1_size_y, self.P1_pos[0], self.P1_pos[1]) self.Player1.set_v(0, 0) self.Player1.set_a(0, self.gravity) self.Player2 = Item(P2_size_x, P2_size_y, self.P2_pos[0], self.P2_pos[1]) self.Player2.set_v(0, 0) self.Player2.set_a(0, self.gravity) # Pygame elements pygame.init() pygame.font.init() gameDisplay = pygame.display self.screen = gameDisplay.set_mode( (self.screen_width, self.screen_height)) gameDisplay.set_caption('Throw the Kuchen') self.clock = pygame.time.Clock() allsprites = pygame.sprite.RenderPlain( (self.Player1, self.Player2, self.Kuchen)) # Background image self.imgBg = pygame.image.load("drawables/bg.png") # User Interface Elements self.wind = self.get_wind() if self.wind < 0: self.msgWind = str(abs(self.wind)) + " ->" elif self.wind > 0: self.msgWind = "<- " + str(abs(self.wind)) else: self.msgWind = str(abs(self.wind)) self.txtWind = Textbox(self.msgWind, (self.screen_width // 2, 0)) self.txtForce = Textbox(str(self.throw_force), (self.screen_width // 2, 50)) self.txtAngle = Textbox(str(int(self.throw_angle)), (self.screen_width // 2, 100)) self.btnThrow = Button("drawables/button_throw.png", (self.screen_width // 2, 200), "Throw!") self.btnContinue = Button("drawables/button_continue.png", (self.screen_width // 2, 200), "Press to continue") # This might be changed if we use a random starting player self.gameStatus = "P1_Ready" self.score_added = False
def getKeys(self): delta_angle = 0 delta_force = 0 keys = pygame.key.get_pressed() if keys[pygame.K_UP]: if self.throw_angle < 90: self.throw_angle += 0.05 else: self.throw_angle = 90 if keys[pygame.K_DOWN]: if self.throw_angle > 0: self.throw_angle -= 0.05 else: self.throw_angle = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.MOUSEBUTTONUP: mousepos = pygame.mouse.get_pos() bt_area = self.btnThrow.get_area() bc_area = self.btnContinue.get_area() if bt_area[0] < mousepos[0] < bt_area[1] and bt_area[ 2] < mousepos[1] < bt_area[3]: self.gameStatus = self.throw(self.Kuchen, self.gameStatus, self.wind) if self.wind < 0: self.msgWind = str(abs(self.wind)) + " ->" elif self.wind > 0: self.msgWind = "<- " + str(abs(self.wind)) else: self.msgWind = str(abs(self.wind)) self.txtWind = Textbox(self.msgWind, (self.screen_width // 2, 0)) if bc_area[0] < mousepos[0] < bc_area[1] and bc_area[ 2] < mousepos[1] < bc_area[3]: if self.gameStatus[2:] == "_Hit": self.score_added = False self.wind = self.nextPlayer() if self.wind > 0: self.msgWind = str(abs(self.wind)) + " ->" elif self.wind < 0: self.msgWind = "<- " + str(abs(self.wind)) else: self.msgWind = str(abs(self.wind)) self.txtWind = Textbox(self.msgWind, (self.screen_width // 2, 0)) if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: delta_angle = 1 if event.key == pygame.K_DOWN: delta_angle = -1 if event.key == pygame.K_LEFT: delta_force = -1 if event.key == pygame.K_RIGHT: delta_force = 1 if event.key == pygame.K_RETURN: if self.gameStatus[2:] == "_Ready": self.gameStatus = self.throw(self.Kuchen, self.gameStatus, self.wind) elif self.gameStatus[2:] == "_Missed" or self.gameStatus[ 2:] == "_Hit": if self.gameStatus[2:] == "_Hit": self.score_added = False self.wind = self.nextPlayer() if self.wind > 0: self.msgWind = str(abs(self.wind)) + " ->" elif self.wind < 0: self.msgWind = "<- " + str(abs(self.wind)) else: self.msgWind = str(abs(self.wind)) self.txtWind = Textbox(self.msgWind, (self.screen_width // 2, 0)) self.screen.blit(self.K_sprite, self.K_pos) if event.type == pygame.KEYUP: if event.key == pygame.K_UP: delta_angle = 0 if event.key == pygame.K_DOWN: delta_angle = 0 if event.key == pygame.K_LEFT: delta_force = 0 if event.key == pygame.K_RIGHT: delta_force = 0 self.throw_angle += delta_angle self.throw_force += delta_force
def game_run(): file = r'activation.mp3' # Initialize pygame.mixer.init() # Load the file pf music pygame.mixer.music.load(file) pygame.mixer.music.play() # Initialize and create screen. pygame.init() setting = Setting() screen = pygame.display.set_mode( (setting.screen_width, setting.screen_height)) pygame.display.set_caption("Aircraft war") # Create play button play_button = Button(setting, screen, 'Play', 200) help_button = Button(setting, screen, 'Help', 400) # Draw a rocket. rocket = Rocket(setting, screen) # Set a Group for bullets. bullets = Group() # Set a Group for alien bullets. alien_bullets = Group() # Set a Group for aliens. aliens = Group() # Create aliens. functions.create_aliens(setting, screen, rocket, aliens, alien_bullets) # Create States. states = States(setting) # Create Scoreboard. scoreboard = Scoreboard(setting, screen, states) # Create Textbox. textbox = Textbox(setting, screen) # Create Incidents. incidents = Group() # Create Background. BackGround = Background('background.jpg', [0, 0]) # Main loop. while True: functions.respond_events(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets, play_button, help_button, textbox, incidents) if states.game_active: rocket.update_rocket() bullets.update(rocket) alien_bullets.update(setting) incidents.update(states) functions.update_bullets(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets) functions.update_aliens(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets, incidents) functions.update_alien_bullets(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets, incidents) functions.update_incidents(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets, incidents) functions.screen_update(setting, screen, states, scoreboard, rocket, aliens, bullets, alien_bullets, play_button, help_button, textbox, incidents) screen.fill([255, 255, 255]) screen.blit(BackGround.image, BackGround.rect)
Player2.set_v(0, 0) Player2.set_a(0, GRAVITY) # Pygame elements pygame.init() pygame.font.init() gameDisplay = pygame.display screen = gameDisplay.set_mode((MAX_X, MAX_Y)) gameDisplay.set_caption('Throw the Kuchen') clock = pygame.time.Clock() # User Interface Elements if WIND < 0: windMsg = str(abs(WIND)) + " ->" elif WIND > 0: windMsg = "<- " + str(abs(WIND)) else: windMsg = str(abs(WIND)) windTxt = Textbox(windMsg , (200, 0)) forceTxt = Textbox(str(throw_force), (200, 50)) angleTxt = Textbox(str(int(throw_angle)), (200, 100)) throwButton = Button("drawables/button_throw.png", (200, 200), "Throw!") continueButton = Button("drawables/button_continue.png", (200, 200), "Press to continue") # Statuses: # JustStarted # Px_Ready # Px_Threw # Px_Hit # Px_Missed gameStatus = "JustStarted" # Just in case we want to start randomly here gameStatus = "P1_Ready" score_added = False
c = cells[i] if c.t == Terrain.PATH: towers.append(Tower(i, c.x, c.y, cell_size, TowerType.PATH.value)) # users can't replace this #display.register(towers[len(towers) - 1]) else: towers.append(Tower(i, c.x, c.y, cell_size, TowerType.ZEROTOWER.value)) # users can replace this #display.register(towers[len(towers) - 1]) # enemies enemies = [] # menu objects for buying and placing # position should be relative to cell_size menu = [] # 0-1 ; Title menu.append(Textbox((6, 6), dark(red), font, "Neon Tower Defence")) display.register(menu[0]) menu.append(Textbox((5, 5), text_color, font, "Neon Tower Defence")) display.register(menu[1]) # 2-4 ; Level playbutton = Cell(-4, cell_size * 3 + 5, cell_size, cell_size / 4 * 3, Terrain.PLAYBUTTON) level_active = False # this function is part of the menu: playbutton def start_level(e): global level_active, menu, wave_number if not level_active: if e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: m_pos = pygame.mouse.get_pos()