今天来改进“在无聊的时候玩小游戏”里的不足
图片:
代码:
原版:
import pygame
from pygame import locals
import random, time, math, sys
class BulletSprite(pygame.sprite.Sprite):
#敌机子弹初始化
def __init__(self,imagepath,pos,screen,m):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (10,10))
self.rect = self.image.get_rect()
self.pos = pos
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
#============== start ================
# 设置相关属性
#============== start ================
def move(self):
self.rect.y-=self.m[1]
self.rect.x+=self.m[0]
#============== start ================
# 子弹移动方法
#============== start ================
def update(self):
self.move()
class Zombie(pygame.sprite.Sprite):
def __init__(self,imagepath,pos,screen,m,score,hp):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (40,50))
self.rect = self.image.get_rect()
self.pos = pos
self.hp=3
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
self.score=score
def move(self,s):
self.rect.y+=1
self.rect.x+=self.m
if self.rect.bottom==680:
print('僵尸吃掉了你的脑子!!!')
print(s)
pygame.quit()
sys.exit()
if self.rect.x<=0 or self.rect.right>=640:
self.m=-self.m
def update(self,s):
self.move(s)
if self.hp<=0:
# self.score+=1
self.kill()
class Plant(pygame.sprite.Sprite):
def __init__(self,imagepath,pos,screen,m):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (40,50))
self.rect = self.image.get_rect()
self.pos = pos
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
def move(self):
self.rect.y+=1
self.rect.x+=self.m
if self.rect.x<=0 or self.rect.right>=640:
self.m=-self.m
def update(self):
self.move()
def main():
pygame.init()
FPSclock = pygame.time.Clock()
screen = pygame.display.set_mode((640,680))
image_bg = pygame.image.load("./images/地图.png")
image_bg = pygame.transform.smoothscale(image_bg, (640,680))
mouse_cursor = pygame.image.load("./images/准星.png")
mouse_cursor = pygame.transform.smoothscale(mouse_cursor, (25, 25))
pygame.mouse.set_visible(False)
group_zombie = pygame.sprite.Group()
group_plant = pygame.sprite.Group()
group_bullet = pygame.sprite.Group()
group_bullet1 = pygame.sprite.Group()
font = pygame.font.Font("stxingka.ttf",32)
n=0
nn=0
d=int(input('dif:'))
while d not in range(1,4):
d=int(input('dif:'))
score=0
while True:
nn+=1
FPSclock.tick(200)
screen.blit(image_bg, (0, 0))
n+=1
screen.blit(font.render("Score:"+str(score) , True,(255, 0, 0)),(20,20))
if n==100:
n=0
if d==1:
zomb=Zombie('./images/Zombie_0.png',[random.randint(0,600),0],random.randint(0,3),random.randint(0,2),score,3)
if d==2:
zomb=Zombie('./images/Zombie_0.png',[random.randint(0,600),0],random.randint(3,5),random.randint(1,3),score,5)
if d==3:
zomb=Zombie('./images/Zombie_0.png',[random.randint(0,600),0],random.randint(3,5),random.randint(1,3),score,7)
group_zombie.add(zomb)
# score=zomb.score
if d==3:
if nn==150:
nn=0
plnt=Plant('./images/Peashooter_0.png',[random.randint(0,600),0],1,random.randint(0,2))
group_plant.add(plnt)
for event in pygame.event.get():
position = pygame.mouse.get_pos()
isPressed = pygame.mouse.get_pressed()[0]
if event.type == locals.QUIT:
print(score)
pygame.quit()
sys.exit()
#============== start ================
# 当点击鼠标,从两个位置朝鼠标坐标点发射两颗子弹
#============== end ================
if isPressed:
a=BulletSprite('./images/子弹.png',[position[0]-680+position[1]+12,680],screen,[3,3])
aa=BulletSprite('./images/子弹.png',[position[0]+680-position[1]+12,680],screen,[-3,3])
group_bullet.add(a)
group_bullet1.add(aa)
# music = pygame.mixer.music
# music.load("./music/射击.mp3") # 加载音乐
# music.play(1)
group_bullet.update()
group_bullet.draw(screen)
group_bullet1.update()
group_bullet1.draw(screen)
group_zombie.update(score)
group_zombie.draw(screen)
group_plant.update()
group_plant.draw(screen)
col=pygame.sprite.groupcollide(group_bullet,group_bullet1,True,True)
col1=pygame.sprite.groupcollide(group_bullet1,group_zombie,True,False)
col2=pygame.sprite.groupcollide(group_bullet,group_zombie,True,False)
col3=pygame.sprite.groupcollide(group_bullet,group_plant,True,False)
col4=pygame.sprite.groupcollide(group_bullet1,group_plant,True,False)
if col1:
z=list(col1.values())[0][0]
z.hp-=1
if z.hp==0:
score+=d*2
if col2:
z=list(col2.values())[0][0]
z.hp-=1
if z.hp==0:
score+=d*2
if col3 or col4:
print('击杀豌豆,误伤队友,失败!!!')
print(score)
pygame.quit()
sys.exit()
screen.blit(mouse_cursor, position)
pygame.display.update()
if __name__ == "__main__":
main()
1.14版:
import pygame
from pygame import locals
import random, time, math, sys
class BulletSprite(pygame.sprite.Sprite):
def __init__(self,imagepath,pos,screen,m):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (10,10))
self.rect = self.image.get_rect()
self.pos = pos
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
self.a=0
self.screen=screen
#============== start ================
# 设置相关属性
#============== start ================
def move(self):
self.rect.y-=self.m[1]
self.rect.x+=self.m[0]+self.m[2]
if self.rect.x<=0 or self.rect.right>=640:
self.m[0]=-self.m[0]
if self.rect.y<=0 or self.rect.bottom>=680:
self.m[1]=-self.m[1]
#============== start ================
# 子弹移动方法
#============== start ================
def update(self):
self.move()
class Zombie(pygame.sprite.Sprite):
def __init__(self,imagepath,pos,screen,m,score,hp):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (40,50))
self.rect = self.image.get_rect()
self.pos = pos
self.hp=hp
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
self.score=score
self.screen=screen
def move(self,s):
self.rect.y+=1
self.l=['a','a','a','na','na','na','na','na','na','na']
self.pik=random.choice(self.l)
if self.pik=='a':
self.rect.y+=10
self.rect.x+=self.m
if self.rect.bottom>=680:
print('僵尸吃掉了你的脑子!!!')
print(s)
pygame.quit()
sys.exit()
if self.rect.x<=0 or self.rect.right>=640:
self.m=-self.m
def fire(self,b,g):
l=[-5,0,5]
for x in range(3):
a=b('./images/PeaNormal_0.png',[self.rect.x,self.rect.y],self.screen,[l[x],1,self.m])
g.add(a)
def update(self,b,g,s):
self.fire(b,g)
self.move(s)
if self.hp<=0:
# self.score+=1
self.kill()
class Plant(pygame.sprite.Sprite):
def __init__(self,imagepath,pos,screen,m):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.smoothscale(self.image, (40,40))
self.rect = self.image.get_rect()
self.screen=screen
self.pos = pos
self.rect.x = pos[0]
self.rect.y = pos[1]
self.m=m
self.a=30
self.g=pygame.sprite.Group()
def move(self):
self.rect.y+=self.a
self.rect.x+=self.m
if self.rect.x<=0 or self.rect.right>=640:
self.m=-self.m
if self.rect.y<=0 or self.rect.bottom>=680:
self.a=-self.a
# if self.rect.bottom>=680:
# self.kill()
def fire(self,b,g):
a=b('./images/PeaNormal_0.png',[self.rect.x,self.rect.y],self.screen,[self.m,1,0])
g.add(a)
def update(self,b,g):
self.fire(b,g)
self.move()
def main():
pygame.init()
FPSclock = pygame.time.Clock()
screen = pygame.display.set_mode((640,680))
image_bg = pygame.image.load("./images/地图.png")
image_bg = pygame.transform.smoothscale(image_bg, (640,680))
mouse_cursor = pygame.image.load("./images/准星.png")
mouse_cursor = pygame.transform.smoothscale(mouse_cursor, (25, 25))
pygame.mouse.set_visible(False)
group_zombie = pygame.sprite.Group()
group_plant = pygame.sprite.Group()
group_bullet = pygame.sprite.Group()
group_bullet1 = pygame.sprite.Group()
group_b = pygame.sprite.Group()
font = pygame.font.Font("stxingka.ttf",32)
n=0
nn=0
ax=False
ap=0
pn=0
d=int(input('dif:'))
while d not in range(1,4):
d=int(input('dif:'))
score=0
if d==3:
plnt=Plant('./images/Peashooter_0.png',[random.randint(0,600),0],1,random.randint(0,2))
group_plant.add(plnt)
pn+=1
ppp=True
aax=False
while True:
# nn+=1
if ap>300:
ap=0
ax=False
FPSclock.tick(200)
screen.blit(image_bg, (0, 0))
n+=1
keys = pygame.key.get_pressed()
if keys[locals.K_x]:
if not aax:
ax=True
if pn>30:
pn=0
group_plant.empty()
if pn==0:
a=BulletSprite('./images/PeaNormal_0.png',[random.randint(0,630),0],screen,[5,1,0])
group_b.add(a)
if n==10:
n=0
zomb=Zombie('./images/Zombie_0.png',[random.randint(0,600),0],random.randint(3,5),random.randint(1,3),score,15)
group_zombie.add(zomb)
# score=zomb.score
aaaa=100
aaaa//=16
print(aaaa)
if d==3:
if nn==500:
nn=500
for x in range(5):
plnt=Plant('./images/Peashooter_0.png',[random.randint(0,600),0],screen,10)
group_plant.add(plnt)
# pn+=1
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
isPressed = pygame.mouse.get_pressed()[0]
if event.type == locals.QUIT:
print(score)
pygame.quit()
sys.exit()
# 当点击鼠标,从两个位置朝鼠标坐标点发射两颗子弹
#============== end ================
if not isPressed:
n1=pos[0]
n2=pos[0]+10
n3=pos[0]-10
if n1<40:
n1=40
if n1>600:
n1=600
if n2<40:
n2=40
if n2>600:
n2=600
if n3<40:
n3=40
if n3>600:
n3=600
plnt=Plant('./images/Peashooter_0.png',[n1,0],screen,random.randint(5,15))
group_plant.add(plnt)
plnt=Plant('./images/Peashooter_0.png',[n2,0],screen,random.randint(5,15))
group_plant.add(plnt)
plnt=Plant('./images/Peashooter_0.png',[n3,0],screen,random.randint(5,15))
group_plant.add(plnt)
pn+=1
# music = pygame.mixer.music
# music.load("./music/射击.mp3") # 加载音乐
# music.play(1)
group_bullet.update()
group_bullet.draw(screen)
group_bullet1.update()
group_bullet1.draw(screen)
group_zombie.update(BulletSprite,group_b,score)
group_zombie.draw(screen)
group_plant.update(BulletSprite,group_b)
group_plant.draw(screen)
if d==3:
group_b.update()
screen.blit(font.render("Score:"+str(score) , True,(255, 0, 0)),(20,20))
if d==3:
group_b.draw(screen)
screen.blit(font.render("plantspawntime:"+str(500-nn) , True,(255, 0, 0)),(20,40))
screen.blit(font.render("zombiespawntime:"+str(10-n) , True,(255, 0, 0)),(20,60))
screen.blit(font.render("plantnumber:"+str(pn) , True,(255, 0, 0)),(20,80))
screen.blit(font.render("ax:"+str(ax) , True,(255, 0, 0)),(20,100))
if ax:
ap+=1
screen.blit(font.render("ap:"+str(ap) , True,(255, 0, 0)),(20,120))
n=0
aax=True
pn=0
group_plant.empty()
# col=pygame.sprite.groupcollide(group_bullet,group_bullet1,True,True)
if d==3:
col=pygame.sprite.groupcollide(group_zombie,group_plant,False,False)
if col:
z=list(col.values())[0][0]
z.m=-z.m
z.a=-z.a
# col=pygame.sprite.groupcollide(plnt.g,group_bullet1,True,True)
col1=pygame.sprite.groupcollide(group_bullet1,group_zombie,True,False)
col2=pygame.sprite.groupcollide(group_bullet,group_zombie,True,False)
col3=pygame.sprite.groupcollide(group_bullet,group_plant,True,False)
col4=pygame.sprite.groupcollide(group_bullet1,group_plant,True,False)
if d==3:
col5=pygame.sprite.groupcollide(group_b,group_zombie,True,False)
if col5:
z=list(col5.values())[0][0]
print(z)
z.hp-=1
if z.hp==0:
if d==1:
score+=2
elif d==2:
score+=5
else:
score+=1
if col1:
z=list(col1.values())[0][0]
z.hp-=1
if z.hp==0:
if d==1:
score+=2
elif d==2:
score+=5
else:
score+=1
if col2:
z=list(col2.values())[0][0]
z.hp-=1
if z.hp==0:
if d==1:
score+=2
elif d==2:
score+=5
else:
score+=1
if col3 or col4:
print('击杀豌豆,误伤队友,失败!!!')
print(score)
pygame.quit()
sys.exit()
screen.blit(mouse_cursor, pos)
pygame.display.update()
if __name__ == "__main__":
main()
(全文完)