请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

【Pygame 第10课】 命中目标

[复制链接]

0

主题

0

好友

4

积分

新手上路

Rank: 1

发表于 2017-9-17 02:13:29 |显示全部楼层
请问老师,如果在命中目标后再加个爆炸效果该怎么实现?
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-9-17 11:57:20 |显示全部楼层
gysgo2 发表于 2017-9-17 02:13
请问老师,如果在命中目标后再加个爆炸效果该怎么实现?

增加一种爆炸的 sprite,用帧动画实现爆炸效果(这个网上可以搜到)
然后击中了之后,就在击中的位置放这个sprite,放完就移除
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

发表于 2017-9-18 19:18:31 |显示全部楼层
本帖最后由 lsp84ch83 于 2017-9-18 19:22 编辑

以下是我对照论坛写的飞机代码,但是我的子弹距离很短,附件图片为运行效果
  1. # -*- coding:utf-8 -*-
  2. import pygame
  3. import random
  4. from sys import exit


  5. # 定义子弹的速度、运动方向
  6. class Bullet:
  7.     def __init__(self):
  8.         # 初始化成员变量:x,y,image
  9.         self.x = 0
  10.         self.y = -1
  11.         self.image = pygame.image.load('bullet.png').convert_alpha()
  12.         # 默认不激活
  13.         self.active = False

  14.     def move(self):
  15.         # 激活状态下,向上移动
  16.         if self.active:
  17.             self.y -= 0.8
  18.         # 当飞出屏幕,就设为不激活
  19.         if self.y < 0:
  20.             self.active = False

  21.     def restart(self):
  22.         # 重置子弹位置
  23.         mouseX, mouseY = pygame.mouse.get_pos()
  24.         self.x = mouseX - self.image.get_width() / 2
  25.         self.y = mouseY - self.image.get_height() / 2
  26.         # 激活子弹
  27.         self.active = True

  28. # 定义敌机的速度、出现位置
  29. class Enemy:
  30.     def restart(self):
  31.         # 重置敌机位置和速度
  32.         self.x = random.randint(50,400)
  33.         self.y = random.randint(-200,-50)
  34.         self.speed = random.random() + 0.1

  35.     def __init__(self):
  36.         # 初始化
  37.         self.restart()
  38.         self.image = pygame.image.load('enemy.png').convert_alpha()

  39.     def move(self):
  40.         if self.y < 800:
  41.             # 向下移动
  42.             self.y += self.speed
  43.         else:
  44.             # 重置
  45.             self.restart()


  46. # 检测子弹是否命中        
  47. def checkHit(enemy,bullet):
  48.     if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (
  49.                     bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
  50.         enemy.restart()
  51.         bullet.active = False

  52. # 初始化
  53. pygame.init()
  54. screen = pygame.display.set_mode((450,800), 0, 32)
  55. pygame.display.set_caption('飞机')
  56. background = pygame.image.load('back.jpg').convert()
  57. plane = pygame.image.load('plane.png').convert_alpha()
  58. # 创建子弹的list
  59. bullets = []
  60. # 向list中添加 5 发子弹
  61. for i in range(5):
  62.     bullets.append(Bullet())
  63. # 子弹总数
  64. count_b = len(bullets)
  65. # 即将激活的子弹序号
  66. index_b = 0
  67. # 发射子弹的间隔
  68. interval_b = 0
  69. # 创建敌机的list
  70. enemies = []
  71. # 向list总添加 5 架敌机
  72. for i in range(5):
  73.     enemies.append(Enemy())

  74. # 游戏主体
  75. while True:
  76.     # 判断退出条件
  77.     for event in pygame.event.get():
  78.         if event.type == pygame.QUIT:
  79.             pygame.quit()
  80.             exit()
  81.     screen.blit(background,(0,0))
  82.     # 发射间隔递减
  83.     interval_b -= 1
  84.     # 当间隔小于0时,激活一发子弹
  85.     if interval_b < 0:
  86.         bullets[interval_b].restart()
  87.         # 重置间隔时间
  88.         interval_b = 200
  89.         # 子弹序号周期性递增
  90.         index_b = (index_b + 1) % count_b
  91.     # 判断每个子弹的状态
  92.     for b in bullets:
  93.         # 处于激活状态的子弹,移动位置并绘制
  94.         if b.active:
  95.             for e in enemies:
  96.                 checkHit(e,b)
  97.             b.move()
  98.             screen.blit(b.image, (b.x, b.y))
  99.     # 更新敌机位置,处理每架敌机的运动
  100.     for e in enemies:
  101.         e.move()
  102.         screen.blit(e.image,(e.x,e.y))
  103.     # 绘制子弹,数据来自其成员变量
  104.     x,y = pygame.mouse.get_pos()
  105.     x -= plane.get_width() / 2
  106.     y -= plane.get_height() / 2
  107.     screen.blit(plane,(x,y))
  108.     pygame.display.update()
复制代码
新建位图图像.jpg
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-9-18 22:54:23 |显示全部楼层
lsp84ch83 发表于 2017-9-18 19:18
以下是我对照论坛写的飞机代码,但是我的子弹距离很短,附件图片为运行效果 ...

那你可以通过调节 speed 或者 interval_b 来修改。
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

发表于 2017-9-19 16:40:06 |显示全部楼层
本帖最后由 lsp84ch83 于 2017-9-19 16:59 编辑
crossin先生 发表于 2017-9-18 22:54
那你可以通过调节 speed 或者 interval_b 来修改。

我通过工具对比了下,代码里有个变量应用错了。
回复

使用道具 举报

1

主题

0

好友

35

积分

新手上路

Rank: 1

发表于 2018-1-12 10:24:09 |显示全部楼层
老师,我的两排子弹其中一排打到敌机为什么会出现不同步的情况呀?是因为延迟的关系吗?
  1. #-*- coding: utf-8 -*-
  2. import pygame  #导入pygame库
  3. import random
  4. from itertools import product

  5. from sys import exit  #向sys模块借一个exit函数用来退出程序

  6. class Bullet:
  7.     def __init__(self):
  8.         self.x = 0
  9.         self.y = -1
  10.         self.image = pygame.image.load('C:/Python27/test/pic/bullet.png').convert_alpha()
  11.         self.active = False #默认不激活
  12.         
  13.     def move(self):#处理子弹运动
  14.         #激活状态下,向上移动
  15.         if self.active:
  16.             self.y -= 0.1
  17.         #当飞出屏幕,就设为不激活
  18.         if self.y < 0:
  19.             self.active = False

  20.     def restart(self):   
  21.         #重置子弹位置
  22.         mouseX,mouseY = pygame.mouse.get_pos()
  23.         self.x = mouseX - self.image.get_width() / 2
  24.         self.y = mouseY - self.image.get_height() / 2
  25.         #激活子弹
  26.         self.active = True

  27. class Enemy:
  28.     def restart(self):#重置敌机位置和速度
  29.         self.x = random.randint(50,400)
  30.         self.y = random.randint(-200,-50)
  31.         self.speed = random.random() + 0.1

  32.     def __init__(self):
  33.         self.restart()
  34.         self.image = pygame.image.load('c:/Python27/test/pic/enemy.png').convert_alpha()

  35.     def move(self):
  36.         if self.y < 800:#向下运动
  37.             self.y += self.speed
  38.         else:
  39.             self.restart() #重置
  40.             
  41. def checkHit(enemy,bullet1,bullet2): #如果子弹在敌机图片的范围之内
  42.     if((bullet1.x + 24  > enemy.x or bullet2.x - 23 > enemy.x )and (
  43.         bullet1.x +24< enemy.x + enemy.image.get_width() or bullet2.x - 23
  44.         < enemy.x + enemy.image.get_width())and( bullet1.y > enemy.y or bullet2.y> enemy.y)
  45.        and( bullet1.y < enemy.y + enemy.image.get_height() or bullet2.y < enemy.y + enemy.image.get_height())):

  46.         enemy.restart() #重置敌机
  47.         bullet1.active = bullet2.active = False #重置子弹
  48.       
  49.         #print bullet2.y ,bullet1.y   


  50. pygame.init() #初始化pygame,为使用硬件做准备
  51. screen = pygame.display.set_mode((450,800), 0, 32) #创建了一个窗口,窗口大小和背景图片大小一样
  52. pygame.display.set_caption("ONE PIECE")
  53. background = pygame.image.load('C:/Python27/test/pic/back.jpg').convert_alpha()#加载并转换图像
  54. plane = pygame.image.load('C:/Python27/test/pic/plane.png').convert_alpha()
  55. pygame.mouse.set_visible(False)

  56. bullets1 = [] #创建子弹的list
  57. bullets2 = []
  58. for i in range(5): #向list中添加5发子弹
  59.     bullets1.append(Bullet())
  60.     bullets2.append(Bullet())
  61.    
  62. count_b = len(bullets1) #子弹总数
  63. index_b = 0 #即将激活的子弹序号
  64. interval_b = 0 #发射子弹的间隔

  65. enemies = []
  66. for i in range(5):
  67.     enemies.append(Enemy()) #创建敌机

  68. while True: #游戏主循环
  69.     for event in pygame.event.get():
  70.         if event.type == pygame.QUIT: #接收到退出事件后退出程序
  71.             pygame.quit()
  72.             exit()
  73.                
  74.     screen.blit(background, (0,0))

  75.     interval_b -= 1 #发射间隔递减
  76.     #print interval_b
  77.     if interval_b < 0: #当间隔小于0时,激活一发子弹
  78.         bullets1[index_b].restart()
  79.         bullets2[index_b].restart()
  80.         interval_b = 900 #重置间隔时间
  81.         index_b = (index_b + 1) % count_b #子弹序号周期性递增

  82.     for b,c in product(bullets1,bullets2): #判断每个子弹的状态
  83.         if b.active and c.active: #处于激活状态的子弹,移动位置并绘制
  84.             for e in enemies:
  85.                 checkHit(e,b,c)
  86.             b.move()
  87.             c.move()
  88.             screen.blit(b.image,(b.x + 24,b.y))
  89.             screen.blit(c.image,(c.x - 23,c.y))        
  90.     #print '第一颗子弹:',bullets1[0].x , bullets1[0].y,'第二颗子弹:',bullets1[1].x , bullets1[1].y ,'第三颗子弹:',bullets1[2].x , bullets1[2].y , '第四颗子弹:',bullets1[3].x , bullets1[3].y , '第五颗子弹:',bullets1[4].x , bullets1[4].y      

  91.     for e in enemies:
  92.         e.move()
  93.         screen.blit(e.image,(e.x,e.y))
  94.         #print '第一架:',enemies[0].x,enemies[0].y,'第二架:',enemies[1].x,enemies[2].y

  95.    
  96.     x,y = pygame.mouse.get_pos()
  97.     x-= plane.get_width()/2
  98.     y-= plane.get_height()/2#计算飞机的左上角位置
  99.     screen.blit(plane,(x,y))#把飞机画到屏幕上
  100.     pygame.display.update() #刷新一下画面
复制代码
11.png


回复

使用道具 举报

1

主题

0

好友

35

积分

新手上路

Rank: 1

发表于 2018-1-12 13:14:33 |显示全部楼层
本帖最后由 Youngkuso 于 2018-1-12 15:10 编辑

不仅延迟,还能感觉到打到敌机的子弹会明显变慢
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2018-1-12 16:32:15 |显示全部楼层
Youngkuso 发表于 2018-1-12 13:14
不仅延迟,还能感觉到打到敌机的子弹会明显变慢

你的代码里不是击中敌机就 actice False ,然后不移动了吗
你这个要自己调试代码来看的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

148

积分

注册会员

Rank: 2

发表于 2018-3-14 16:18:22 |显示全部楼层
请问判断飞机是否被打中的那段代码
            for e in enemies:
                checkHit(e, b)
是不是也可以放在别的位置,比如单独出去,或者是处理每个飞机运动的for循环里,比如
    for e in enemies:
        checkHit(e, b)
        e.move()
        screen.blit(e.image, (e.x, e.y))
两处有什么不同么?只是响应时间会有所不同,还是会有很多其他的不一样,谢谢老师
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2018-3-15 17:15:12 |显示全部楼层
skybeak 发表于 2018-3-14 16:18
请问判断飞机是否被打中的那段代码
            for e in enemies:
                checkHit(e, b)

可以,一个功能可以有不同的实现
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-19 09:50 , Processed in 0.019806 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部