马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 juhugufudu 于 2019-9-13 21:38 编辑 import pygame
from pygame.locals import *
import time
import random
count = 0
y = 0
class HeroPlane(object):
"""docstring for HeroPlane"""
def __init__(self,screen):
self.x = 210
self.y = 700
self.image = pygame.image.load("./feiji/hero1.png")
self.screen = screen
#存子弹
self.bullet_list = []
def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
'''bullet 是一个对象'''
if bullet.judge():
self.bullet_list.remove(bullet)
else:
bullet.checkHit()
bullet.display()
bullet.move()
def move_left(self):
self.x -=7
def move_right(self):
self.x +=7
def fire(self):
self.bullet_list.append(Bullet(self.x,self.y,self.screen))
class EnemyPlane(object):
"""docstring for EnemyPlane"""
def __init__(self,screen):
self.x = 0
self.y = 0
self.image = pygame.image.load("./feiji/enemy0.png")
self.screen = screen
self.bullet_list = []
#判断
self.direction = "right"
def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
'''bullet 是一个对象'''
if bullet.judge():
self.bullet_list.remove(bullet)
else:
bullet.display()
bullet.move()
def move(self):
global y
if self.direction == "right":
self.x +=5
y = self.x
elif self.direction == "left":
self.x -=5
y = self.x
if self.x>480-50:
self.direction = "left"
elif self.x <0:
self.direction = "right"
def fire(self):
self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
class EnemyBullet(object):
def __init__(self,screen,x,y):
self.screen = screen
self.x = x+25
self.y = y+40
self.image = pygame.image.load("./feiji/bullet1.png")
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def move(self):
self.y += 5
def judge(self):
'''判断是否越界'''
if self.y >852:
return True
else:
return False
class Bullet(object):
"""docstring for Bullet"""
def __init__(self,x,y,screen_temp):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./feiji/bullet.png")
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def move(self):
self.y-=20
def checkHit(self):
global count
global y
#print(self.x)
if y <= self.x+10 and y >= self.x-10 and self.y ==0:
count+=1
print(count)
def judge(self):
'''判断是否越界'''
if self.y <0:
return True
else:
return False
def key_control(hero):
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
hero.move_left()
elif event.key == K_RIGHT or event.key == K_d:
hero.move_right()
elif event.key == K_SPACE:
hero.fire()
def main():
#显示窗口
screen = pygame.display.set_mode((480,852),0,32)
#显示背景
background = pygame.image.load("./feiji/background.png")
#显示飞机
hero = HeroPlane(screen)
enemy = EnemyPlane(screen)
#while循环一直显示
while True:
screen.blit(background,(0,0))
hero.display()
#显示敌机
enemy.display()
enemy.move()
num = random.randint(1,30)
if num == 9:
enemy.fire()
#更新
pygame.display.update()
#键盘输入
key_control(hero)
#休息
time.sleep(0.01)
if __name__ == '__main__':
main()
'''
误差方法
y = 0
x 误差 10px
'''
效果简单到包...(大神·勿喷)
|