|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是主程序模块:
import pygame
import sys
import traceback
import my_plane
import enemy
import bullet
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullet = []
bullet_index = 0
bullet_num = 4
for i in range(bullet_num):
bullet.append(bullet(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullet[bullet_index].reset(me.rect.midtop)
bullet_index = (bullet_index + 1) % bullet_num
for b in bullet:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
traceback.print_exc()
pygame.quit()
input()
这是子弹模块:
import pygame
class Bullet(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('子弹.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 12
self.active = True
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
为什么主程序有以下3个问题,请解答并吧改进后的代码给我:
未使用的 import 语句 'import bullet'
'list' 对象不可调用
从外部作用域隐藏名称 'bullet'
问题1:未使用的import语句'import bullet'
这个问题是由于在主程序中导入了子弹模块,但是并没有使用到子弹模块中的任何内容,导致编译器认为这是一个未使用的导入语句。
解决方法:
可以通过删除主程序中的import语句来解决这个问题。
修改后的代码:这是主程序模块:
import pygame
import sys
import traceback
import my_plane
import enemy
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullet = []
bullet_index = 0
bullet_num = 4
for i in range(bullet_num):
bullet.append(bullet(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullet[bullet_index].reset(me.rect.midtop)
bullet_index = (bullet_index + 1) % bullet_num
for b in bullet:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
问题2:'list'对象不可调用
这个问题是由于在主程序中使用了一个名为 bullet 的列表对象作为函数名,导致编译器认为这是一个不可调用的对象。
解决方法:
将列表对象 bullet 重命名为其他名称,以避免与函数名冲突。
修改后的代码:这是主程序模块:
import pygame
import sys
import traceback
import my_plane
import enemy
import bullet
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullets = []
bullet_index = 0
bullet_num = 4
for i in range(bullet_num):
bullets.append(bullet.Bullet(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullets[bullet_index].reset(me.rect.midtop)
bullet_index = (bullet_index + 1) % bullet_num
for b in bullets:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
问题3:从外部作用域隐藏名称'bullet'
这个问题是由于在主程序中定义了一个名为 bullet 的列表对象,而子弹模块中也定义了一个名为 Bullet 的类,导致在主程序中的 bullet 列表对象将隐藏子弹模块中的 Bullet 类。
解决方法:
将主程序中的 bullet 列表对象重命名为其他名称,以避免与子弹模块中的 Bullet 类冲突。
修改后的代码:这是主程序模块:
import pygame
import sys
import traceback
import my_plane
import enemy
import bullet
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullets = []
bullet_index = 0
bullet_num = 4
for i in range(bullet_num):
bullets.append(bullet.Bullet(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullets[bullet_index].reset(me.rect.midtop)
bullet_index = (bullet_index + 1) % bullet_num
for b in bullets:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
以上是修复后的代码,已经解决了主程序中的三个问题。
|
|