python
这是主程序模块: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 or key_pressed:
me.move_up()
if key_pressed or key_pressed:
me.move_down()
if key_pressed or key_pressed:
me.move_left()
if key_pressed or key_pressed:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullet.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'。
2. 'list'对象不可调用。
3. 从外部作用域隐藏名称'bullet'。
首先,对于问题1,可以看到在主程序中没有使用'import bullet',因此可以将该导入语句删除,因为在主程序中并没有使用到bullet模块中的任何内容。
其次,对于问题2和问题3,是由于在主程序中使用了变量名和模块名相同(都为'bullet'),导致名称冲突和混淆。为了避免这种混淆,可以将主程序中的bullet变量重命名为其他名称,例如bullets。
下面是改进后的代码:
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)
bullets = []# 改进:将bullet重命名为bullets
bullet_index = 0
bullet_num = 4
for i in range(bullet_num):
bullets.append(Bullet(me.rect.midtop))# 改进:使用Bullet类创建子弹对象
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 or key_pressed:
me.move_up()
if key_pressed or key_pressed:
me.move_down()
if key_pressed or key_pressed:
me.move_left()
if key_pressed or key_pressed:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullets.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
traceback.print_exc()
pygame.quit()
input()
以上是改进后的代码,已经解决了问题并将变量名和模块名进行区分。希望对你有所帮助!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案 问题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 or key_pressed:
me.move_up()
if key_pressed or key_pressed:
me.move_down()
if key_pressed or key_pressed:
me.move_left()
if key_pressed or key_pressed:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullet.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 or key_pressed:
me.move_up()
if key_pressed or key_pressed:
me.move_down()
if key_pressed or key_pressed:
me.move_left()
if key_pressed or key_pressed:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullets.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 or key_pressed:
me.move_up()
if key_pressed or key_pressed:
me.move_down()
if key_pressed or key_pressed:
me.move_left()
if key_pressed or key_pressed:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullets.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()
以上是修复后的代码,已经解决了主程序中的三个问题。 不是这个意思,我这个程序要实现的目的是打飞机,你这样硬改还能实现目的吗?
页:
[1]