一、搭建界面
- 代码:
#coding=utf-8 import pygame ''' 1. 搭建界面,主要完成窗口和背景图的显示 ''' def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3. 把背景图片放到窗口中显示 while True: #设定需要显示的背景图 screen.blit(background,(0,0)) #更新需要显示的内容 pygame.display.update() if __name__ == "__main__": main()
结果:
二、检测键盘
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 2. 用来检测事件,比如按键操作 ''' def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3. 把背景图片放到窗口中显示 while True: #设定需要显示的背景图 screen.blit(background,(0,0)) #获取事件,比如按键等 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') #检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print('right') #检测按键是否是空格键 elif event.key == K_SPACE: print('space') #更新需要显示的内容 pygame.display.update() if __name__ == "__main__": main()
结果:
三、显示、控制玩具飞机-面向过程
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 3. 使用面向过程的方式来显示一个飞机,并控制其左右移动 ''' def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #测试,用来创建一个玩家飞机的图片 hero = pygame.image.load("../feiji/hero1.png") #用来保存飞机的x,y坐标 x=0 y=0 #3. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) #设定需要显示的飞机图片 screen.blit(hero,(x,y)) #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') #控制飞机让其向左移动 x-=5 elif event.key == K_d or event.key == K_RIGHT: print('right') #控制飞机让其向右移动 x+=5 elif event.key == K_SPACE: print('space') pygame.display.update() if __name__ == "__main__": main()
结果:
四、显示、控制玩具飞机-面向对象
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 4. 使用面向对象的方式显示飞机,以及控制其左右移动 接下来要做的任务: 1. 实现飞机在你想要的位置显示 2. 实现按键控制飞机移动 ''' class HeroPlane(object): def __init__(self, screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen #用来保存英雄飞机需要的图片名字 self.imageName = "../feiji/hero1.png" #根据名字生成飞机图片 self.image = pygame.image.load(self.imageName) def display(self): self.screen.blit(self.image,(self.x,self.y)) def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3. 创建一个飞机对象 heroPlane = HeroPlane(screen) #3. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() key_control(heroPlane) pygame.display.update() if __name__ == "__main__": main()
结果:
五、玩家飞机发射子弹
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 5. 实现玩家飞机发射子弹 接下来要做的任务: 1. 实现飞机在你想要的位置显示 2. 实现按键控制飞机移动 3. 实现按下空格键的时候,显示一颗子弹 ''' class HeroPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/hero1.png" self.image = pygame.image.load(self.imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen) self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen): self.x = x+40 self.y = y-20 self.screen = screen self.image = pygame.image.load("../feiji/bullet.png") def move(self): self.y -= 5 def display(self): self.screen.blit(self.image,(self.x,self.y)) def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3. 创建一个飞机对象 heroPlane = HeroPlane(screen) #3. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() key_control(heroPlane) pygame.display.update() if __name__ == "__main__": main()
结果:
六、显示敌机
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 6. 显示敌人飞机 ''' class HeroPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/hero1.png" self.image = pygame.image.load(self.imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen) self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen): self.x = x+40 self.y = y-20 self.screen = screen self.image = pygame.image.load("../feiji/bullet.png") def move(self): self.y -= 5 def display(self): self.screen.blit(self.image,(self.x,self.y)) class EnemyPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 0 self.y = 0 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/enemy0.png" self.image = pygame.image.load(self.imageName) def display(self): self.screen.blit(self.image,(self.x,self.y)) def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3.1 创建一个飞机对象 heroPlane = HeroPlane(screen) #3.2 创建一个敌人飞机 enemyPlane = EnemyPlane(screen) #4. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() enemyPlane.display() key_control(heroPlane) pygame.display.update() if __name__ == "__main__": main()
结果:
七、优化代码
- 代码:
#coding=utf-8 import pygame from pygame.locals import * ''' 7. 优化代码:优化发射出的子弹 ''' class HeroPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/hero1.png" self.image = pygame.image.load(self.imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) #判断一下子弹的位置是否越界,如果是,那么就要删除这颗子弹 # #这种方法会漏掉很多需要删除的数据 # for i in self.bulletList: # if i.y<0: # self.bulletList.remove(i) #用来存放需要删除的对象引用 needDelItemList = [] #保存需要删除的对象 for i in self.bulletList: if i.judge(): needDelItemList.append(i) #删除self.bulletList中需要删除的对象 for i in needDelItemList: self.bulletList.remove(i) #因为needDelItemList也保存了刚刚删除的对象的引用,所以可以删除整个列表,那么 #整个列表中的引用就不存在了,也可以不调用下面的代码,因为needDelItemList是局部变量 #当这个方法的调用结束时,这个局部变量也就不存在了 # del needDelItemList for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen) self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen): self.x = x+40 self.y = y-20 self.screen = screen self.image = pygame.image.load("../feiji/bullet.png") def move(self): self.y -= 5 def display(self): self.screen.blit(self.image,(self.x,self.y)) def judge(self): if self.y<0: return True else: return False class EnemyPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 0 self.y = 0 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/enemy0.png" self.image = pygame.image.load(self.imageName) def display(self): self.screen.blit(self.image,(self.x,self.y)) def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3.1 创建一个飞机对象 heroPlane = HeroPlane(screen) #3.2 创建一个敌人飞机 enemyPlane = EnemyPlane(screen) #4. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() enemyPlane.display() key_control(heroPlane) pygame.display.update() if __name__ == "__main__": main()
结果:
八、让敌机移动
- 代码:
#coding=utf-8 import pygame from pygame.locals import * import time ''' 8. 让敌机移动 ''' class HeroPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/hero1.png" self.image = pygame.image.load(self.imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) #判断一下子弹的位置是否越界,如果是,那么就要删除这颗子弹 # #这种方法会漏掉很多需要删除的数据 # for i in self.bulletList: # if i.y<0: # self.bulletList.remove(i) #用来存放需要删除的对象引用 needDelItemList = [] #保存需要删除的对象 for i in self.bulletList: if i.judge(): needDelItemList.append(i) #删除self.bulletList中需要删除的对象 for i in needDelItemList: self.bulletList.remove(i) #因为needDelItemList也保存了刚刚删除的对象的引用,所以可以删除整个列表,那么 #整个列表中的引用就不存在了,也可以不调用下面的代码,因为needDelItemList是局部变量 #当这个方法的调用结束时,这个局部变量也就不存在了 # del needDelItemList for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen) self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen): self.x = x+40 self.y = y-20 self.screen = screen self.image = pygame.image.load("../feiji/bullet.png") def move(self): self.y -= 5 def display(self): self.screen.blit(self.image,(self.x,self.y)) def judge(self): if self.y<0: return True else: return False class EnemyPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 0 self.y = 0 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/enemy0.png" self.image = pygame.image.load(self.imageName) self.direction = "right" def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): #如果碰到了右边的边界,那么就往左走,如果碰到了左边的边界,那么就往右走 if self.direction == "right": self.x += 4 elif self.direction == "left": self.x -= 4 if self.x>480-50: self.direction = "left" elif self.x<0: self.direction = "right" def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3.1 创建一个飞机对象 heroPlane = HeroPlane(screen) #3.2 创建一个敌人飞机 enemyPlane = EnemyPlane(screen) #4. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() enemyPlane.move() enemyPlane.display() key_control(heroPlane) pygame.display.update() #通过延时的方式,来降低这个while循环的循环速度,从而降低了cpu占用率 time.sleep(0.01) if __name__ == "__main__": main()
结果:
九、敌机发射子弹
- 代码:
#coding=utf-8 import pygame from pygame.locals import * import time import random ''' 9. 让敌机发射子弹 ''' class HeroPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 230 self.y = 580 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/hero1.png" self.image = pygame.image.load(self.imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) #判断一下子弹的位置是否越界,如果是,那么就要删除这颗子弹 # #这种方法会漏掉很多需要删除的数据 # for i in self.bulletList: # if i.y<0: # self.bulletList.remove(i) #用来存放需要删除的对象引用 needDelItemList = [] #保存需要删除的对象 for i in self.bulletList: if i.judge(): needDelItemList.append(i) #删除self.bulletList中需要删除的对象 for i in needDelItemList: self.bulletList.remove(i) #因为needDelItemList也保存了刚刚删除的对象的引用,所以可以删除整个列表,那么 #整个列表中的引用就不存在了,也可以不调用下面的代码,因为needDelItemList是局部变量 #当这个方法的调用结束时,这个局部变量也就不存在了 # del needDelItemList for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen) self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen): self.x = x+40 self.y = y-20 self.screen = screen self.image = pygame.image.load("../feiji/bullet.png") def move(self): self.y -= 5 def display(self): self.screen.blit(self.image,(self.x,self.y)) def judge(self): if self.y<0: return True else: return False class EnemyPlane(object): def __init__(self,screen): #设置飞机默认的位置 self.x = 0 self.y = 0 #设置要显示内容的窗口 self.screen = screen self.imageName = "../feiji/enemy0.png" self.image = pygame.image.load(self.imageName) self.direction = "right" #用来存储敌人飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) #判断一下子弹的位置是否越界,如果是,那么就要删除这颗子弹 # #这种方法会漏掉很多需要删除的数据 # for i in self.bulletList: # if i.y<0: # self.bulletList.remove(i) #存放需要删除的对象信息 needDelItemList = [] for i in self.bulletList: if i.judge(): needDelItemList.append(i) for i in needDelItemList: self.bulletList.remove(i) # del needDelItemList #更新及这架飞机发射出的所有子弹的位置 for bullet in self.bulletList: bullet.display() bullet.move() def move(self): #如果碰到了右边的边界,那么就往左走,如果碰到了左边的边界,那么就往右走 if self.direction == "right": self.x += 4 elif self.direction == "left": self.x -= 4 if self.x>480-50: self.direction = "left" elif self.x<0: self.direction = "right" def sheBullet(self): num = random.randint(1,100) if num == 88: newBullet = EnemyBullet(self.x,self.y,self.screen) self.bulletList.append(newBullet) class EnemyBullet(object): def __init__(self,x,y,screen): self.x = x+30 self.y = y+30 self.screen = screen self.image = pygame.image.load("../feiji/bullet1.png") def move(self): self.y += 4 def display(self): self.screen.blit(self.image,(self.x,self.y)) def judge(self): if self.y>852: return True else: return False def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3.1 创建一个飞机对象 heroPlane = HeroPlane(screen) #3.2 创建一个敌人飞机 enemyPlane = EnemyPlane(screen) #4. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() enemyPlane.move() enemyPlane.display() enemyPlane.sheBullet() key_control(heroPlane) pygame.display.update() #通过延时的方式,来降低这个while循环的循环速度,从而降低了cpu占用率 time.sleep(0.01) if __name__ == "__main__": main()
结果:
十、代码优化-抽象出基类
- 代码:
#coding=utf-8 import pygame from pygame.locals import * import time import random ''' 代码优化:抽出基类 ''' class Plane(object): def __init__(self, screen, imageName): #设置要显示内容的窗口 self.screen = screen self.image = pygame.image.load(imageName) #用来存储英雄飞机发射的所有子弹 self.bulletList = [] def display(self): self.screen.blit(self.image,(self.x,self.y)) #用来存放需要删除的对象引用 needDelItemList = [] #保存需要删除的对象 for i in self.bulletList: if i.judge(): needDelItemList.append(i) #删除self.bulletList中需要删除的对象 for i in needDelItemList: self.bulletList.remove(i) for bullet in self.bulletList: bullet.display()#显示一个子弹的位置 bullet.move()#让这个子弹进行移动,下次再显示的时候就会看到子弹在修改后的位置 class HeroPlane(Plane): def __init__(self, screen): super(HeroPlane, self).__init__(screen, "../feiji/hero1.png") #设置飞机默认的位置 self.x = 230 self.y = 580 def moveLeft(self): self.x -= 10 def moveRight(self): self.x += 10 def sheBullet(self): newBullet = Bullet(self.x, self.y, self.screen, "hero") self.bulletList.append(newBullet) class EnemyPlane(Plane): def __init__(self, screen): super(EnemyPlane, self).__init__(screen, "../feiji/enemy0.png") #设置飞机默认的位置 self.x = 0 self.y = 0 self.direction = "right" def move(self): #如果碰到了右边的边界,那么就往左走,如果碰到了左边的边界,那么就往右走 if self.direction == "right": self.x += 4 elif self.direction == "left": self.x -= 4 if self.x>480-50: self.direction = "left" elif self.x<0: self.direction = "right" def sheBullet(self): num = random.randint(1,100) if num == 88: newBullet = Bullet(self.x,self.y,self.screen, "enemy") self.bulletList.append(newBullet) class Bullet(object): def __init__(self,x,y,screen,planeName): self.name = planeName self.screen = screen if self.name == "hero": self.x = x+40 self.y = y-20 imageName = "../feiji/bullet.png" elif self.name == "enemy": self.x = x+30 self.y = y+30 imageName = "../feiji/bullet1.png" self.image = pygame.image.load(imageName) def move(self): if self.name == "hero": self.y -= 4 elif self.name == "enemy": self.y += 4 def display(self): self.screen.blit(self.image,(self.x,self.y)) def judge(self): if self.y>690 or self.y<0: return True else: return False def key_control(heroPlane): #判断是否是点击了退出按钮 for event in pygame.event.get(): # print(event.type) if event.type == QUIT: print("exit") exit() elif event.type == KEYDOWN: if event.key == K_a or event.key == K_LEFT: print('left') heroPlane.moveLeft() #控制飞机让其向左移动 elif event.key == K_d or event.key == K_RIGHT: print('right') heroPlane.moveRight() elif event.key == K_SPACE: print('space') heroPlane.sheBullet() def main(): #1. 创建一个窗口,用来显示内容 screen = pygame.display.set_mode((480,690),0,32) #2. 创建一个和窗口大小的图片,用来充当背景 background = pygame.image.load("../feiji/background.png") #3.1 创建一个飞机对象 heroPlane = HeroPlane(screen) #3.2 创建一个敌人飞机 enemyPlane = EnemyPlane(screen) #4. 把背景图片放到窗口中显示 while True: screen.blit(background,(0,0)) heroPlane.display() enemyPlane.move() enemyPlane.display() enemyPlane.sheBullet() key_control(heroPlane) pygame.display.update() #通过延时的方式,来降低这个while循环的循环速度,从而降低了cpu占用率 time.sleep(0.01) if __name__ == "__main__": main()
结果: