赞
踩
亲爱的读者们,今天我们要讨论,Python程序设计第三版清华出版社PDF Python程序设计第三版 董付国资源,一起探索吧!
这篇文章主要介绍了python简单编程小游戏,具有一定借鉴价值,需要的朋友可以参考下PHP,我学习路上的那盏灯。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。
哈喽铁子们
表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对?
比方说,可以通过打游戏来学编程!
今天给大家分享100个Python小游戏,一定要收藏!
飞机大战相信大家都玩过吧,非常简单有意思的游戏,咱们通过Python给它复刻出来,回味童年。
素材文件
全部源码:
- import sys
- import cfg
- import pygame
- from modules import *
-
-
- '''游戏界面'''
- def GamingInterface(num_player, screen):
- # 初始化
- (cfg.SOUNDPATHS['Cool Space Music'])
- pygame.mixer.music.set_volume(0.4)
- (-1)
- explosion_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['boom'])
- fire_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['shot'])
- font = (cfg.FONTPATH, 20)
- # 游戏背景图
- bg_imgs = [cfg.IMAGEPATHS['bg_big'], cfg.IMAGEPATHS['seamless_space'], cfg.IMAGEPATHS['space3']]
- bg_move_dis = 0
- bg_1 = (bg_imgs[0]).convert()
- bg_2 = (bg_imgs[1]).convert()
- bg_3 = (bg_imgs[2]).convert()
- # 玩家, 子弹和小行星精灵组
- player_group = pygame.sprite.Group()
- bullet_group = pygame.sprite.Group()
- asteroid_group = pygame.sprite.Group()
- # 产生小行星的时间间隔
- asteroid_ticks = 90
- for i in range(num_player):
- (Ship(i+1, cfg))
- clock = .Clock()
- # 分数
- score_1, score_2 = 0, 0
- # 游戏主循环
- while True:
- for event in ():
- if == :
- ()
- ()
- # --玩家一: ↑↓←→控制, j射击; 玩家二: wsad控制, 空格射击
- pressed_keys = .get_pressed()
- for idx, player in enumerate(player_group):
- direction = None
- if idx == 0:
- if pressed_keys[pygame.K_UP]:
- direction = 'up'
- elif pressed_keys[pygame.K_DOWN]:
- direction = 'down'
- elif pressed_keys[pygame.K_LEFT]:
- direction = 'left'
- elif pressed_keys[pygame.K_RIGHT]:
- direction = 'right'
- if direction:
- (direction)
- if pressed_keys[pygame.K_j]:
- if player.cooling_time == 0:
- ()
- (())
- player.cooling_time = 20
- elif idx == 1:
- if pressed_keys[pygame.K_w]:
- direction = 'up'
- elif pressed_keys[pygame.K_s]:
- direction = 'down'
- elif pressed_keys[pygame.K_a]:
- direction = 'left'
- elif pressed_keys[pygame.K_d]:
- direction = 'right'
- if direction:
- (direction)
- if pressed_keys[pygame.K_SPACE]:
- if player.cooling_time == 0:
- ()
- (())
- player.cooling_time = 20
- if player.cooling_time > 0:
- player.cooling_time -= 1
- if (score_1 + score_2) < 500:
- background = bg_1
- elif (score_1 + score_2) < 1500:
- background = bg_2
- else:
- background = bg_3
- # --向下移动背景图实现飞船向上移动的效果
- (background, (0, -background.get_rect().height + bg_move_dis))
- (background, (0, bg_move_dis))
- bg_move_dis = (bg_move_dis + 2) % background.get_rect().height
- # --生成小行星
- if asteroid_ticks == 0:
- asteroid_ticks = 90
- (Asteroid(cfg))
- else:
- asteroid_ticks -= 1
- # --画飞船
- for player in player_group:
- if pygame.sprite.spritecollide(player, asteroid_group, True, None):
- player.explode_step = 1
- ()
- elif player.explode_step > 0:
- if player.explode_step > 3:
- player_group.remove(player)
- if len(player_group) == 0:
- return
- else:
- player.explode(screen)
- else:
- (screen)
- # --画子弹
- for bullet in bullet_group:
- ()
- if pygame.sprite.spritecollide(bullet, asteroid_group, True, None):
- bullet_group.remove(bullet)
- if bullet.player_idx == 1:
- score_1 += 1
- else:
- score_2 += 1
- else:
- (screen)
- # --画小行星
- for asteroid in asteroid_group:
- ()
- asteroid.rotate()
- (screen)
- # --显示分数
- score_1_text = '玩家一得分: %s' % score_1
- score_2_text = '玩家二得分: %s' % score_2
- text_1 = font.render(score_1_text, True, (0, 0, 255))
- text_2 = font.render(score_2_text, True, (255, 0, 0))
- (text_1, (2, 5))
- (text_2, (2, 35))
- # --屏幕刷新
- pygame.display.update()
- (60)
-
-
- '''主函数'''
- def main():
- ()
- ()
- ()
- screen = pygame.display.set_mode(cfg.SCREENSIZE)
- pygame.display.set_caption('简易版——飞机大战 ')
- num_player = StartInterface(screen, cfg)
- if num_player == 1:
- while True:
- GamingInterface(num_player=1, screen=screen)
- EndInterface(screen, cfg)
- else:
- while True:
- GamingInterface(num_player=2, screen=screen)
- EndInterface(screen, cfg)
-
-
- '''run'''
- if __name__ == '__main__':
- main()

都是一些大家耳熟能详的小游戏
素材
全部源码:
- import os
- import sys
- import cfg
- import pygame
- from modules import *
-
-
- '''游戏主程序'''
- def main():
- ()
- screen = pygame.display.set_mode(cfg.SCREENSIZE)
- pygame.display.set_caption('开心消消乐声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/871133?site推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。