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

Crossin的编程教室

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

【Pygame 第8课】 火力全开

[复制链接]

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-8-3 16:41:32 |显示全部楼层
crazy31423 发表于 2017-8-3 15:21
请问: 在代码中, interval_b=100,这个100是什么意思,是指程序的循环次数,还是什么计时单位? ...

循环次数
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

58

积分

注册会员

Rank: 2

发表于 2017-8-3 19:22:24 |显示全部楼层
crossin先生 发表于 2017-8-3 16:41
循环次数

谢谢,再请问下,我把您的Python课程和pygame学完了,下面开始干什么好呢(其实就是想您给点建议,我想用python工作赚钱,往哪个方向学好一点呢,谢谢)
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-8-4 23:32:04 |显示全部楼层
crazy31423 发表于 2017-8-3 19:22
谢谢,再请问下,我把您的Python课程和pygame学完了,下面开始干什么好呢(其实就是想您给点建议,我想用 ...

web方面,做点爬虫,开发个网站,差不多可以试试找工作了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

22

积分

新手上路

Rank: 1

发表于 2017-12-27 16:42:57 |显示全部楼层
crossin先生,问一个逻辑问题
按照思路,设置子弹速度比较慢,导致一轮打完后第一发子弹还没到达屏幕上端
第五颗子弹发射后就会出现第一颗子弹被重置重新发射,如此循环,导致这一轮子弹永远无法到达屏幕上端
但实际运行结果是,这一轮子弹会一直往上走,中间出现空子弹期
当第一颗子弹运行到屏幕最上端后,就会立即重置重新发射
这是怎么回事呢?
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-12-28 10:32:09 |显示全部楼层
很好吃 发表于 2017-12-27 16:42
crossin先生,问一个逻辑问题
按照思路,设置子弹速度比较慢,导致一轮打完后第一发子弹还没到达屏幕上端
...

检查你的代码逻辑,调试
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

22

积分

新手上路

Rank: 1

发表于 2017-12-28 12:11:40 |显示全部楼层
crossin先生 发表于 2017-12-28 10:32
检查你的代码逻辑,调试

谢谢crossin先生,问题已解决
我在restart函数里用了if y < 0这个判断条件,导致了这个效果
回复

使用道具 举报

0

主题

0

好友

16

积分

新手上路

Rank: 1

发表于 2018-1-17 19:57:01 |显示全部楼层
先生,这里if b.active可以把判断注释掉吗,因为子弹move方法中已经有判断了

#判断每科子弹的状态   
    for b in bullets:
        if b.active:#如果子弹处于激活状态,那就开始移动并绘制
            b.move()#调用move方法,处理子弹的运动
            screen.blit(b.image,(b.x,b.y))#把子弹滑到屏幕上,数据来自成员变量

def move(self):
        #激活状态下,向上移动
        if self.active:
            self.y -=2
        #当飞出屏幕,就设为不激活   
        if self.y < 0:
            self.active = False
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2018-1-18 11:10:12 |显示全部楼层
不是啤酒肚 发表于 2018-1-17 19:57
先生,这里if b.active可以把判断注释掉吗,因为子弹move方法中已经有判断了

#判断每科子弹的状态   

那也会被绘制出来
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

28

积分

新手上路

Rank: 1

发表于 2018-1-23 15:59:55 |显示全部楼层
  1. # -*- coding:utf-8 -*-
  2. import random
  3. from sys import exit

  4. import pygame


  5. # 子弹类
  6. class Bullet:
  7.     def __init__(self):

  8.         self.x1 = 0
  9.         self.y1 = -1

  10.         self.x2 = -1
  11.         self.y2 = -1

  12.         self.x3 = 1
  13.         self.y3 = -1
  14.         self.image = pygame.image.load('bullet.png').convert_alpha()
  15.         # 默认不激活
  16.         self.active = False

  17.     def move(self):
  18.         # 激活状态下,向上移动
  19.         if self.active:
  20.             self.y1 -= 3
  21.             self.y2 -= 3
  22.             self.y3 -= 3
  23.         # 当飞出屏幕,就设为不激活
  24.         if self.y1 < 0:
  25.             self.active = False

  26.     def restart(self):
  27.         # 重置子弹位置
  28.         mouseX, mouseY = pygame.mouse.get_pos()
  29.         pygame.mouse.set_visible(False)
  30.         self.x1 = mouseX - self.image.get_width() / 2 - 10
  31.         self.y1 = mouseY - self.image.get_height() / 2

  32.         self.x2 = mouseX - self.image.get_width() / 2
  33.         self.y2 = mouseY - self.image.get_height() / 2

  34.         self.x3 = mouseX - self.image.get_width() / 2 + 10
  35.         self.y3 = mouseY - self.image.get_height() / 2
  36.         # 激活子弹
  37.         self.active = True


  38. # 敌机类
  39. class Enemy:
  40.     def __init__(self):
  41.         self.restart()
  42.         self.image = pygame.image.load('enemy.png').convert_alpha()

  43.     def move(self):
  44.         if self.y < 800:
  45.             self.y += self.speed
  46.         else:
  47.             self.restart()

  48.     def restart(self):
  49.         self.x = random.randint(50, 400)
  50.         self.y = random.randint(-200, -50)
  51.         self.speed = random.random() + 0.1


  52. pygame.init()
  53. screen = pygame.display.set_mode((450, 800), 0, 32)
  54. pygame.display.set_caption("天空,超爱你!")
  55. # 加载相关图像
  56. sky = 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. # 创建子弹对象和敌机对象
  70. enemy = Enemy()

  71. while True:
  72.     for event in pygame.event.get():
  73.         if event.type == pygame.QUIT:
  74.             pygame.quit()
  75.             exit()
  76.     screen.blit(sky, (0, 0))

  77.     # 发射间隔递减
  78.     interval_b -= 1
  79.     # 当间隔小于0时,激活一发子弹
  80.     if interval_b < 0:
  81.         bullets[index_b].restart()
  82.         # 重置间隔时间
  83.         interval_b = 100
  84.         # 子弹序号周期性递增
  85.         index_b = (index_b + 1) % count_b
  86.     # 判断每个子弹的状态
  87.     for b in bullets:
  88.         # 处于激活状态的子弹,移动位置并绘制
  89.         if b.active:
  90.             b.move()
  91.             screen.blit(b.image, (b.x1, b.y1))
  92.             screen.blit(b.image, (b.x2, b.y2))
  93.             screen.blit(b.image, (b.x3, b.y3))

  94.     # 敌机开始运动
  95.     enemy.move()
  96.     screen.blit(enemy.image, (enemy.x, enemy.y))
  97.     x, y = pygame.mouse.get_pos()
  98.     x -= plane.get_width() / 2
  99.     y -= plane.get_height() / 2
  100.     screen.blit(plane, (x, y))
  101.     pygame.display.update()
复制代码
微信截图_20180123155723.png
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2018-4-2 20:06:31 |显示全部楼层
请问老师:我这样为什么不会有子弹发射出去呀?感觉问题出在bullet.move()里面,但是一用bullet.active就没有子弹了
  1. import pygame
  2. from sys import exit
  3. from alien import Alien
  4. from random import randint
  5. from random import random

  6. class Alien:
  7.     def restart(self):
  8.         self.x = randint(50,350)
  9.         self.y = randint(-200,-50)
  10.         self.speed = random()+1
  11.     def __init__(self):
  12.         self.restart()
  13.         self.image = pygame.image.load('image/alien.bmp')
  14.     def move(self):
  15.         if self.y < 700:
  16.             self.y += self.speed
  17.         else:
  18.             self.restart()

  19. class Bullet():
  20.     def __init__(self):
  21.         self.x = 0
  22.         self.y = 0
  23.         self.image = pygame.image.load('image/bullet.bmp')
  24.         self.active = False
  25.     def restart(self):
  26.         self.active = True
  27.         (mouse_x,mouse_y) = pygame.mouse.get_pos()
  28.         self.x = mouse_x - self.image.get_width()/2
  29.         self.y = mouse_y - self.image.get_height()/2
  30.     def move(self):
  31.         if self.active == True:
  32.             self.y -= 5
  33.         if self.y <0:
  34.             self.active = False

  35. pygame.init()
  36. screen = pygame.display.set_mode((400,700))
  37. pygame.display.set_caption('打飞机')
  38. background = pygame.image.load('image/bg.bmp')
  39. ship = pygame.image.load('image/ship.bmp')
  40. bullet = Bullet()
  41. alien = Alien()
  42. while True:
  43.     for event in pygame.event.get():
  44.         if event.type == pygame.QUIT:
  45.             pygame.quit()
  46.             exit()
  47.         if event.type == pygame.MOUSEBUTTONDOWN:
  48.             background = pygame.image.load('image/bg2.bmp')
  49.     (x,y) = pygame.mouse.get_pos()
  50.     x -= (ship.get_width()/2)
  51.     y -= (ship.get_width()/2)
  52.     bullet.move()
  53.     alien.move()
  54.     screen.blit(background,(0,0))
  55.     screen.blit(ship,(x,y))
  56.     screen.blit(bullet.image,(bullet.x,bullet.y))
  57.     screen.blit(alien.image,(alien.x,alien.y))
  58.     pygame.display.update()
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-3-29 18:58 , Processed in 0.020431 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部