游戏的原理
while True:
1.检测事件,如果有事件就控制相应的图片移动
2. 把所有的图片重新画一遍
创建背景
import pygame
import time
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
while True:
screen.blit.(background,(0,0))
pygame.display.update()
time.sleep(0.01)
if __name__ == "__main__":
main()
显示飞机
import pygame
import time
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = pygame.image.load("./feiji/hero1.png")
while True:
screen.blit.(background,(0,0))
screen.blit.(bero,(210,700))
pygame.display.update()
time.sleep(0.01)
if __name__ == "__main__":
main()
检测键盘和左右移动
import pygame
from pygame.locals import *
import time
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = pygame.image.load("./feiji/hero1.png")
x = 210
y = 700
while True:
screen.blit.(background,(0,0))
screen.blit.(bero,(x,y))
pygame.display.update()
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
x-=5
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
x+=5
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
time.sleep(0.01)
if __name__ == "__main__":
main()
面向对象
import pygame
from pygame.locals import *
import time
class HeroPlane(object):
def __init__(self,screen_temp):
self.x = 210
self.y = 700
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
def display(self):
screen.blit(self.image,(self.x,self.y))
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
while True:
screen.blit.(background,(0,0))
hero.display()
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
发射子弹
import pygame
from pygame.locals import *
import time
class HeroPlane(object):
def __init__(self,screen_temp):
self.x = 210
self.y = 700
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class Bullet(object):
def __init__(self,screen_temp,):
self.x = x+40
self.y = y-20
self.scrren = 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-=5
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
while True:
#设定需要显示的背景图
screen.blit.(background,(0,0))
hero.display()
#更新需要显示的内容
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
显示敌机
import pygame
from pygame.locals import *
import time
class HeroPlane(object):
def __init__(self,screen_temp):
self.x = 210
self.y = 700
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class EnemyPlane(object):
def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
#self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
screen.blit(self.image,(self.x,self.y))
#for bullet in self.bullet_list:
# bullet.display()
# bullet.move()
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class Bullet(object):
def __init__(self,screen_temp,):
self.x = x+40
self.y = y-20
self.scrren = 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-=5
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
#4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
#设定需要显示的背景图
screen.blit.(background,(0,0))
hero.display()
enemy.display()
#更新需要显示的内容
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
控制敌机左右移动
import pygame
from pygame.locals import *
import time
class HeroPlane(object):
def __init__(self,screen_temp):
self.x = 210
self.y = 700
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class EnemyPlane(object):
def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
#self.bullet_list = []#存储发射出去的子弹对象引用
self.direction = "right"#用来存储飞机默认的显示方向
def display(self):
screen.blit(self.image,(self.x,self.y))
#for bullet in self.bullet_list:
# bullet.display()
# bullet.move()
def move(self):
if self.direction=="right"
self.x+=5
elif self.direction=="left":
self.x-=5
if self.x>480-50:
self.direction = "left"
elif self.x<0:
self.direction = "right"
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class Bullet(object):
def __init__(self,screen_temp,):
self.x = x+40
self.y = y-20
self.scrren = 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-=5
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
#4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
#设定需要显示的背景图
screen.blit.(background,(0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
#更新需要显示的内容
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
代码优化:子弹越界问题
for循环中可能出现的bug,删不干净
a = [11,22,33,44,55,66,77]
#for i in a:
# if i==33:
# a.remove(i)#这种得到的a列表为[11,22,44,55,66,77]可能会出现漏删
b = []
for i in a:
if i==33 or i==44:
b.append(i)
for i in b:
a.remove(i)
敌机
import pygame
from pygame.locals import *
import time
import random
class HeroPlane(object):
def __init__(self,screen_temp):
self.x = 210
self.y = 700
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
if bullet.judge():#判断子弹是否越界
self.bullet_list.remove(bullet)
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class EnemyPlane(object):
def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.scrren = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
self.bullet_list = []#存储发射出去的子弹对象引用
self.direction = "right"#用来存储飞机默认的显示方向
def display(self):
screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move(self):
if self.direction=="right"
self.x+=5
elif self.direction=="left":
self.x-=5
if self.x>480-50:
self.direction = "left"
elif self.x<0:
self.direction = "right"
def fire(self):
random_num = random.randint(1,100)
if random_num == 2 or random_num ==20:
self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
class Bullet(object):
def __init__(self,screen_temp,):
self.x = x+40
self.y = y-20
self.scrren = 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-=5
def judge(self):
if self.y<0:#测试是否越界可以整个任意值,如200,以判断程序是否有bug
return True
else:
return False
class EnemyBullet(object):
def __init__(self,screen_temp,):
self.x = x+25
self.y = y+40
self.scrren = screen_temp
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:#测试是否越界可以整个任意值,如200,以判断程序是否有bug
return True
else:
return False
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
#4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
#设定需要显示的背景图
screen.blit.(background,(0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
enemy.fire()#敌机开火
#更新需要显示的内容
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
抽取基类
import pygame
from pygame.locals import *
import time
import random
class BasePlane(object):
def __init__(self,screen_temp,x,y,image_name):
self.x = x
self.y = y
self.scrren = screen_temp
self.image = pygame.image.load(image_name)
self.bullet_list = []#存储发射出去的子弹对象引用
def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
if bullet.judge():#判断子弹是否越界
self.bullet_list.remove(bullet)
class HeroPlane(object):
def __init__(self,screen_temp,x,y,image_name):
BasePlane.__init__(self,screen_temp,210,700,"./feiji/hero1.png") #super().__init__()
def move_left(self):
self.x-=5
def move_right(self):
self.x+=5
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class EnemyPlane(object):
def __init__(self,screen_temp,x,y,image_name):
BasePlane.__init__(self,screen_temp,0,0,"./feiji/enemy0.png")
self.direction = "right"#用来存储飞机默认的显示方向
def move(self):
if self.direction=="right"
self.x+=5
elif self.direction=="left":
self.x-=5
if self.x>480-50:
self.direction = "left"
elif self.x<0:
self.direction = "right"
def fire(self):
random_num = random.randint(1,100)
if random_num == 2 or random_num ==20:
self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
class BaseBullet(object):
def __init__(self,screen_temp,x,y,image_name):
self.x = x
self.y = y
self.scrren = screen_temp
self.image = pygame.image.load(image_name)
def display(self):
self.screen.blit(self.image,(self.x,self.y))
class Bullet(object):
def __init__(self,screen_temp,x,y,image_name):
BaseBullet.__init__(self,screen_temp,x+40,y-20,"./feiji/bullet.png")
def move(self):
self.y-=5
def judge(self):
if self.y<0:#测试是否越界可以整个任意值,如200,以判断程序是否有bug
return True
else:
return False
class EnemyBullet(object):
def __init__(self,screen_temp,x,y,image_name):
BaseBullet.__init__(self,screen_temp,x+25,y+40,"./feiji/bullet1.png")
def move(self):
self.y-=5
def judge(self):
if self.y>852:#测试是否越界可以整个任意值,如200,以判断程序是否有bug
return True
else:
return False
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机的图片
hero = HeroPlane()
#4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
#设定需要显示的背景图
screen.blit.(background,(0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
enemy.fire()#敌机开火
#更新需要显示的内容
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()