http://blog.sysuschool.com/u/mygod/index.html
请稍候,载入中。。。
 
请稍候,载入中。。。
2019/5/15 8:22:00
博文:pygame续(四)理理游戏逻辑

 

卡在-will的游戏(飞机)代码几天了,有个错误还没找出来(敌机出现没有随机,而且调试没有反应,郁闷),代码参照-will博客写的,肯定是一个容易忽略的错误,程序也不报错,游戏也能运行,但敌机排成一条线出现在x的0位置,呵呵。

这个错误慢慢找,先理理-will写这个游戏的逻辑,如果自己写这个游戏,该怎么做?看别人写的代码,哪怕附加说明,一步步写下来,最后面对几百行代码,发现逻辑混乱得很。顺便提下,就算自己写的,过段时间,也有这感觉,想想寒假前辅导学生的网页作品《天台上的气象站》,写了较多的js程序代码,现在再看这些代码,很怀疑自己当初怎么写下来的。

-will博客的飞机游戏:用键盘方向键控制角色hero上下左右移动,同时不停发射子弹,敌机从屏幕上方出现(定时出现,位置左右随机)向下移动,敌机不能碰子弹和hero不能碰敌机。

所需对象:英雄飞机hero、子弹bullet、敌机enemy,其中子弹和敌机需要精灵组,需要定义基于精灵的类(pygame.sprite.Sprite),Hero类、Bullet类和Enemy类。

内容有点多,分开理,先看hero,定义Hero类,创建hero对象,调用hero对象的方法。

1、角色飞机类Hero:创建hero需传参hero_surface(英雄飞机)和hero_init_

pos(初始位置),hero对象属性有image(surface)、rect(位置)、speed(速度),bullet_group(子弹精灵组),定义move方法和single_shoot方法

class Hero(pygame.sprite.Sprite):

    def __init__(self,hero_surface,hero_init_pos):

        # pygame.sprite.Sprite.__init__(self)

        super().__init__()

        self.image = hero_surface

        self.rect = self.image.get_rect()

        self.rect.topleft = hero_init_pos

        self.speed = 5

 

        self.bullet_group = pygame.sprite.Group()

 

    def move(self,offset):

        x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]

        y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]

 

        if x < 0:

            self.rect.left = 0

        elif x > SCREEN_WIDTH - self.rect.width:

            self.rect.left = SCREEN_WIDTH - self.rect.width

        else:

            self.rect.left = x

 

        if y < 0:

            self.rect.top = 0

        elif y > SCREEN_HEIGHT - self.rect.height:

            self.rect.top = SCREEN_HEIGHT - self.rect.height

        else:

            self.rect.top = y

 

    def single_shoot(self,bullet_surface):

        bullet = Bullet(bullet_surface,self.rect.midtop)

        self.bullet_group.add(bullet)

——类中先继承父对象的初始化方法,并定义属性image(传入的hero_surface)、rect(通过get_rect获取)、修改rect.topleft(传入的位置列表)、speed(速度)、bullet_group(子弹精灵组)

——类中move方法,在主程序循环中调用,传入offset,

注:在主程序中定义的字典变量,各键key对应值根据按键情况取,初始各值均为0

offset = {pygame.K_LEFT:0,pygame.K_RIGHT:0,pygame.K_DOWN:0,pygame.K_UP:0}

 

在循环的事件遍历中,根据按键(方向键),给offset[key]进行取值,按下取2,弹起归0,如下

 

for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            exit()

 

        if event.type == pygame.KEYDOWN:

            if event.key in offset:

                offset[event.key] = 2

        elif event.type == pygame.KEYUP:

            if event.key in offset:

                offset[event.key] = 0

 

move方法代码如上

 

——类中single_shoot方法,发射子弹,传入bullet_surface参数,子弹位置不用传入,子弹创建时的位置应该是hero的midtop位置,方法中利用Bullet类创建bullet对象(传入bullet_surface),并将bullet对象加入hero的bullet_group精灵组

 

single_shoot方法代码如上。

 

Bullet子弹类的定义类似Hero类,代码如下:

 

class Bullet(pygame.sprite.Sprite):

    def __init__(self,bullet_surface,bullet_init_pos):

        pygame.sprite.Sprite.__init__(self)

        self.image = bullet_surface

        self.rect = self.image.get_rect()

        self.rect.topleft = bullet_init_pos

        self.speed = 8

 

    def update(self):

        self.rect.top -= self.speed

        if self.rect.top < -self.rect.height:

            self.kill()

 

——子弹类中的update方法,将会被精灵组bullet_group的update方法调用,定义了子弹对象的rect.top(即y坐标)的变化,和子弹飞出屏幕的处理(调用kill)

 

2、主程序中的调用:

 

用Hero类,创建hero对象(传参hero_surface和pos),其中参数surface有两种状态,创建hero对象时先用hero_surface[0]

 

