255) red_colour = pygame.Color(255, 0, 0) grey_colour = pygame.Color(150, 150, 150) # 定义游戏结束函数 def GameOver...(gamesurface): # 设置提示字体的格式 GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16) # 设置提示字体的颜色...GameOver_colour = GameOver_font.render('Game Over', True, grey_colour) # 设置提示位置 GameOver_location...= GameOver_colour.get_rect() GameOver_location.midtop = (320, 10) gamesurface.blit(GameOver_colour..., GameOver_location) # 提示运行信息 pygame.display.flip() # 等待5秒 time.sleep(5) # 退出游戏
= 0 gameover_max = 100 # 载入音乐 pygame.mixer.music.load("bg.mp3") pygame.mixer.music.play(-1) hitsound...- gameover) / 10) cd_sur = score_font.render( "重新关注我的龚总好吧:CoXie带你学编程开始倒计时{}".format...(cd), True, (255, 0, 0) ) sur.blit(cd_sur, (100, 200)) gameover = gameover...> gameover_max: pygame.mouse.set_visible(False) times = 0 score = 0...gameover = 0 效果展示以及素材获取 效果如下 ?
y: (current + 10) * height, width: boxes[current - 1].width }; } function gameOver...() { mode = 'gameOver'; context.fillText('Game over....= 'gameOver') { context.clearRect(0, 0, canvas.width, canvas.height); context.fillText...boxes[current - 1].x; if (Math.abs(difference) >= boxes[current].width) { gameOver...newBox(); debris.y = 0; } canvas.onpointerdown = function() { if (mode == 'gameOver
(): # 设置GameOver的显示颜色 GameOver_Surf = BASICFONT.render('Game Over!'..., True, GREY) # 设置GameOver的位置 GameOver_Rect = GameOver_Surf.get_rect() GameOver_Rect.midtop...= (320, 10) # 绑定以上设置到句柄 DISPLAY.blit(GameOver_Surf, GameOver_Rect) pygame.display.flip(...() if snake_Head[1]460: GameOver() # 贪吃蛇触碰到自己 for i in snake_Body[1:]: if...snake_Head[0]==i[0] and snake_Head[1]==i[1]: GameOver() 4 后记 本文利用Pygame模块实现了最简单的贪吃蛇版本。
= 0 gameover_max = 100 # 载入音乐 pygame.mixer.music.load("bg.mp3") pygame.mixer.music.play(-1) hitsound...- gameover) / 10) cd_sur = score_font.render( "重新开始倒计时{}".format(cd), True, (255..., 0, 0) ) sur.blit(cd_sur, (100, 200)) gameover = gameover + 1 else:...> gameover_max: pygame.mouse.set_visible(False) times = 0 score = 0...gameover = 0 效果展示以及素材获取 效果如下 [2639ncusmh.gif?
= pygame.image.load("images/gameover.png").convert_alpha() gameover_rect = gameover_image.get_rect...= gameover_font.render("Your Score", True, (255, 255, 255)) gameover_text1_rect = gameover_text1...- gameover_text1_rect.width) // 2, height // 3 screen.blit(gameover_text1, gameover_text1...gameover_text2_rect = gameover_text2.get_rect() gameover_text2_rect.left, gameover_text2_...< pos[0] < gameover_rect.right and \ gameover_rect.top < pos[1] < gameover_rect.bottom
=True 在main函数中,将原来的while True循环改成while not gameover;这样当gameover变成True时,就会退出游戏循环,游戏也就结束了,结束后加上pygame.quit...=True def display_frame(self,screen,font,gameover_font): screen.fill(bg) if self.gameover:...# 设置结束文本 gameover_text = gameover_font.render("Game Over", True, (255,0,0)) gamecontinue_text...def display_frame(self,screen,font,gameover_font): screen.fill(bg) if self.gameover: # 设置结束文本...gameover_text = gameover_font.render("Game Over", True, (255,0,0)) gamecontinue_text =font.render
restart_rect.x,restart_rect.y = (900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50 # 加载gameover...gameover = pygame.image.load("dino/gameover.png") gameover_rect = gameover.get_rect() gameover_rect.x..., gameover_rect.y = ( 900-gameover.get_rect().width)/2, (200-gameover.get_rect().height)/2 # 地面移动速度与距离...True # 设置重新再来图片 screen.blit(restart, restart_rect) screen.blit(gameover..., gameover_rect) pygame.display.update() score = 0 is_restart = False
restart_rect.x,restart_rect.y = (900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50 # 加载 gameover...gameover = pygame.image.load("dino/gameover.png") gameover_rect = gameover.get_rect() gameover_rect.x..., gameover_rect.y = ( 900-gameover.get_rect().width)/2, (200-gameover.get_rect().height)/2 # 地面移动速度与距离... = 10 # 设置重新再来图片 screen.blit(restart, restart_rect) screen.blit(gameover..., gameover_rect) pygame.display.update() 对于碰撞,只要恐龙碰撞到了仙人掌,那么游戏结束,展示重新再来图片 由于我们希望游戏可以记录我们的最好成绩
restart_rect.x,restart_rect.y = (900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50 # 加载 gameover...gameover = pygame.image.load("dino/gameover.png") gameover_rect = gameover.get_rect() gameover_rect.x..., gameover_rect.y = ( 900-gameover.get_rect().width)/2, (200-gameover.get_rect().height)/2 # 地面移动速度与距离...= 10 # 设置重新再来图片 screen.blit(restart, restart_rect) screen.blit(gameover..., gameover_rect) pygame.display.update() 对于碰撞,只要恐龙碰撞到了仙人掌,那么游戏结束,展示重新再来图片 由于我们希望游戏可以记录我们的最好成绩
(): # 设置GameOver的显示颜色 GameOver_Surf = BASICFONT.render(‘Game Over!’..., True, GREY) # 设置GameOver的位置 GameOver_Rect = GameOver_Surf.get_rect() GameOver_Rect.midtop = (320, 10...) # 绑定以上设置到句柄 DISPLAY.blit(GameOver_Surf, GameOver_Rect) pygame.display.flip() # 等待3秒 time.sleep(3) #...() if snake_Head[1]460: GameOver() # 贪吃蛇触碰到自己 for i in snake_Body[1:]: if snake_Head...[0]==i[0] and snake_Head[1]==i[1]: GameOver() 4 后记 本文利用Pygame模块实现了最简单的贪吃蛇版本。
而GameOver函数就是跳出这个循环的条件。...这里给出当蛇吃到自己身体或者碰到边界时显示的界面(判断死亡的代码会在之后展示) # 4、Game Over def gameOver(playSurface, score): gameOverFont...判断是否死亡 当蛇头超出边界或者蛇头与自己的蛇身重合时,蛇类死亡,调用GameOver。...if snakePosition[0] > 620 or snakePosition[0] < 0: gameOver(playSurface, score) if snakePosition[...1] > 460 or snakePosition[1] < 0: gameOver(playSurface, score) for snakeBody in snakeSegments[1:]
close clear clc global gameover global rect_in global map global blocks_x global blocks_y global block_width...blocks_y个方块 blocks_y = floor((bottom - top) / block_height); map = zeros(blocks_x,blocks_y); % 是否游戏结束的标记 gameover...start = datetime('now'); %设定一个时间、如果一直没成功就自然结束 while (datetime('now')-start<duration(0,0,600)) if (gameover...== 0) banner(); banner(); click_blank(); else gameover = 0;%点到雷重来...; win.key(113) win.leftClick; showmap(); end if all(all(map~=0)) && gameover
= pygame.image.load(cfg.OTHER_IMAGE_PATHS.get('gameover')) gameover_img = pygame.transform.scale(gameover_img..., (150, 75)) gameover_img_rect = gameover_img.get_rect() gameover_img_rect.midtop = cfg.WIDTH/2,...cfg.HEIGHT/8 gameover_flash_time = 25 gameover_flash_count = 0 gameover_show_flag = True # 游戏胜利与否的提示...+= 1 if gameover_flash_count > gameover_flash_time: gameover_show_flag = not gameover_show_flag...gameover_flash_count = 0 if gameover_show_flag: screen.blit(gameover_img, gameover_img_rect
在游戏主循环里,我们增加一个记录游戏是否结束的变量gameover。把之前的游戏逻辑放在gameover为False的情况下。而当checkCrash为True时,就把gameover设为True。...gameover = False while True: ### if not gameover: ###省略部分游戏逻辑 for e in enemies...: #如果撞上敌机,设gameover为True if checkCrash(e, plane): gameover =
为了让游戏结束后能方便地重新开始,我们再往事件响应的代码中增加一段处理: #判断在gameover状态下点击了鼠标 if gameover and event.type == pygame.MOUSEBUTTONUP...e in enemies: e.restart() for b in bullets: b.active = False score = 0 gameover...= False 当gameover状态下发生了鼠标按钮抬起的事件(即玩家点击了鼠标),我们就把本体和敌机都重置位置,子弹都设active为False,分数清零,gameover为False,游戏重新开始
子弹射击: 子弹射击 如上图:按下空格键,可实现子弹射击 GameOver gameover 敌机碰撞飞机后,游戏GameOver。
领取专属 10元无门槛券
手把手带您无忧上云