Pygame项目

Pygame项目

一、利用Pygame 画一个大白

import pygame
import random
pygame.init()
screen=pygame.display.set_mode((480,600))
pygame.display.set_caption("Baymax")

screen.fill((250,50,50))
pygame.draw.circle(screen,(255,255,255),(240,100),50)
pygame.draw.circle(screen,(0,0,0),(215,100),10)
pygame.draw.circle(screen,(0,0,0),(265,100),10)
pygame.draw.line(screen,(0,0,0),(215,100),(265,100))
pygame.draw.ellipse(screen,(255,255,255),(140,120,200,280))
pygame.draw.ellipse(screen,(255,255,255),(110,150,100,50))
pygame.draw.ellipse(screen,(255,255,255),(270,150,100,50))

game_over=False
while not game_over:
	event_list=pygame.event.get()
	for event in event_list:
		if event.type==pygame.QUIT:
			game_over=True
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_ESCAPE:
				game_over=True
		#按下鼠标,则改变眼睛颜色
		if event.type==pygame.MOUSEBUTTONDOWN:
			pygame.draw.circle(screen,(random.randint(0,256),random.randint(0,256),random.randint(0,256)),(215,100),10)
			pygame.draw.circle(screen,(random.randint(0,256),random.randint(0,256),random.randint(0,256)),(265,100),10)
	pygame.display.flip()

Pygame项目_第1张图片

二、创建玩家、子弹、敌人三类;当子弹打中一个敌人后马上在画面添加一个新敌人;击中一次敌人会分裂成两个小敌人,再击中一次才消失

import pygame
import random
import time
WIDTH,HEIGHT=480,600

pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shooting gallery")
clock=pygame.time.Clock()


