我正在尝试写关于外星人射击的pygame,
第一部分,我添加了飞船和子弹,效果很好,然后我做了第二部分,将外星人添加到屏幕上,但是有些问题
在屏幕上添加Alien之前,一切正常,但是当我将Alien.py导入脚本时,飞船的移动速度非常缓慢,几乎无法移动,这是什么原因造成的?
在书中使用aliens.draw(screen)
,而我使用for alien in aliens.sprites:setting.screen.blit(alien.alien,alien.alien_rect)
这两个命令有什么区别?它会导致船缓慢移动吗???
这是代码
bullet.py
#!/usr/bin/python
import sys
import pygame
class Bullet(pygame.sprite.Sprite):
def __init__(self,setting,char):
super().__init__()
pic=pygame.image.load("../image/bullet.jpg").convert_alpha()
self.mellon=pygame.transform.smoothscale(pic,(30,80))
self.rect=self.mellon.get_rect()
# self.rect=pygame.Rect(0,0,setting.bullet_w,setting.bullet_h)
self.rect.centerx=char.ship_rect.centerx
self.rect.top=char.ship_rect.top
self.color=setting.bullet_c
self.speed=setting.bullet_s
self.y=float(self.rect.centery)
def move_bullet(self):
self.y -= self.speed
self.rect.y= self.y
def draw_bullet(self,setting):
setting.screen.blit(self.mellon,self.rect)
#pygame.draw.rect(setting.screen,self.color,self.rect)
def blit_bullet(self,bullets,setting):
for bullet in bullets.sprites():
setting.screen.blit(bullet.mellon,bullet.rect)
#pygame.draw.rect(setting.screen,bullet.color,bullet.rect)
bullet.y -= bullet.speed
bullet.rect.y=bullet.y
for bullet in bullets:
if bullet.rect.y <= 10:
bullets.remove(bullet)
ship.py
#!/usr/bin/python
import sys
import pygame
class Ship():
def __init__(self,screen,setting):
bk=pygame.image.load("../image/muha.png").convert()
self.bkg=pygame.transform.smoothscale(bk,(setting.w,setting.h))
temp=pygame.image.load("../image/title.jpg").convert_alpha()
self.ship=pygame.transform.smoothscale(temp,(200,200))
self.screen=screen
self.screen_rect=screen.get_rect()
self.ship_rect=self.ship.get_rect()
self.ship_rect.center=self.screen_rect.center
self.ship_rect.bottom=self.screen_rect.bottom
self.x=float(self.ship_rect.centerx)
self.y=float(self.ship_rect.centery)
def blit_screen(self):
self.screen.blit(self.bkg,(0,0))
def blit_ship(self):
self.screen.blit(self.ship,self.ship_rect)
function.py
#!/usr/bin/python
import sys
import pygame
from bullet import Bullet
class Function():
def __init__(self):
self.moving_up = False
self.moving_down = False
self.moving_left = False
self.moving_right = False
def event(self,bullets,setting,ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key ==pygame.K_z:
if len(bullets) < setting.bullet_n:
bullets.add(Bullet(setting,ship))
elif event.key == pygame.K_UP:
self.moving_up=True
elif event.key == pygame.K_DOWN:
self.moving_down=True
elif event.key == pygame.K_LEFT:
self.moving_left=True
elif event.key == pygame.K_RIGHT:
self.moving_right=True
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.moving_up=False
elif event.key == pygame.K_DOWN:
self.moving_down=False
elif event.key == pygame.K_LEFT:
self.moving_left=False
elif event.key == pygame.K_RIGHT:
self.moving_right=False
def move(self,setting,ship):
if self.moving_up == True and ship.ship_rect.top >= 0:
ship.y -= setting.speed
if self.moving_down == True and ship.ship_rect.bottom <= setting.h:
ship.y += setting.speed
if self.moving_left == True and ship.ship_rect.left >= 0:
ship.x -= setting.speed
if self.moving_right == True and ship.ship_rect.right <= setting.w:
ship.x += setting.speed
ship.ship_rect.centerx=ship.x
ship.ship_rect.centery=ship.y
外国人
#!/usr/bin/python
import sys
import pygame as p
class Alien(p.sprite.Sprite):
def __init__(self):
super().__init__()
pic=p.image.load("../image/ship.jpg").convert_alpha()
self.alien=p.transform.smoothscale(pic,(100,100))
self.alien_rect=self.alien.get_rect()
self.alien_rect.x=(self.alien_rect.width)
self.y = float(self.alien_rect.y)
def create(self,setting,aliens):
space=setting.w-(self.alien_rect.x)*2
alien_number=int(space/(2*(self.alien_rect.width)))
for number in range(alien_number):
alien=Alien()
alien.alien_rect.x=self.alien_rect.x+2*self.alien_rect.width*number
aliens.add(alien)
def move(self,setting):
self.y += setting.alien_speed2
self.alien_rect.y = self.y
def blit(self,setting,aliens):
for alien in aliens.sprites():
setting.screen.blit(alien.alien,alien.alien_rect)
主脚本
#!/usr/bin/python
import sys,os
import pygame
from bullet import Bullet
from ship import Ship
from function import Function
from alien import Alien
class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.color=(255,255,255)
self.speed=1.5
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
pygame.display.set_caption("Muhaha")
self.bullet_s=1.5
self.bullet_w=5
self.bullet_h=20
self.bullet_n=10
self.bullet_c=(0,0,0)
self.alien_speed=1.5
def game():
pygame.init()
setting=Setting(1200,800)
ship=Ship(setting.screen,setting)
function=Function()
bullet=Bullet(setting,ship)
bullets=pygame.sprite.Group()
alien=Alien()
aliens=pygame.sprite.Group()
while True:
function.event(bullets,setting,ship)
ship.blit_screen()
function.event(bullets,setting,ship)
function.move(setting,ship)
bullet.blit_bullet(bullets,setting)
ship.blit_ship()
alien.create(setting,aliens)
alien.blit(setting,aliens)
pygame.display.flip()
game()
最佳答案
在game()
函数中,调用alien.create(setting,aliens)
每帧。
在1000帧之后,您将拥有1000个外来精灵。
您将在每帧渲染1000个精灵,对于每个额外的帧,您将拥有另一个精灵。因此,游戏将变得非常缓慢。