def run(self, settings): self.playing = True while self.playing: self.new() self.dt = self.clock.tick(settings.FPS) / 1000 gf.checkEvents(settings, self.newPlayer, self.wallGroup) self.update() self.draw(settings)
def app(): # initialize pygame pygame.mixer.pre_init(22050, -16, 2, 512) pygame.mixer.init() pygame.init() pygame.mixer.music.load('resources/sound/hit.mp3') #create and open a window on the screen with given dimensions screen = pygame.display.set_mode((900, 600)) screenRect = screen.get_rect() screen.fill((230, 230, 230)) pygame.display.set_caption("Pong") #make the play button play = Button(screen, "Play") text_color = (255, 255, 255) button_color = (39, 160, 205) font = pygame.font.SysFont(None, 60) center = screenRect.center msg_image = font.render("HELLO WORLD", True, text_color, button_color) msg_image_rect = msg_image.get_rect() msg_image_rect.center = center setting = Settings() setting.gameActive = False #create my paddles paddle1 = Paddle(screen, 1) paddle2 = Paddle(screen, 2) paddle3 = Paddle(screen, 3) paddle4 = Paddle(screen, 4) paddle5 = Paddle(screen, 5) paddle6 = Paddle(screen, 6) ball = Ball(screen) score = Score(screen) #start the main loop for the game. while True: gf.checkEvents(ball, screen, paddle1, paddle2, paddle3, paddle4, paddle5, paddle6, play, setting) if setting.gameActive: paddle1.update() paddle2.update() paddle3.update() paddle4.update() paddle5.update() paddle6.update() ball.update(paddle1, paddle2, paddle3, paddle4, paddle5, paddle6, score) screen.blit(msg_image, msg_image_rect) gf.updateScreen(ball, screen, paddle1, paddle2, paddle3, paddle4, paddle5, paddle6, score, play, setting)
def runGame(): """<<alien invasion>>主函数""" # 初始化游戏 pygame.init() # 创建Settings类对象访问游戏设置 aiSettings = Settings() # 创建一个屏幕对象 screen = pygame.display.set_mode( (aiSettings.screenWidth, aiSettings.screenHeight)) """ set_mode会返回一个Surface对象,代表了在桌面上出现的那个窗口,三个参数第一个为元祖,代表分辨率(必须); 第二个是一个标志位,具体意思见下表,如果不用什么特性,就指定0;第三个为色深。 标志位 功能 FULLSCREEN 创建一个全屏窗口 DOUBLEBUF 创建一个“双缓冲”窗口,建议在HWSURFACE或者OPENGL时使用 HWSURFACE 创建一个硬件加速的窗口,必须和FULLSCREEN同时使用 OPENGL 创建一个OPENGL渲染的窗口 RESIZABLE 创建一个可以改变大小的窗口 NOFRAME 创建一个没有边框的窗口 """ # 创建一个统计数据存储对象,并初始化游戏部分动态参数 status = GameStatus(aiSettings) # 创建一个飞船对象 ship = Ship(aiSettings, screen) # 创建一组外星人序列 aliens = Group() # 创建一组子弹序列 bullets = Group() # 创建PLAY按钮 playButton = Button(aiSettings, screen, 'Play') # 创建计分板 scoreBoard = ScoreBoard(aiSettings, screen, status) # 设置窗口标题 pygame.display.set_caption("Alien Invasion By JerCas丶Ety") # 开始游戏主循环 """ 无限循环,直到用户跳出。在这个主循环里做的事情就是不停地画背景和更新光标位置, 虽然背景是不动的,我们还是需要每次都画它, 否则鼠标覆盖过的位置就不能恢复正常了。 """ while True: # 1.检查玩家输入-2.更新飞船位置-3.更新所有未消失子弹位置并删除已消失子弹位置-4.根据以上更新后的screen对象位置刷新重新绘制屏幕 gameFunction.checkEvents(aiSettings, screen, status, playButton, ship, aliens, bullets, scoreBoard) # 游戏处于活动状态时 才刷新三者对象位置 if status.gameActive: # 飞船位置更新,!!!必须置于屏幕刷新语句前,以便实现飞船位置更新后再刷新屏幕而将更新后的新位置刷新到屏幕上以实现视觉上的位置移动 ship.update() gameFunction.updateBullets(aiSettings, screen, status, ship, aliens, bullets, scoreBoard) gameFunction.updateAliens(aiSettings, status, screen, ship, aliens, bullets, scoreBoard) gameFunction.updateScreen(aiSettings, screen, status, ship, aliens, bullets, playButton, scoreBoard)
def run(self, settings): self.playing = True settings.endingMovements = False while self.playing: self.new() gf.checkEvents(self, settings, self.sb, self.newPlayer, self.wallGroup, USEREVENT) self.update() self.draw(settings) if self.playerRestarts: self.restartGame()
def runWarehouse(): """Main Function (init lists and starts main loop)""" pygame.init() settings = Settings() screen = pygame.display.set_mode( (settings.screenWidth, settings.screenHeight)) pygame.display.set_caption("Warehouse Game") lastAction = ["menu"] lastGraph = ["list"] pageNum = [0] warehouse = [5, 5, 5] # Testing, size is always 5, 5, 5 boxList = [] backOrder = [] objectList = [] createMenu.createBG(settings, objectList) createMenu.createMainMenu(settings, objectList) createMenu.getList(objectList, boxList, pageNum) img = pygame.image.load("warehouseLogo.png") repeat = True while repeat: screen.fill((0, 0, 0)) # sss += 1 # if sss == 20: # objectList.append(testHolder) # testHolder.printList() # if sss%500 == 0 and sss > 0: # bname.msg += str(sss%499) # print("-----------") # boxInfo = [0, 0, [2, 2, 2]] # pos = [0, 0, 0] # for h in range(pos[2], boxInfo[2][2] + pos[2]): # for l in range(pos[1], boxInfo[2][1] + pos[1]): # for w in range(pos[0], boxInfo[2][0] + pos[0]): # print(w, l, h) # print("-----------") i = 0 for item in objectList: i = item.layer if item.layer > i else i item.draw(screen) if i == 2: screen.blit(img, (460, 250)) gf.checkEvents(objectList, boxList, backOrder, warehouse, lastAction, lastGraph, pageNum) pygame.display.flip()
def showEndScreen(self): self.mapName = self.map5 self.new1() while self.stillPlaying: self.dt = self.clock.tick(settings.FPS) / 1000 gf.checkEvents(self, settings, self.sb, self.newPlayer, self.wallGroup, USEREVENT) self.update() self.draw(settings) if self.playerRestarts == True: self.stillPlaying = False elif self.playerDone == True: self.stillPlaying = False
def runGame(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screenWidth, ai_settings.screenHeight)) pygame.display.set_caption("Alien Invasion") ship = Ship(ai_settings, screen) aliens = Group() bullets = Group() queenBees = Group() queenBullets = Group() stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) gf.createFleet(ai_settings, screen, stats, sb, ship, aliens) playButton = Button(ai_settings, screen, "Play", 200, 50, 0) resumeButton = Button(ai_settings, screen, "Resume Game", 250, 50, 0) pygame.mixer.music.load('audioFiles/were going on a trip.wav') pygame.mixer.music.play(-1) # updates game every run through while True: gf.checkEvents(ai_settings, screen, stats, sb, playButton, ship, aliens, bullets, queenBees, queenBullets) if stats.gameActive: ship.update() gf.updateAliens(ai_settings, screen, stats, sb, ship, aliens, bullets, queenBees, queenBullets) gf.updateBullets(ai_settings, screen, stats, sb, ship, aliens, bullets, queenBees, playButton, queenBullets) gf.updateQueenBeeBullets(ai_settings, screen, stats, sb, ship, aliens, bullets, queenBees, playButton, queenBullets) gf.updateScreen(ai_settings, screen, stats, sb, ship, bullets, aliens, queenBees, queenBullets, playButton, resumeButton)
screen = pygame.display.set_mode((ai_settings.W, ai_settings.H)) sb = Scoreboard(ai_settings, screen) clock = pygame.time.Clock() runner = Runner(400, 300, 75, 75) # for increasing speed, setting up obstacles, setting up coins respectively pygame.time.set_timer(USEREVENT + 1, 500) pygame.time.set_timer(USEREVENT + 2, random.randrange(3000, 5000)) pygame.time.set_timer(USEREVENT + 3, 3000) run = True objects = [] coins = Group() while run: gf.checkEvents(runner, ai_settings) if ai_settings.gameActive: emptyObj = gf.updateScreen(ai_settings, screen, runner, objects, coins, sb) # for emptying the coins and obstacles if runner hits obstacle if emptyObj == True: objects = [] for x in coins: coins.remove(x) # for creating obstacles, coins, and increases speed for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() quit()