class Player(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.Surface((30,30))
		self.image.fill((255,0,0))
		self.rect=self.image.get_rect()

		self.rect.centerx=WIDTH/2
		self.rect.bottom=HEIGHT
	def update(self):
		keystate=pygame.key.get_pressed()
		if keystate[pygame.K_LEFT]:
			self.rect.centerx-=35
			
		if keystate[pygame.K_RIGHT]:
			self.rect.centerx+=35
			
		if self.rect.left<0:
			self.rect.left=0
		if self.rect.right>WIDTH:
			self.rect.right=WIDTH
	def shoot(self):
		bullet=Bullet(self.rect.centerx,self.rect.centery)
		bullets.add(bullet)

class Enemy(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.Surface((20,20))
		self.image.fill((0,255,0))
		self.rect=self.image.get_rect()
		self.rect.left=random.randint(0,WIDTH-self.rect.w)

		self.vx=random.randint(-5,5)
		self.vy=5
	def update(self):
		if 0<=self.rect.left<=WIDTH-self.rect.w and 0<=self.rect.top<=HEIGHT-self.rect.h:
			self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
			self.rect.x+=self.vx
			self.rect.y+=self.vy

class LittleEnemy(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.Surface((10,10))
		self.image.fill((0,255,0))
		self.rect=self.image.get_rect()
		self.rect.left=random.randint(0,WIDTH-self.rect.w)

		self.vx=random.randint(-3,3)
		self.vy=4
	def update(self):
		if 0<=self.rect.left<=WIDTH-self.rect.w and 0<=self.rect.top<=HEIGHT-self.rect.h:
			#self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
			self.rect.x+=self.vx
			self.rect.y+=self.vy

class Bullet(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.Surface((5,10))
		self.image.fill((250,128,128))
		self.rect=self.image.get_rect()

		self.rect.centerx=x
		self.rect.centery=y

	def update(self):
		self.rect.y-=35

player=Player()
littleenemys=pygame.sprite.Group()
enemys=pygame.sprite.Group()
for i in range (0,10):
	enemy=Enemy()
	enemys.add(enemy)
bullets=pygame.sprite.Group()


game_over=False
while not game_over:
	event_list=pygame.event.get()
	for event in event_list:
		if event.type==pygame.QUIT:
			game_over=True
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_ESCAPE:
				game_over=True
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_SPACE:
				player.shoot()
		hits1=pygame.sprite.groupcollide(enemys,bullets,True,True)
		if hits1:
			littleenemy1=LittleEnemy()
			littleenemys.add(littleenemy1)
			littleenemy2=LittleEnemy()
			littleenemys.add(littleenemy2) 

		hits2=pygame.sprite.groupcollide(littleenemys,bullets,True,True)
	screen.fill((255,255,255))
	screen.blit(player.image,player.rect)
	enemys.draw(screen)
	littleenemys.draw(screen)
	bullets.draw(screen)
	player.update()
	enemys.update()
	littleenemys.update()
	bullets.update()
	time.sleep(0.1)
	pygame.display.flip()

Pygame项目_第2张图片
难点:怎么知道enemy与bullet发生hits时,enemy或者bullet的位置呢,只有知道了这个才能让enemy在发生撞击的位置进行分裂。hit.rect.center

三、添加游戏素材及动画

import pygame
import random
import time
from os import path
img_dir=path.join(path.dirname(__file__),'img')
background_dir=path.join(img_dir,'background.png')
WIDTH,HEIGHT=480,600
NEW_ENEMY_GENERATE_INTERVAL=500

pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shooting gallery")
clock=pygame.time.Clock()

background=pygame.image.load(background_dir).convert()
background_rect=background.get_rect()
player_dir=path.join(img_dir,'spaceShips_009.png')
player_img=pygame.image.load(player_dir).convert()
enemy_dir=path.join(img_dir,'spaceMeteors_003.png')
enemy_img=pygame.image.load(enemy_dir).convert()
bullet_dir=path.join(img_dir,'spaceMissiles_040.png')
bullet_img=pygame.image.load(bullet_dir).convert()

class Player(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(player_img,(35,35))
		self.image=pygame.transform.flip(player_img,False,True)
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=WIDTH/2
		self.rect.bottom=HEIGHT
	def update(self):
		keystate=pygame.key.get_pressed()
		if keystate[pygame.K_LEFT]:
			self.rect.centerx-=35
		if keystate[pygame.K_RIGHT]:
			self.rect.centerx+=35			
		if self.rect.left<0:
			self.rect.left=0
		if self.rect.right>WIDTH:
			self.rect.right=WIDTH
	def shoot(self):
		bullet=Bullet(self.rect.centerx,self.rect.centery)
		bullets.add(bullet)
		launch=Launch(self.rect.centerx,self.rect.centery)
		launchs.add(launch)

class Enemy(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		img_width=random.randint(20,60)
		self.image=pygame.transform.scale(enemy_img,(img_width,img_width))
		self.image.set_colorkey((0,0,0))
		self.image_origin=self.image.copy()
		self.rect=self.image.get_rect()
		self.rect.left=random.randint(0,WIDTH-self.rect.w)
		self.rect.bottom=0
		self.vx=random.randint(-2,2)
		self.vy=random.randint(2,10)

		self.last_time=pygame.time.get_ticks()
		self.rotate_speed=random.randint(-5,5)
		self.rotate_angle=0

	def update(self):
		if 0<=self.rect.left<=WIDTH-self.rect.w and self.rect.top<=HEIGHT-self.rect.h:
			#self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
			self.rect.x+=self.vx
			self.rect.y+=self.vy
			self.rotate()
	def rotate(self):
		self.rotate_angle=(self.rotate_angle+self.rotate_speed)%360
		self.image=pygame.transform.rotate(self.image_origin,self.rotate_angle)

class LittleEnemy(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(enemy_img,(15,15))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
		self.vx=random.choice([-5,-10,-15,5,10,15])
		self.vy=random.choice([5,6,7,8,9,10])
	def update(self):
		if 0<=self.rect.left<=WIDTH-self.rect.w and 0<=self.rect.top<=HEIGHT-self.rect.h:
			#self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
			self.rect.x+=self.vx
			self.rect.y+=self.vy

class Bullet(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(bullet_img,(15,20))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
	def update(self):
		self.rect.y-=35

class Explosion(pygame.sprite.Sprite):
	def __init__(self,center):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(explosion_animation[0],(30,30))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.center=center
		self.frame=0
		self.last_time=pygame.time.get_ticks()
	def update(self):
		now=pygame.time.get_ticks()
		if now-self.last_time>30:
			if self.frame<len(explosion_animation):
				self.image=pygame.transform.scale(explosion_animation[self.frame],(30,30))
				self.image.set_colorkey((0,0,0))
				self.frame+=1
				self.last_time=now
			else:
				self.kill()

class Launch(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(launch_animation[0],(15,20))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
		self.frame=0
		self.last_time=pygame.time.get_ticks()
	def update(self):
		now=pygame.time.get_ticks()
		#print(now)
		if now-self.last_time>30:
			if self.frame<len(launch_animation):
				self.image=pygame.transform.scale(launch_animation[self.frame],(15,20))
				self.image.set_colorkey((0,0,0))
				self.rect.y-=35
				self.last_time=now
			else:
				self.kill()

launch_animation=[]
for i in range(1,3):
	launch_dir=path.join(img_dir,'spaceRockets_00'+str(i)+'.png')
	launch_img=pygame.image.load(launch_dir).convert()
	launch_animation.append(launch_img)
launchs=pygame.sprite.Group()

explosion_animation=[]
for i in range(9):
	explosion_dir=path.join(img_dir,'regularExplosion0'+str(i)+'.png')
	explosion_img=pygame.image.load(explosion_dir).convert()
	explosion_animation.append(explosion_img)
explosions=pygame.sprite.Group()

player=Player()
littleenemys=pygame.sprite.Group()
enemys=pygame.sprite.Group()


# for i in range (0,5):
# 	enemy=Enemy()
# 	enemys.add(enemy)
bullets=pygame.sprite.Group()
last_enemy_generate_time=0

game_over=False
while not game_over:
	event_list=pygame.event.get()
	for event in event_list:
		if event.type==pygame.QUIT:
			game_over=True
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_ESCAPE:
				game_over=True
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_SPACE:
				player.shoot()
		now=pygame.time.get_ticks()
		if now-last_enemy_generate_time>NEW_ENEMY_GENERATE_INTERVAL:
			enemy=Enemy()
			enemys.add(enemy)
			last_enemy_generate_time=now

		hits1=pygame.sprite.groupcollide(enemys,bullets,True,True)
		for hit1 in hits1:
			littleenemy1=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy1)
			littleenemy2=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy2)
			explosion=Explosion(hit1.rect.center)
			explosions.add(explosion)

		hits2=pygame.sprite.groupcollide(littleenemys,bullets,True,True)

		hits3=pygame.sprite.spritecollide(player,enemys,False,pygame.sprite.collide_rect_ratio(0.7))
		if hits3:
			game_over=True
			

	screen.blit(background,background_rect)
	player.update()
	enemys.update()
	littleenemys.update()
	bullets.update()
	explosions.update()
	launchs.update()
	screen.blit(player.image,player.rect)
	enemys.draw(screen)
	littleenemys.draw(screen)
	bullets.draw(screen)
	explosions.draw(screen)
	launchs.draw(screen)
	
	time.sleep(0.05)
	pygame.display.flip()

Pygame项目_第3张图片
四、增加UI、音效、补给、菜单

import pygame
import random
import time
from os import path
img_dir=path.join(path.dirname(__file__),'img')
background_dir=path.join(img_dir,'background.png')
WIDTH,HEIGHT=480,600
NEW_ENEMY_GENERATE_INTERVAL=500

pygame.mixer.pre_init(44100,-16,2,2048)
pygame.mixer.init()
pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shooting gallery")
clock=pygame.time.Clock()

sound_dir=path.join(path.dirname(__file__),'Sounds')
shoot_sound=pygame.mixer.Sound(path.join(sound_dir,'Shoot.wav'))
explosion_sound=pygame.mixer.Sound(path.join(sound_dir,'Explosion.wav'))
select_sound=pygame.mixer.Sound(path.join(sound_dir,'Select.wav'))

background=pygame.image.load(background_dir).convert()
background_rect=background.get_rect()
player_dir=path.join(img_dir,'spaceShips_009.png')
player_img=pygame.image.load(player_dir).convert()
enemy_dir=path.join(img_dir,'spaceMeteors_003.png')
enemy_img=pygame.image.load(enemy_dir).convert()
bullet_dir=path.join(img_dir,'spaceMissiles_040.png')
bullet_img=pygame.image.load(bullet_dir).convert()
missile_dir=path.join(img_dir,'spaceMissiles_007.png')
missile_img=pygame.image.load(missile_dir).convert()
player_img_small=pygame.transform.scale(player_img,(26,20))
player_img_small.set_colorkey((0,0,0))

powerup_imgs={}
powerup_add_hp_dir=path.join(img_dir,'gem_red.png')
powerup_imgs['add_hp']=pygame.image.load(powerup_add_hp_dir).convert()
powerup_add_life_dir=path.join(img_dir,'heartFull.png')
powerup_imgs['add_life']=pygame.image.load(powerup_add_life_dir).convert()
powerup_add_missile_dir=path.join(img_dir,'gem_yellow.png')
powerup_imgs['add_missile']=pygame.image.load(powerup_add_missile_dir).convert()
powerups=pygame.sprite.Group()


def draw_ui():
	pygame.draw.rect(screen,(0,255,0),(10,10,player.hp,15))
	pygame.draw.rect(screen,(255,255,255),(10,10,100,15),2)
	font_name=pygame.font.match_font('arial')
	font=pygame.font.Font(font_name,20)
	text_surface=font.render(str(player.score),True,(255,255,255))
	text_rect=text_surface.get_rect()
	text_rect.midtop=(WIDTH/2,10)
	screen.blit(text_surface,text_rect)

def draw_text(text,surface,color,size,x,y):
	font_name=pygame.font.match_font('arial')
	font=pygame.font.Font(font_name,size)
	text_surface=font.render(text,True,color)
	text_rect=text_surface.get_rect()
	text_rect.midtop=(x,y)
	surface.blit(text_surface,text_rect)
def show_menu():
	global game_state,screen
	screen.blit(background,background_rect)

	draw_text('Space Shooter',screen,(255,255,255),40,WIDTH/2,100)
	draw_text('Press Space key to start',screen,(255,255,255),20,WIDTH/2,300)
	draw_text('Press Esc key to quit',screen,(255,255,255),20,WIDTH/2,350)

	event_list=pygame.event.get()
	for event in event_list:
		if event.type==pygame.QUIT:
			pygame.quit()
			quit()
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_ESCAPE:
				pygame.quit()
				quit()
		if event.type==pygame.KEYDOWN:
			if event.key==pygame.K_SPACE:
				game_state=1
	pygame.display.flip()

class PowerUp(pygame.sprite.Sprite):
	def __init__(self,center):
		pygame.sprite.Sprite.__init__(self)
		random_num=random.random()
		if random_num>=0 and random_num<0.5:
			self.type='add_hp'
		elif random_num>=0.5 and random_num<0.8:
			self.type='add_missile'
		else:
			self.type='add_life'
		self.image=powerup_imgs[self.type]
		self.rect=self.image.get_rect()
		self.image.set_colorkey((0,0,0))
		self.rect.center=center
	def update(self):
		self.rect.y+=25

class Missile(pygame.sprite.Sprite):
	def __init__(self,center):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(missile_img,(30,40))
		self.rect=self.image.get_rect()
		self.image.set_colorkey((0,0,0))
		self.rect.center=center
	def update(self):
		self.rect.y-=5

class Player(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(player_img,(35,35))
		self.image=pygame.transform.flip(player_img,False,True)
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=WIDTH/2
		self.rect.bottom=HEIGHT

		self.hp=100
		self.lives=3
		self.score=0
		self.hidden=False
		self.hide_time=0
	def update(self):
		keystate=pygame.key.get_pressed()
		if keystate[pygame.K_LEFT]:
			self.rect.centerx-=35
		if keystate[pygame.K_RIGHT]:
			self.rect.centerx+=35			
		if self.rect.left<0:
			self.rect.left=0
		if self.rect.right>WIDTH:
			self.rect.right=WIDTH
		now=pygame.time.get_ticks()
		if self.hidden and now-self.hide_time>1000:
			self.hidden=False
			self.rect.bottom=HEIGHT
	def shoot(self):
		bullet=Bullet(self.rect.centerx,self.rect.centery)
		bullets.add(bullet)
		shoot_sound.play()
		launch=Launch(self.rect.centerx,self.rect.centery)
		launchs.add(launch)

	def hide(self):
		self.hidden=True
		self.rect.y=-200
		select_sound.play()
		self.hide_time=pygame.time.get_ticks()

class Enemy(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		img_width=random.randint(20,60)
		self.image=pygame.transform.scale(enemy_img,(img_width,img_width))
		self.image.set_colorkey((0,0,0))
		self.image_origin=self.image.copy()
		self.rect=self.image.get_rect()
		self.rect.left=random.randint(0,WIDTH-self.rect.w)
		self.rect.bottom=0
		self.vx=random.randint(-2,2)
		self.vy=random.randint(2,10)
		self.radius=img_width
 
		self.last_time=pygame.time.get_ticks()
		self.rotate_speed=random.randint(-5,5)
		self.rotate_angle=0

	def update(self):
		#if 0<=self.rect.left<=WIDTH-self.rect.w and self.rect.top<=HEIGHT-self.rect.h:
			#self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
		self.rect.x+=self.vx
		self.rect.y+=self.vy
		self.rotate()
	def rotate(self):
		self.rotate_angle=(self.rotate_angle+self.rotate_speed)%360
		self.image=pygame.transform.rotate(self.image_origin,self.rotate_angle)

class LittleEnemy(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(enemy_img,(15,15))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
		self.vx=random.choice([-5,-10,-15,5,10,15])
		self.vy=random.choice([5,6,7,8,9,10])
	def update(self):
		#if 0<=self.rect.left<=WIDTH-self.rect.w and 0<=self.rect.top<=HEIGHT-self.rect.h:
			#self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
		self.rect.x+=self.vx
		self.rect.y+=self.vy

class Bullet(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(bullet_img,(15,20))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
	def update(self):
		self.rect.y-=35

class Explosion(pygame.sprite.Sprite):
	def __init__(self,center):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(explosion_animation[0],(30,30))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.center=center
		self.frame=0
		self.last_time=pygame.time.get_ticks()
	def update(self):
		now=pygame.time.get_ticks()
		if now-self.last_time>30:
			if self.frame<len(explosion_animation):
				self.image=pygame.transform.scale(explosion_animation[self.frame],(30,30))
				self.image.set_colorkey((0,0,0))
				self.frame+=1
				self.last_time=now
			else:
				self.kill()

class Launch(pygame.sprite.Sprite):
	def __init__(self,x,y):
		pygame.sprite.Sprite.__init__(self)
		self.image=pygame.transform.scale(launch_animation[0],(15,20))
		self.image.set_colorkey((0,0,0))
		self.rect=self.image.get_rect()
		self.rect.centerx=x
		self.rect.centery=y
		self.frame=0
		self.last_time=pygame.time.get_ticks()
	def update(self):
		now=pygame.time.get_ticks()
		#print(now)
		if now-self.last_time>30:
			if self.frame<len(launch_animation):
				self.image=pygame.transform.scale(launch_animation[self.frame],(15,20))
				self.image.set_colorkey((0,0,0))
				self.rect.y-=35
				self.last_time=now
			else:
				self.kill()
missiles=pygame.sprite.Group()

launch_animation=[]
for i in range(1,3):
	launch_dir=path.join(img_dir,'spaceRockets_00'+str(i)+'.png')
	launch_img=pygame.image.load(launch_dir).convert()
	launch_animation.append(launch_img)
launchs=pygame.sprite.Group()

explosion_animation=[]
for i in range(9):
	explosion_dir=path.join(img_dir,'regularExplosion0'+str(i)+'.png')
	explosion_img=pygame.image.load(explosion_dir).convert()
	explosion_animation.append(explosion_img)
explosions=pygame.sprite.Group()

player=Player()
littleenemys=pygame.sprite.Group()
enemys=pygame.sprite.Group()


# for i in range (0,5):
# 	enemy=Enemy()
# 	enemys.add(enemy)
bullets=pygame.sprite.Group()
last_enemy_generate_time=0

game_state=0
game_over=False

while not game_over:
	#clock.tick(30)
	if game_state==0:
		show_menu()	
	else:
		event_list=pygame.event.get()
		for event in event_list:
			if event.type==pygame.QUIT:
				pygame.quit()
				quit()
			if event.type==pygame.KEYDOWN:
				if event.key==pygame.K_ESCAPE:
					game_over=True
			if event.type==pygame.KEYDOWN:
				if event.key==pygame.K_SPACE:
					player.shoot()
		now=pygame.time.get_ticks()
		if now-last_enemy_generate_time>NEW_ENEMY_GENERATE_INTERVAL:
			enemy=Enemy()
			enemys.add(enemy)
			last_enemy_generate_time=now

		hits1=pygame.sprite.groupcollide(enemys,bullets,True,True)
		for hit1 in hits1:
			littleenemy1=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy1)
			littleenemy2=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy2)
			explosion=Explosion(hit1.rect.center)
			explosions.add(explosion)
			explosion_sound.play()
			player.score+=hit1.radius 
			if random.random()>0.9:
				powerup=PowerUp(hit1.rect.center)
				powerups.add(powerup)

		hits2=pygame.sprite.groupcollide(littleenemys,bullets,True,True)

		hits3=pygame.sprite.spritecollide(player,enemys,True,pygame.sprite.collide_rect_ratio(0.9))
		for hit3 in hits3:
			player.hp-=hit3.radius
			if player.hp<0:
				player.hide()
				player.lives-=1
				player.hp=100
				if player.lives==0:
					game_over=True
			
		hits4=pygame.sprite.spritecollide(player,powerups,True,pygame.sprite.collide_rect_ratio(0.9))
		for hit4 in hits4:
			if hit4.type=='add_hp':
				player.hp+=50
				if player.hp>100:
					player.hp=100 
			elif hit4.type=='add_life':
				player.lives+=1
				if player.lives>3:
					player.lives=3
			elif hit4.type=='add_missile':
				missile=Missile(hit4.rect.center)
				missiles.add(missile)	
			else:
				pass

		hits1=pygame.sprite.groupcollide(enemys,missiles,True,True)
		for hit1 in hits1:
			littleenemy1=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy1)
			littleenemy2=LittleEnemy(hit1.rect.centerx,hit1.rect.centery)
			littleenemys.add(littleenemy2)
			explosion=Explosion(hit1.rect.center)
			explosions.add(explosion)
			explosion_sound.play()
			player.score+=hit1.radius 
			if random.random()>0.9:
				powerup=PowerUp(hit1.rect.center)
				powerups.add(powerup)

		screen.blit(background,background_rect)
		img_rect=player_img_small.get_rect()
		img_rect.x=WIDTH-(img_rect.w+10)
		img_rect.y=10
		for i in range(player.lives):
			screen.blit(player_img_small,img_rect)
			img_rect.x-=img_rect.w+10
		player.update()
		enemys.update()
		littleenemys.update()
		bullets.update()
		explosions.update()
		launchs.update()
		screen.blit(player.image,player.rect)
		enemys.draw(screen)
		littleenemys.draw(screen)
		bullets.draw(screen)
		explosions.draw(screen)
		launchs.draw(screen)
		powerups.update()
		powerups.draw(screen)
		missiles.update()
		missiles.draw(screen)
		draw_ui()
		time.sleep(0.05)
		pygame.display.flip()

Pygame项目_第4张图片
我将游戏打包成exe文件了。
Pygame项目_第5张图片
Pygame项目_第6张图片

你可能感兴趣的:(Python)