python --pygame 游戏之 飞剑骑士

必备知识:python 类 的使用

                  python -- pygame 的基本用法

# encoding : utf-8
# anthor : comi
# date :2018/08/15

import pygame,sys
from pygame.locals import *
from random import randint

class Player(pygame.sprite.Sprite ):
    # sprite for the Player
    def __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 / 2
        self.rect.bottom = height - 10

    def update(self):
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            self.rect.x -= 10
        if key_pressed[pygame.K_RIGHT]:
            self.rect.x += 10
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.x >= width - 50:
            self.rect.x = width - 50

    def shoot(self):
        bullet = Bullets(self.rect.centerx, self.rect.top)
        Bullet_groups.add(bullet)

class Block(pygame.sprite.Sprite):
    # sprite for block
    def __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.speedy
        self.rect.x += self.speedx
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.x > width - 50:
            self.rect.x = width - 50
        if self.rect.y > height - 40:
            self.rect.centerx = randint(0, width - 50)
            self.rect.centery = 0
            self.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 = x
        self.rect.bottom = y
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()         # delete

def 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))

# init

width = 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 bullets
    player_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_groups

def gameloop(player_groups,Block_groups,player,Bullet_groups):
    score = 0
    pygame.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()
        # update
        player_groups.update()
        Block_groups.update()
        Bullet_groups.update()

        # if Collisions block with bullets,break
        hits = pygame.sprite.groupcollide(Block_groups,Bullet_groups ,True,True)  # delete
        for hit in hits:
            block = Block()  # 创建砖块
            Block_groups.add(block)  # 添加砖块
            score += 1
            print(score)

        # if Collisions,break
        hits = pygame.sprite.spritecollide(player,Block_groups,False)  # back a lists
        if 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)  # 画出子弹
        # update
        pygame.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

欢迎大家点评交流

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Pygame游戏设计是使用Python编程语言和Pygame模块来进行游戏开发的一种方式。Pygame是一个专为电子游戏设计的Python模块,它提供了丰富的功能和工具,可以帮助开发者轻松地创建2D游戏。通过Pygame,开发者可以处理图形、音效、输入设备和游戏逻辑等方面的功能。 在Python游戏设计,使用Pygame可以轻松创建各种类型的游戏,包括贪吃蛇、飞机大战等。通过Pygame提供的功能,开发者可以实现游戏的界面设计、用户交互、游戏逻辑和音效等方面的功能。同时,Pygame还提供了一些实用的工具和函数,方便开发者进行游戏开发和调试。 如果你对Python游戏设计感兴趣,可以参考以下步骤: 1. 安装Pygame模块:在官方网站https://www.pygame.org/上下载Pygame模块,并按照官方文档的说明进行安装。 2. 导入Pygame模块:在Python代码导入Pygame模块,以便可以使用它提供的功能和工具。 3. 创建游戏窗口:使用Pygame创建一个游戏窗口,设置窗口的大小和标题等属性。 4. 处理用户输入:使用Pygame监听用户的键盘和鼠标输入,并根据用户的操作进行相应的响应和处理。 5. 绘制游戏元素:使用Pygame提供的绘图函数和工具,在游戏窗口绘制游戏元素,如精灵、背景和特效等。 6. 更新游戏逻辑:在游戏循环,根据游戏的逻辑进行状态更新和处理,包括碰撞检测、计分和游戏结束等。 7. 播放音效和音乐:使用Pygame提供的音效和音乐功能,为游戏添加声音效果和背景音乐。 8. 调试和优化:在开发过程,通过调试和优化,确保游戏的正常运行和流畅性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值