码迷,mamicode.com
首页 > 其他好文 > 详细

pygame 打飞机实现

时间:2015-06-07 18:41:09      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

设置了两个子弹 和敌机两个类

很容易实现  直接上代码吧   实现效果如图  原谅我傻逼的画风

 

技术分享

 

 1 #coding:utf-8
 2 import pygame
 3 import random
 4 from sys import exit
 5 class Bullet:
 6     def __init__(self):
 7         self.x=0
 8         self.y=-1
 9         self.image=pygame.image.load("bullet.png").convert_alpha()
10         self.active=False
11 
12     def move(self):
13         if self.active:
14                 self.y-=3
15         if self.y<0:
16             self.active=False
17             
18     def restart(self):                       #重新设置bullet的位置
19         mouseX,mouseY=pygame.mouse.get_pos()
20         self.x = mouseX -self.image.get_width() /2
21         self.y = mouseY -self.image.get_height() / 2
22         self.active=True
23 
24 class Enemy:
25     def restart(self):
26         self.x=random.randint(0,225)   #让坐标随机
27         self.y=random.randint(-250,-200)
28         self.speed=random.random()+0.1
29 
30     def __init__(self):
31         self.restart()
32         self.image=pygame.image.load("enemy.png").convert_alpha()
33 
34     def move(self):
35         if self.y<400:
36             self.y+=self.speed    
37         else:
38             self.restart()     #若冲出屏幕则重新start
39 
40 def checkHit(enemy, bullet):    #碰撞检测
41     if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()/2) and (bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()/2):
42         enemy.restart()
43         bullet.active = False    
44 
45 pygame.init()
46     
47 screen=pygame.display.set_mode((450,800),0,32)
48 pygame.display.set_caption("Hello,world")
49 background=pygame.image.load("back.jpg")
50 plane=pygame.image.load("plane.png").convert_alpha()
51 
52 
53 bullets=[] #创建子弹的list
54 for i in range(5):            #创建五发子弹        
55     bullets.append(Bullet())
56 count_b=len(bullets)
57 index_b=0                #发射子弹的序号
58 interval_b=0            #发射子弹的间隔时间
59 
60 enemies=[]
61 for e in range(5):
62     enemies.append(Enemy())
63 
64 while True:
65     for event in pygame.event.get():
66         if event.type ==pygame.QUIT:
67             pygame.quit()
68             exit()
69 
70     screen.blit(background,(0,0))
71     
72     interval_b-=1
73     if interval_b<0:
74         bullets[index_b].restart()
75         interval_b=50                    
76         index_b=(index_b+1)%count_b        #序号循环
77 
78     for b in bullets:                #发射一发子弹
79         if b.active:
80             b.move()
81             screen.blit(b.image,(b.x,b.y))
82 
83     for e in enemies:
84         e.move()
85         screen.blit(e.image,(e.x,e.y))
86 
87     for b in bullets:
88         if b.active:
89             for e in enemies:
90                 checkHit(e,b)
91 
92     x,y=pygame.mouse.get_pos()
93     x-=plane.get_width()/2
94     y-=plane.get_height()/2
95 
96     screen.blit(plane,(x,y))
97     pygame.display.update()

 

pygame 打飞机实现

标签:

原文地址:http://www.cnblogs.com/threezj/p/4558795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!