简介好消息!下一个假期已经临近并在向我们招手。如果大家接下来的5天努力的话,我们一定能过中秋节的~
“明月照亮大海,与天涯海角共享这一刻,又到了中秋节了,跟着我们的小编,看看我们如何寓教于乐~~今天我就教大家如何写作!”适合中秋节的游戏!
正文准备相应材料如下:
环境安装:Python3.6、pycharm2023、游戏模块Pygame。
安装:pip install pygame 初始化游戏并加载素材:
def initGame(): pygame.init() screen=pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption(\’中秋月饼游戏\’) game_images={} key, cfg.IMAGE_PATHS.items 的值(第:章:第:章) game_sounds={} for key, value in cfg.AUDIO_PATHS.items(): if key==\’bgm\’: continue game_sounds[key]=pygame.mixer.Sound(value) return screen, game_images, game_sounds 主要函数含义:
def main(): # 初始化屏幕, 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) # 定义英雄Hero=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=0most_score=0 if not os.path.exists(cfg.HIGHEST_SCORE_RECORD_FILEPATH) else int(open(cfg.HIGHEST_SCORE_RECORD_FILEPATH) . read ()) # 游戏主循环时钟=pygame.time.Clock() while True: # –填充背景screen.fill(0) screen.blit(game_images[\’background\’], (0, 0)) # -倒计时信息countdown_text=\’倒计时:\’ + 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] blit (countdown_text) , countdown_rect) # — pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() sys.exit() key_pressed=pygame.key.get_pressed() if event key 检测key_pressed[pygame. K_a] 或key_pressed[pygame.K_LEFT]: Hero.move(cfg.SCREENSIZE, \’左\’) if key_pressed[pygame.K_d] 或key_pressed[pygame.K_RIGHT]: Hero.move(cfg.SCREENSIZE, \’右\’) # — 随机生成generate_food_count +=1 ifgenerate_food_countgenerate_food_freq:generate_food_freq=random.randint(10, 20)generate_food_count=0 food=Food(game_images,random.choice([\’gold\’,] * 10 + [\’apple \’]), cfg.SCREENSIZE ) food_sprites_group.add(food) # — 如果food.update():food_sprites_group: 的食物更新将被删除food_sprites_group.remove(food) # — 如果pygame.sprite,food_sprites_group: 的食物碰撞检测。 collide_mask(food, Hero): game_sounds[\’get\’].play() food_sprites_group.remove(food) Score +=food.score if Scoremost_score:mostest_score=Score # –绘制英雄Hero.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=core_text.get_rect ( ) 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) 定义月饼、灯笼等掉落物。
导入pygameimport randomclass Food(pygame.sprite.Sprite): def __init__(self, images_dict, selected_key, screensize, **kwargs): pygame.sprite.Sprite.__init__(self) self.screensize=屏幕尺寸self.image=image_dict[ selected_key] ] self.mask=pygame.mask.from_surface(self.image) self.rect=self.image.get_rect() self.rect.left, self.rect.bottom=randint(20, screensize[0]-20 ) ), -10 self.speed=random.randrange(5, 10) self.score=1 if selected_key==\’gold\’ else 5 \’\’\’更新食物位置\’\’\’ def update(self): self. Bottom +=self.speed if self.rect.top self.screensize[1]: return True return False 定义将接收月饼的角色。
导入pygameclass 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=image[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=位置self.direction=\’右\’ self.speed=8 self.switch_frame_count=0 self.switch_frame_freq=1 self.frame_index=0 \’\’\’英雄向左使用right\’ \’\’ def move(self, screensize, Direction): [\’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=方向self.switch_frame_count=0 self.switch_frame_count +=1 if self.switch_frame_count % self.switch_frame_freq==0: self. switch_frame_count=0 self .frame_index=(self.frame_index + 1) % len(self.images) self.image=self.images[self.frame_index] 如果方向==\’left\’: self.rect.left=max(self .rect .left -self.speed, 0) else: self.rect.left=min(self.rect.left+self.speed, screensize[0]) \’\’\’\’在屏幕上绘制\’\’\’\’ defdraw(self, screen) : screen .blit(self.image, self.rect) 游戏结束界面: 1分30秒的那组比赛结束时得到的分数就是数字。
import sysimport pygame\’\’\’游戏结束画面\’\’\’def showEndGameInterface(screen, cfg,score,most_score): # 文本信息显示设置font_big=pygame.font.Font(cfg.FONT_PATH, 60) font_small=pygame.font. (cfg.FONT_PATH, 40) text_title=font_big.render(f\’时间到了!\’, True, (255, 0, 0)) text_title_rect=text_title.get_rect() text_title_rect.centerx=screen.get_rect().centerx text_title_rect 。 centery=screen.get_rect().centery – 100 text_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().centerx text_score_rect.centery=screen.get_rect().centery – 10 text_tip=font_small.render(f\’请输入Q 退出游戏,输入R 重新启动游戏\’, True, (255 , 0 , 0)) text_tip_rect=text_tip.get_rect() text_tip_rect.centerx=screen.get_rect().centerx text_tip_rect.centery=screen.get_rect().centery + 60 text_tip_count=0 text_tip_freq=10 text_tip_show_flag=True #界面主循环Clock=pygame.time.Clock() while True: screen.fill(0) for events in pygame.event.get() : ifevent.type==pygame.QUIT: pygame.quit( ) sys.exit() elifevent.type==pygame.KEYDOWN: ifevent.key==pygame.K_q: 返回False elifevent.key==pygame.K_r: 返回True screen.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 +=1 if text_tip_count % text_tip_freq==0: text_tip_count=0 text_tip_show_flag=text_tip_show_flag not pygame.display.flip() Clock.tick(cfg.FPS) 游戏界面:
这就是总结!今天的游戏更新和中秋节主题我一下子就写出来了!呵呵~跟我一样机智啊!制作起来并不容易,但是记得连续点击三下。 如果需要打包完整源码+素材压缩包:免费获取Python初学者安装包、免费激活码等Python信息,源码库:#privatemessage editor06#免费。
本文和图片来自网络,不代表火豚游戏立场,如若侵权请联系我们删除:https://www.huotun.com/game/597497.html