其他分享
首页 > 其他分享> > 2020-12-15

2020-12-15

作者:互联网

报错

帮忙看一下哪里错了,找了很久崩溃了

TypeError: pygame.sprite.Sprite.add() argument after * must be an iterable, not Alien
Python3

import sys
import pygame
from bullet import Bullet
from alien import Alien
def keydowm_events(event,ai_settings,screen,dtg,bullets):
	if event.key==pygame.K_RIGHT:
		dtg.moving_right=True
	elif event.key==pygame.K_LEFT:
		dtg.moving_left=True
	elif event.key==pygame.K_SPACE:	
		fire_bullet(ai_settings,screen,dtg,bullets)
	elif event.key==pygame.K_q:
		sys.exit()
def keyup_events(event,dtg):
	if event.key==pygame.K_RIGHT:
		dtg.moving_right=False
	elif event.key==pygame.K_LEFT:
		dtg.moving_left=False
def events(ai_settings,screen,dtg,bullets):
	#控制键盘和鼠标
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			sys.exit()
		#移动人物
		elif event.type==pygame.KEYDOWN:
			keydowm_events(event,ai_settings,screen,dtg,bullets)
		elif event.type==pygame.KEYUP:
			keyup_events(event,dtg)
#每次循环都重新绘制屏幕
def update_screen(ai_settings,screen,dtg,aliens,bullets):
	screen.fill(ai_settings.bg_color)
	for bullet in bullets.sprites():
		bullet.draw_bullet()
	dtg.blitme()
	aliens.draw(screen)
	#显示屏幕
	pygame.display.flip()
def update_bullets(bullets):
	#子弹
	bullets.update()
	for bullet in bullets.copy():
		if bullet.rect.bottom<=0:
			bullets.remove(bullet)
def fire_bullet(ai_settings,screen,dtg,bullets):
	if len(bullets)<ai_settings.bullets_allowed:
		new_bullet=Bullet(ai_settings,screen,dtg)
		bullets.add(new_bullet)
def get_number_aliens_x(ai_settings,alien_width):
	available_space_x=ai_settings.screen_width-2*alien_width
	number_aliens_x=int(available_space_x/(2*alien_width))
	return number_aliens_x
def create_alien(ai_settings,screen,aliens,alien_number,row_number):
	alien=Alien(ai_settings,screen)
	alien_width=alien.rect.width
	alien.x=alien_width+2*alien_width*alien_number
	alien.rect.x=alien.x
	alien.rect.y=alien.rect.height+2*alien.rect.height*row_number
	aliens.add(alien)
def create_fleet(ai_settings,screen,dtg,aliens):
	alien=Alien(ai_settings,screen)
	number_aliens_x=get_number_aliens_x(ai_settings,alien.rect.width)
	number_rows=get_number_rows(ai_settings,dtg.rect.height,alien.rect.height)
	for row_number in range(number_rows):
		for alien_number in range(number_aliens_x):
			create_alien(ai_settings,screen,alien,alien_number,row_number)
def get_number_rows(ai_settings,dtg_height,alien_height):
	available_space_y=(ai_settings.screen_height-(3*alien_height)-dtg_height)
	number_rows=int(available_space_y/(2*alien_height))
	return number_rows

Python3

from pygame.sprite import Group
from settings import Settings
from dtg import Dtg
import game_functions as gf 
def run_game():
	#设计屏幕
	pygame.init()
	ai_settings=Settings()
	screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
	pygame.display.set_caption("Alien Invasion")
	dtg=Dtg(ai_settings,screen)
	bullets=Group()
	aliens=Group()
	#建立外星人群
	#游戏循环
	while True:
		#控制键盘和鼠标
		gf.events(ai_settings,screen,dtg,bullets)
		dtg.update()
		#子弹
		gf.update_bullets(bullets)
		#每次循环都重新绘制屏幕
		gf.update_screen(ai_settings,screen,dtg,aliens,bullets)
		gf.create_fleet(ai_settings,screen,dtg,aliens)
run_game()


标签:12,15,settings,screen,dtg,2020,pygame,bullets,event
来源: https://blog.csdn.net/weixin_53605265/article/details/111190877