中秋将至利用python画一些月饼从天而降不用买了
目录
- 导语
- 正文
- ??总结
文章图片
导语 好消息!下一个假期已经在路上了,正在向我们招手呢!
大家只要再坚持5天
就能迎来中秋小长假啦~
文章图片
?“海上生明月,天涯共此时”
又是一年中秋至!快跟着小编来看看怎么寓教于乐吧~~
今天带大家编写一款应时应景的中秋小游戏!
天上掉月饼啦~天上掉月饼啦~天上掉月饼啦~
文章图片
正文 ?准备好相应的素材如下:
?
文章图片
环境安装:
Python3.6、pycharm2021、游戏模块Pygame。
安装:pip installpygame
?初始化游戏加载素材:
def initGame(): pygame.init()screen = pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('中秋月饼小游戏') game_images = {}for key, value in cfg.IMAGE_PATHS.items():if isinstance(value, list):images = []for item in value: images.append(pygame.image.load(item))game_images[key] = imageselse:game_images[key] = pygame.image.load(value)game_sounds = {}for key, value in cfg.AUDIO_PATHS.items():if key == 'bgm': continuegame_sounds[key] = pygame.mixer.Sound(value) return screen, game_images, game_sounds
主函数定义:
def main():# 初始化screen, game_images, game_sounds = initGame()# 播放背景音乐pygame.mixer.music.load(cfg.AUDIO_PATHS['bgm'])pygame.mixer.music.play(-1, 0.0)# 字体加载font = pygame.font.Font(cfg.FONT_PATH, 40)# 定义herohero = Hero(game_images['hero'], position=(375, 520))# 定义掉落组food_sprites_group = pygame.sprite.Group()generate_food_freq = random.randint(10, 20)generate_food_count = 0# 当前分数/历史最高分score = 0highest_score = 0 if not os.path.exists(cfg.HIGHEST_SCORE_RECORD_FILEPATH) else int(open(cfg.HIGHEST_SCORE_RECORD_FILEPATH).read())# 游戏主循环clock = pygame.time.Clock()while True:# --填充背景screen.fill(0)screen.blit(game_images['background'], (0, 0))# --倒计时信息countdown_text = 'Count down: ' + str((90000 - pygame.time.get_ticks()) // 60000) + ":" + str((90000 - pygame.time.get_ticks()) // 1000 % 60).zfill(2)countdown_text = font.render(countdown_text, True, (0, 0, 0))countdown_rect = countdown_text.get_rect()countdown_rect.topright = [cfg.SCREENSIZE[0]-30, 5]screen.blit(countdown_text, countdown_rect)# --按键检测for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()key_pressed = pygame.key.get_pressed()if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:hero.move(cfg.SCREENSIZE, 'left')if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:hero.move(cfg.SCREENSIZE, 'right')# --随机生成generate_food_count += 1if generate_food_count > generate_food_freq:generate_food_freq = random.randint(10, 20)generate_food_count = 0food = Food(game_images, random.choice(['gold',] * 10 + ['apple']), cfg.SCREENSIZE)food_sprites_group.add(food)# --更新掉落for food in food_sprites_group:if food.update(): food_sprites_group.remove(food)# --碰撞检测for food in food_sprites_group:if pygame.sprite.collide_mask(food, hero):game_sounds['get'].play()food_sprites_group.remove(food)score += food.scoreif score > highest_score: highest_score = score# --画herohero.draw(screen)# --画food_sprites_group.draw(screen)# --显示得分score_text = f'Score: {score}, Highest: {highest_score}'score_text = font.render(score_text, True, (0, 0, 0))score_rect = score_text.get_rect()score_rect.topleft = [5, 5]screen.blit(score_text, score_rect)# --判断游戏是否结束if pygame.time.get_ticks() >= 90000:break# --更新屏幕pygame.display.flip()clock.tick(cfg.FPS)# 游戏结束, 记录最高分并显示游戏结束画面fp = open(cfg.HIGHEST_SCORE_RECORD_FILEPATH, 'w')fp.write(str(highest_score))fp.close()return showEndGameInterface(screen, cfg, score, highest_score)
定义月饼、灯笼等掉落:
?
文章图片
import pygameimport random class Food(pygame.sprite.Sprite):def __init__(self, images_dict, selected_key, screensize, **kwargs):pygame.sprite.Sprite.__init__(self)self.screensize = screensizeself.image = images_dict[selected_key]self.mask = pygame.mask.from_surface(self.image)self.rect = self.image.get_rect()self.rect.left, self.rect.bottom = random.randint(20, screensize[0]-20), -10self.speed = random.randrange(5, 10)self.score = 1 if selected_key == 'gold' else 5'''更新食物位置'''def update(self):self.rect.bottom += self.speedif self.rect.top > self.screensize[1]:return Truereturn False
定义接月饼的人物:
import pygame class Hero(pygame.sprite.Sprite):def __init__(self, images, position=(375, 520), **kwargs):pygame.sprite.Sprite.__init__(self)self.images_right = images[:5]self.images_left = images[5:]self.images = self.images_right.copy()self.image = self.images[0]self.mask = pygame.mask.from_surface(self.image)self.rect = self.image.get_rect()self.rect.left, self.rect.top = positionself.diretion = 'right'self.speed = 8self.switch_frame_count = 0self.switch_frame_freq = 1self.frame_index = 0'''左右移动hero'''def move(self, screensize, direction):assert direction in ['left', 'right']if direction != self.diretion:self.images = self.images_left.copy() if direction == 'left' else self.images_right.copy()self.image = self.images[0]self.diretion = directionself.switch_frame_count = 0self.switch_frame_count += 1if self.switch_frame_count % self.switch_frame_freq == 0:self.switch_frame_count = 0self.frame_index = (self.frame_index + 1) % len(self.images)self.image = self.images[self.frame_index]if direction == 'left':self.rect.left = max(self.rect.left-self.speed, 0)else:self.rect.left = min(self.rect.left+self.speed, screensize[0])'''画到屏幕上'''def draw(self, screen):screen.blit(self.image, self.rect)
游戏结束界面:设置的一分30秒结束接到多少就是多少分数。
?
文章图片
import sysimport pygame '''游戏结束画面'''def showEndGameInterface(screen, cfg, score, highest_score):# 显示的文本信息设置font_big = pygame.font.Font(cfg.FONT_PATH, 60)font_small = pygame.font.Font(cfg.FONT_PATH, 40)text_title = font_big.render(f"Time is up!", True, (255, 0, 0))text_title_rect = text_title.get_rect()text_title_rect.centerx = screen.get_rect().centerxtext_title_rect.centery = screen.get_rect().centery - 100text_score = font_small.render(f"Score: {score}, Highest Score: {highest_score}", True, (255, 0, 0))text_score_rect = text_score.get_rect()text_score_rect.centerx = screen.get_rect().centerxtext_score_rect.centery = screen.get_rect().centery - 10text_tip = font_small.render(f"Enter Q to quit game or Enter R to restart game", True, (255, 0, 0))text_tip_rect = text_tip.get_rect()text_tip_rect.centerx = screen.get_rect().centerxtext_tip_rect.centery = screen.get_rect().centery + 60text_tip_count = 0text_tip_freq = 10text_tip_show_flag = True# 界面主循环clock = pygame.time.Clock()while True:screen.fill(0)for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_q:return Falseelif event.key == pygame.K_r:return Truescreen.blit(text_title, text_title_rect)screen.blit(text_score, text_score_rect)if text_tip_show_flag:screen.blit(text_tip, text_tip_rect)text_tip_count += 1if text_tip_count % text_tip_freq == 0:text_tip_count = 0text_tip_show_flag = not text_tip_show_flagpygame.display.flip()clock.tick(cfg.FPS)
游戏界面:
???
文章图片
?
文章图片
??总结 好啦!今天的游戏更新跟中秋主题一次性写完啦!嘿嘿~机智如我!???
文章图片
【中秋将至利用python画一些月饼从天而降不用买了】到此这篇关于中秋将至利用python画一些月饼从天而降不用买了的文章就介绍到这了,更多相关python 中秋 月饼 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 利用python将文本文件转换成mp3--听书
- 如何利用|如何利用 RPA 实现自动化获客?
- 利用Qt实现可扩展对话框的示例代码
- 移动互联网|监管风暴将至!互联网巨头卧榻不保?
- 【翻译】驯服野兽(Scylla|【翻译】驯服野兽:Scylla 如何利用控制理论来控制压实)
- 企业信息化|利用虚拟摄像头解决腾讯会议多人同时共享屏幕的需求
- Android技术分享| 利用Kotlin协程,多任务并行,测试RTM SDK性能
- 利用盛科设备搭建BGP+EVPN实现VXLAN二层通道
- Android利用Hero实现列表与详情页无缝切换动画
- big|利用Python数据分析处理进行炒股实战行情