python
import pygameimport 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 语句 'import bullet'
从外部作用域隐藏名称 'bullet'
为什么会出现这两个问题,请大佬解答一下 问题1:为什么会出现"未使用的 import 语句 'import bullet'"的警告?
答:出现这个警告是因为在代码中导入了bullet模块,但是在后续的代码中并没有使用到该模块。警告是编译器为了提醒开发者可能存在的问题而发出的,如果确实没有使用到bullet模块,可以将这行导入语句删除。
问题2:为什么会出现"从外部作用域隐藏名称 'bullet'"的警告?
答:出现这个警告是因为在代码中定义了一个名为bullet的变量,而在导入bullet模块时,该变量和模块名称发生了冲突。警告是编译器为了提醒开发者可能存在的问题而发出的,为了避免命名冲突,可以将变量名改为其他名称,或者将导入语句修改为"import bullet as blt"等形式来避免冲突。 你好!根据你提供的代码,出现两个问题的原因如下:
1. "未使用的 import 语句 'import bullet'从外部作用域隐藏名称 'bullet'":这个警告表示在代码中导入了bullet模块,但是并没有在后续代码中使用该模块的内容。可以通过删除该行导入语句来解决这个问题。
2. "name 'main' is not defined":这个错误是因为在主程序中调用了main()函数,但是这个函数没有被定义。要解决这个问题,你需要在代码中定义一个名为main()的函数,并在其中编写主程序逻辑。
下面是修改后的代码示例:
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
traceback.print_exc()
pygame.quit()
input()
希望能帮到你!如有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案 "name 'main' is not defined"我没有这个问题啊,况且我也定义了main函数,为什么你会这样回答? 这是bullet模块:
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
我不知道为什么没有引用,请吧改进后的代码给我
页:
[1]