python --pygame 游戏之 飞剑骑士
必备知识:python 类 的使用
python -- pygame 的基本用法
# encoding : utf-8
# anthor : comi
# date :2018/08/15import pygame,sys
from pygame.locals import *
from random import randintclass Player(pygame.sprite.Sprite ):# sprite for the Playerdef __init__(self):pygame.sprite.Sprite.__init__(self) # 精灵初始化# self.image = pygame.Surface ((50,50)) # 设置精灵大小# self.image.fill(green) # 精灵颜色self.image = pygame.image.load(r'C:\Users\User\Desktop\py-pro\test\img\block.png').convert_alpha()self.rect = self.image.get_rect() # 图片方形self.rect.centerx = width / 2self.rect.bottom = height - 10def update(self):key_pressed = pygame.key.get_pressed()if key_pressed[pygame.K_LEFT]:self.rect.x -= 10if key_pressed[pygame.K_RIGHT]:self.rect.x += 10if self.rect.x < 0:self.rect.x = 0if self.rect.x >= width - 50:self.rect.x = width - 50def shoot(self):bullet = Bullets(self.rect.centerx, self.rect.top)Bullet_groups.add(bullet)class Block(pygame.sprite.Sprite):# sprite for blockdef __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load(r'C:\Users\User\Desktop\py-pro\test\img\enemy.png').convert_alpha()#self.image.fill(Red)self.rect = self.image.get_rect()self.rect.centerx = randint(50, width-50)self.rect.centery = randint(-100,-50)self.speedx = randint( -2,2)self.speedy = randint(5, 10)def update(self):self.rect.y += self.speedyself.rect.x += self.speedxif self.rect.x < 0:self.rect.x = 0if self.rect.x > width - 50:self.rect.x = width - 50if self.rect.y > height - 40:self.rect.centerx = randint(0, width - 50)self.rect.centery = 0self.speedy = randint(0, 8)class Bullets(pygame.sprite.Sprite):def __init__(self,x,y):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load(r'C:\Users\User\Desktop\py-pro\test\img\sword.png').convert_alpha()#self.image.fill(Yellow)self.rect = self.image.get_rect()self.rect.centerx = xself.rect.bottom = yself.speedy = -10def update(self):self.rect.y += self.speedyif self.rect.bottom < 0:self.kill() # deletedef MBox():text = pygame.font.Font('freesansbold.ttf', 32)text_surface = text.render("score:", True, (0, 0, 0), (255, 255, 255))screen.blit(text_surface,(width/2,0))# initwidth = 800
height = 600
pygame.init()
pygame.mixer.init()
pygame.display.set_caption('飞剑骑士')
screen = pygame.display.set_mode((width,height),0,32)
FPS = 60
FPSClock = pygame.time.Clock()# color
white = (255,255,255)
black = (0,0,0)
green = (0,200,0)
Red = (255,0,0)
Yellow = (255,255,0)def Add_Thing():# create player,block and bulletsplayer_groups = pygame.sprite.Group() # 创建玩家组Block_groups = pygame.sprite.Group() # 创建砖块组Bullet_groups = pygame.sprite.Group() # 创建子弹组for i in range(5): # 砖块个数block = Block() # 创建 砖块Block_groups.add(block) # 添加砖块player = Player() # 创建玩家player_groups.add(player) # 添加玩家return player_groups, Block_groups,player,Bullet_groupsdef gameloop(player_groups,Block_groups,player,Bullet_groups):score = 0pygame.mixer.music.load(r'C:\Users\User\Desktop\py-pro\test\sound\backmusic.ogg')pygame.mixer.music.play(-1)background = pygame.image.load(r'C:\Users\User\Desktop\py-pro\test\img\back1.jpg').convert()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:player.shoot()# updateplayer_groups.update()Block_groups.update()Bullet_groups.update()# if Collisions block with bullets,breakhits = pygame.sprite.groupcollide(Block_groups,Bullet_groups ,True,True) # deletefor hit in hits:block = Block() # 创建砖块Block_groups.add(block) # 添加砖块score += 1print(score)# if Collisions,breakhits = pygame.sprite.spritecollide(player,Block_groups,False) # back a listsif hits:pygame.quit()sys.exit()# Draw#screen.fill(white) # 背景颜色screen.blit(background, (0, 0))#MBox()player_groups.draw(screen) # 画出方块Block_groups.draw(screen) # 画出障碍物Bullet_groups.draw(screen) # 画出子弹# updatepygame.display.update()FPSClock.tick(FPS)if __name__ == '__main__':player_groups,Block_groups,player,Bullet_groups= Add_Thing()gameloop(player_groups,Block_groups,player,Bullet_groups)
效果图如下:
素材如下:
音乐链接如下:https://opengameart.org/content/drama-drama-drama
欢迎大家点评交流
python --pygame 游戏之 飞剑骑士相关推荐
- python编写游戏怎么打包——详解python+pygame游戏开发之使用Py2exe打包游戏为exe文件
python编写游戏怎么打包 1.简介 2.软件准备 3.打包流程 最近在用python+pygame 开发游戏,写完以后在分享给朋友玩的时候遇到了很大的问题,只有搭建了环境才能运行python脚本. ...
- python pygame 游戏实践: 俄罗斯方块(Tetris Game)第一步
正在学习俄罗斯方块(Tetris Game)游戏, 主要参考Tetris with PyGame | Python Assets,不过有所修改,原作的class 太复杂, 不好理解, 试图用自己习惯的 ...
- Python——pygame游戏:外星人入侵(打飞机)
游戏附件&代码 链接: https://pan.baidu.com/s/1kUrISCFiptsnr-tdYy7IzQ 提取码: vjcx 游戏画面 音乐 背景音乐:<MEGALOBOX ...
- python pygame 游戏实战: 2048 game 游戏简化版
2048游戏的算法参考:2048 Game in Python - GeeksforGeeks 以下是2048游戏的简化版, 在python 3.9运行没有问题. 没有用精美的图像, 主要是原理的实现 ...
- 《空洞机甲》编程赛__自制 Python Pygame 游戏
gitee地址 代码过多,可以去我的资源里下载 图片资源等都在资源里,只需积分下载即可,遇到无法下载可以加我QQ273309802 这里附上连接 https://download.csdn.net/d ...
- python小游戏之三
猜拳游戏 Python代码实现猜拳小游戏 Python代码实现猜拳小游戏_zhangtongyuan0909的博客-CSDN博客_python猜拳游戏代码 用python中类与对象写一个猜拳游戏 用p ...
- 《Python和Pygame游戏开发指南》——2.16 pygame.display.update()函数
本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第2章,第2.16节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...
- 《Python和Pygame游戏开发指南》——1.12 图书中的文本折行
本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第1章,第1.12节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...
- 【Python笔记】pygame 游戏框架
pygame 游戏框架 初识 pygame 安装 pygame pygame 常用模块 pygame 的基本使用(跳跃的小球游戏) 初识 pygame pygame 是跨平台 Python 模块,专为 ...
最新文章
- ef关联多实体查询_Mybatis基本知识十二:关联关系查询之延迟加载:侵入式延迟加载...
- 激战服务器位置,《激战2》世界之战指南(选择服务器)
- electron调试html,electron桌面应用程序开发入门
- 12 种主流编程语言输出“ Hello World ”,把我给难住了!
- 卸载centos7自带mysql_centos7完全卸载mysql
- postman delete 请求传递数组_Postman请求方法
- android单元测试作用,Android单元测试源码解读
- 卡特兰(Catalan)数列
- 查看python安装的库_查看python及其第三方库的版本和安装位置
- html5 教程网站
- 7.微服务设计 --- 测试
- SourceAnalysis/Binder源码分析.md
- Web APIs概念详解(附图解)
- 计算机实验环境怎么写visual,visualfoxpro计算机实验报告答案
- 基于docker的wekan部署
- 实验室的温湿度要求及其控制措施的详细讲解
- vivado hls 笔记
- 用Vue实现小Q聊天机器人(一)
- 《2021/07/24》1 -- linux -- 网络名称空间和网桥的基本操作
- B站 bilibili 视频、分区、up主数据合集