- 帖子
- 4
- 精华
- 0
- 积分
- 35
- 阅读权限
- 10
- 注册时间
- 2018-1-5
- 最后登录
- 2018-2-1
|
老师,我的两排子弹其中一排打到敌机为什么会出现不同步的情况呀?是因为延迟的关系吗?- #-*- coding: utf-8 -*-
- import pygame #导入pygame库
- import random
- from itertools import product
- from sys import exit #向sys模块借一个exit函数用来退出程序
- class Bullet:
- def __init__(self):
- self.x = 0
- self.y = -1
- self.image = pygame.image.load('C:/Python27/test/pic/bullet.png').convert_alpha()
- self.active = False #默认不激活
-
- def move(self):#处理子弹运动
- #激活状态下,向上移动
- if self.active:
- self.y -= 0.1
- #当飞出屏幕,就设为不激活
- if self.y < 0:
- self.active = False
- def restart(self):
- #重置子弹位置
- mouseX,mouseY = pygame.mouse.get_pos()
- self.x = mouseX - self.image.get_width() / 2
- self.y = mouseY - self.image.get_height() / 2
- #激活子弹
- self.active = True
- class Enemy:
- def restart(self):#重置敌机位置和速度
- self.x = random.randint(50,400)
- self.y = random.randint(-200,-50)
- self.speed = random.random() + 0.1
- def __init__(self):
- self.restart()
- self.image = pygame.image.load('c:/Python27/test/pic/enemy.png').convert_alpha()
- def move(self):
- if self.y < 800:#向下运动
- self.y += self.speed
- else:
- self.restart() #重置
-
- def checkHit(enemy,bullet1,bullet2): #如果子弹在敌机图片的范围之内
- if((bullet1.x + 24 > enemy.x or bullet2.x - 23 > enemy.x )and (
- bullet1.x +24< enemy.x + enemy.image.get_width() or bullet2.x - 23
- < enemy.x + enemy.image.get_width())and( bullet1.y > enemy.y or bullet2.y> enemy.y)
- and( bullet1.y < enemy.y + enemy.image.get_height() or bullet2.y < enemy.y + enemy.image.get_height())):
- enemy.restart() #重置敌机
- bullet1.active = bullet2.active = False #重置子弹
-
- #print bullet2.y ,bullet1.y
- pygame.init() #初始化pygame,为使用硬件做准备
- screen = pygame.display.set_mode((450,800), 0, 32) #创建了一个窗口,窗口大小和背景图片大小一样
- pygame.display.set_caption("ONE PIECE")
- background = pygame.image.load('C:/Python27/test/pic/back.jpg').convert_alpha()#加载并转换图像
- plane = pygame.image.load('C:/Python27/test/pic/plane.png').convert_alpha()
- pygame.mouse.set_visible(False)
- bullets1 = [] #创建子弹的list
- bullets2 = []
- for i in range(5): #向list中添加5发子弹
- bullets1.append(Bullet())
- bullets2.append(Bullet())
-
- count_b = len(bullets1) #子弹总数
- index_b = 0 #即将激活的子弹序号
- interval_b = 0 #发射子弹的间隔
- enemies = []
- for i in range(5):
- enemies.append(Enemy()) #创建敌机
- while True: #游戏主循环
- for event in pygame.event.get():
- if event.type == pygame.QUIT: #接收到退出事件后退出程序
- pygame.quit()
- exit()
-
- screen.blit(background, (0,0))
- interval_b -= 1 #发射间隔递减
- #print interval_b
- if interval_b < 0: #当间隔小于0时,激活一发子弹
- bullets1[index_b].restart()
- bullets2[index_b].restart()
- interval_b = 900 #重置间隔时间
- index_b = (index_b + 1) % count_b #子弹序号周期性递增
- for b,c in product(bullets1,bullets2): #判断每个子弹的状态
- if b.active and c.active: #处于激活状态的子弹,移动位置并绘制
- for e in enemies:
- checkHit(e,b,c)
- b.move()
- c.move()
- screen.blit(b.image,(b.x + 24,b.y))
- screen.blit(c.image,(c.x - 23,c.y))
- #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
- for e in enemies:
- e.move()
- screen.blit(e.image,(e.x,e.y))
- #print '第一架:',enemies[0].x,enemies[0].y,'第二架:',enemies[1].x,enemies[2].y
-
- x,y = pygame.mouse.get_pos()
- x-= plane.get_width()/2
- y-= plane.get_height()/2#计算飞机的左上角位置
- screen.blit(plane,(x,y))#把飞机画到屏幕上
- pygame.display.update() #刷新一下画面
复制代码
|
|