hero_surface = []

hero_surface.append(shoot_img.subsurface(pygame.Rect(0,0,102,126)))

hero_surface.append(shoot_img.subsurface(pygame.Rect(102,0,102,126)))

hero_pos = [200,500]

 

hero = Hero(hero_surface[0],hero_pos)

 

子弹bullet_surface

 

bullet_surface = pygame.image.load("images/bullet1.png")

 

主循环中,用计数循环调用hero_surface[0]和hero_surface[1](参考前博文),用计数定时调用hero的single_shoot方法发射子弹

 

while True:

 

    ...

 

    if ticks >= ANIMATE_CYCLE:     # ANIMATE_CYCLE变量取值30

        ticks = 0

 

    hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]

 

    screen.blit(hero.image,hero.rect)

 

     if ticks % 10 == 0:

        hero.single_shoot(bullet_surface)

——注意hero非精灵,用screen.blit绘制到屏幕(注意是循环中重复绘制)

在循环的最后调用move方法,还有需要调用bullet_group的update方法和draw方法

    hero.bullet_group.update()

    hero.bullet_group.draw(screen)

 

    pygame.display.update()

  

    hero.move(offset)

完整代码如下(hero的speed属性没有用!)

import pygame

from pygame.locals import *

from sys import exit

 

class Hero(pygame.sprite.Sprite):

    def __init__(self,hero_surface,hero_init_pos):

        super().__init__()

        self.image = hero_surface

        self.rect = self.image.get_rect()

        self.rect.topleft = hero_init_pos

        # self.speed = 5

        self.bullet_group = pygame.sprite.Group()

 

    def move(self,offset):

        self.rect.left += offset[pygame.K_RIGHT]-offset[pygame.K_LEFT]

        self.rect.top += offset[pygame.K_DOWN]-offset[pygame.K_UP]

 

        if self.rect.left < 0:

            self.rect.left = 0

        elif self.rect.left > width - self.rect.width:

            self.rect.left = width - self.rect.width

 

        if self.rect.top < 0:

            self.rect.top = 0

        elif self.rect.top > height - self.rect.height:

            self.rect.top = height -self.rect.height

 

    def single_shoot(self,bullet_surface):

        bullet = Bullet(bullet_surface,self.rect.midtop)

        self.bullet_group.add(bullet)

 

 

class Bullet(pygame.sprite.Sprite):

 

    def __init__(self,bullet_surface,bullet_init_pos):

        super().__init__()

        self.image = bullet_surface

        self.rect = self.image.get_rect()

        self.rect.topleft = bullet_init_pos

        self.speed = 10

 

    def update(self):

        self.rect.top -= self.speed

        if self.rect.top < - self.rect.height:

            self.kill()

 

 

pygame.init()

 

size = width,height = 480,700

screen = pygame.display.set_mode(size)

 

bg = pygame.image.load("images/background.png")

shoot_img = pygame.image.load("images/shoot.png")

 

hero_surface = []

hero_surface.append(shoot_img.subsurface(pygame.Rect(0,0,102,126)))

hero_surface.append(shoot_img.subsurface(pygame.Rect(102,0,102,126)))

hero_pos = [200,500]

 

hero = Hero(hero_surface[0],hero_pos)

offset = { pygame.K_RIGHT:0,pygame.K_LEFT:0,pygame.K_UP:0,pygame.K_DOWN:0}

 

bullet_surface = pygame.image.load("images/bullet1.png")

 

ticks = 0

clock = pygame.time.Clock()

 

while True:

 

    clock.tick(60)

 

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            pygame.quit()

            exit()

 

        if event.type == pygame.KEYDOWN:

            if event.key in offset:

                offset[event.key] = 2

        elif event.type == pygame.KEYUP:

            if event.key in offset:

                offset[event.key] = 0

 

    if ticks >= 30:

        ticks =0

 

    hero.image = hero_surface[ ticks // 15 ]

    hero.move(offset)

 

    if ticks % 10 == 0:

        hero.single_shoot(bullet_surface)

 

    ticks += 1

 

    screen.blit(bg, (0, 0))

    screen.blit(hero.image, hero.rect)

 

    hero.bullet_group.update()

    hero.bullet_group.draw(screen)

 

    pygame.display.update()

 

 

 

 

mygod | 阅读全文 | 回复(0) | 引用通告 | 编辑
发表评论:
请稍候,载入中。。。
公告
请稍候,载入中。。。
时间记忆
请稍候,载入中。。。
最新日志
请稍候,载入中。。。
最新评论
请稍候,载入中。。。
最新回复
请稍候,载入中。。。
我的好友
我的相册
站点信息
请稍候,载入中。。。
生活因感动而精彩,理想在创造中放飞
Powered by Oblog.