坦克游戏雏形
这是随机创造2到3个敌军坦克上下巡逻 遇到边界就反弹
我方坦克 可以任意移动 和发出子弹 空格键发出
ps:子弹未来得及作碰撞检测
from enum import Enum, unique
from time import sleep
import pygame
from random import randint
black_color = (0, 0, 0)
@unique #这个是判断下面UP RIGHT DOWN LEFT是唯一的常量 如果重复 这个就会报错
class Direction(Enum): #枚举常量 定方向 经验: 符号常量由于字面常量 ,枚举是定义符号常量的最佳选择
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
Q =4
# class BaseTtem(pygame.sprite.Sprite):
# def __init__(self,screem):
# pygame.sprite.Sprite.__init__(self)
# #所有对象共享的属性
# self.screem=screem
class Tank(object): #定义个类坦克
def __init__(self,x,y,speed=5,tdir= Direction.UP): #用枚举表示常量 有限的可能性
self.x = x
self.y =y
self.speed = speed
self.tdir = tdir
def move(self):
if self.tdir == Direction.UP:
self.y -= self.speed
elif self.tdir == Direction.RIGHT:
self.x += self.speed
elif self.tdir == Direction.DOWN:
self.y += self.speed
else:
self.x -= self.speed
def fire(self): #开火装置 子弹初始位置
if self.tdir == Direction.UP: #如果坦克向上
x,y =self.x ,self.y -25 #子弹位置和 炮管位置方向一样 加减数据可以调子弹初始位置
elif self.tdir == Direction.RIGHT:
x,y = self.x +25 ,self.y
elif self.tdir == Direction.DOWN:
x, y = self.x, self.y +25
else:
x, y = self.x -25, self.y
return Bullet(x,y,self.tdir) #开火就返回子弹 就是说 子弹开火的方向和坦克此时方向一样
def draw(self,screen):
#绘制坦克主体
pygame.draw.circle(screen, [255, 0, 0], [self.x, self.y], 15, 0) # 颜色位置 半径
#绘制坦克的额履带
if self.tdir == Direction.UP or self.tdir == Direction.DOWN:
pygame.draw.rect(screen, black_color, (self.x - 25, self.y - 15, 10, 30), 0)
pygame.draw.rect(screen, black_color, (self.x + 15, self.y - 15, 10, 30), 0)
else:
pygame.draw.rect(screen, black_color, (self.x - 15, self.y - 25, 30, 10), 0)
pygame.draw.rect(screen, black_color, (self.x - 15, self.y + 15, 30, 10), 0)
#绘制坦克的炮管
if self.tdir == Direction.UP:
pygame.draw.line(screen, black_color, (self.x, self.y), (self.x, self.y - 25), 5) # 炮管
elif self.tdir == Direction.LEFT:
pygame.draw.line(screen, black_color, (self.x - 25, self.y), (self.x, self.y), 5)
elif self.tdir == Direction.DOWN:
pygame.draw.line(screen, black_color, (self.x, self.y+25), (self.x, self.y ), 5)
elif self.tdir == Direction.RIGHT:
pygame.draw.line(screen, black_color, (self.x, self.y), (self.x + 25, self.y), 5)
pygame.draw.line(screen, black_color, (self.x, self.y), (self.x, self.y), 30)
def crash(self):
return #self.x <= 30 or self.x >= 725 or self.y <= 30 or self.y >= 725
class Wall(Tank): ## 定义墙
def __init__(self,x,y,hight,width,color =(0,0,0)): # pos 元组 一个点
super().__init__(x, y)
self.color =color
self.hight = hight
self.width = width
# 默认颜色为黑色
# 初始化
def draw(self,screen): # 1 画围墙 4 是像素
pygame.draw.rect(screen, self.color,(self.x,self.y,self.width,self.hight), 5)
class Enemy(Tank): #敌军坦克
def __init__(self,x,y,speed=5,tdir= Direction.DOWN):
super().__init__(x,y,speed,tdir)
self.direction = 'down'
def draw(self,screen):
pygame.draw.circle(screen, [255, 200, 0], [self.x, self.y], 15, 0)
pygame.draw.rect(screen, black_color, (self.x - 25, self.y - 15, 10, 30), 0)
pygame.draw.rect(screen, black_color, (self.x + 15, self.y - 15, 10, 30), 0)
pygame.draw.line(screen, black_color, (self.x, self.y+25), (self.x, self.y ), 5)
pygame.draw.line(screen, black_color, (self.x, self.y), (self.x, self.y), 30)
def move(self):
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 500:
self.direction = "left"
elif self.x < 15:
self.direction = "right"
if self.direction == 'up': #碰撞到边缘就返回检测
self.y -= 10
elif self.direction == 'down':
self.y += 10
if self.y> 550:
self.direction = "up"
elif self.y < 50:
self.direction = "down"
if self.direction == 'down':
self.y += 5
class Bullet(object): #子弹
def __init__(self,x,y,mdir,speed = 8): #left子弹初始位置方向
self.x = x
self.y = y
self.mdir = mdir
self.speed = speed
def fly(self):
"""
子弹飞起来
:return: 控制飞行的方向和速度
"""
if self.mdir == Direction.UP:
self.y -= self.speed
elif self.mdir == Direction.RIGHT:
self.x += self.speed
elif self.mdir == Direction.DOWN:
self.y += self.speed
else:
self.x -= self.speed
def draw(self,screen):
# #画子弹
pygame.draw.circle(screen, [255, 200, 0], [self.x , self.y ], 5, 0) #0是实心 1是空心
def main():
#python 搜索一个变量的方式是LEGB方式 Local -->Embedded -->Global-->Built -in 局部 嵌套 全局 内置
def refresh_screen():
pygame.draw.rect(screen, bg_color, (0, 0, 800, 600), 0) # 颜色宽高
tank.draw(screen) # 画坦克
wall.draw(screen)
for tm in missiles: #子弹
tm.draw(screen)
for e_tank in enemy_tanks: #从坦克列表中 遍历 建造坦克1
e_tank.draw(screen)
e_tank.move()
pygame.display.flip()
wall = Wall(10, 10, 585, 790)
tank = Tank(300,300) #定义坦克对象 传入位置
enemy_tanks = [] #创建一个坦克的列表
missiles =[] #创建空的 子弹列表
for i in range(1,6): #循环建造坦克2 这里有点BUG 只会生成2到三个坦克
m = randint(1,3)
etank = Enemy(50+100*m,200,5) #构造敌军坦克的坐标
enemy_tanks.append(etank) #将每个坦克的坐标放到列表
pygame.init()
screen = pygame.display.set_mode((800, 600)) # 画布
pygame.display.set_caption('坦克大战') # 定义名字
bg_color = (240, 240, 240) # 参数组成: 红255 绿255 蓝255
game_stop = False
running = True
while running:
# tank.move() #让坦克动起来自己跑
for tm in missiles:
tm.fly()
refresh_screen() #刷新
sleep(0.05)
if not game_stop:
tank.move()
# if tank.crash():
if tank.x <=35 or tank.x >= 770 or tank.y <=40 or tank.y >=575:
game_stop = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w: #键盘按键设置 此处可以print(event.key)查看按键wasd的数字码
tank.tdir = Direction.UP
game_stop =False
elif event.key == pygame.K_a:
tank.tdir = Direction.LEFT
game_stop = False
elif event.key == pygame.K_s:
tank.tdir = Direction.DOWN
game_stop = False
elif event.key == pygame.K_d:
tank.tdir = Direction.RIGHT
game_stop = False
elif event.key == pygame.K_SPACE : #子弹 空格键 运行
m = tank.fire()
missiles.append(m) #开火时,把子弹增加放到子弹列表
for tm in missiles:
if tm.x <=25 or tm.x >= 775 or tm.y <=25 or tm.y >=580:
missiles.remove(tm)
pygame.quit()
if __name__ == '__main__':
main()
–飞机大战—
(参考了网上的飞机大战)
开始随机位置生成敌机位置,我军飞机可以任意按wasd 上下左右键控制 空格键打子弹
敌机 会自动移动 并随机发射子弹
ps: 后面有时间应该还会完善
#碰撞
from random import randint
import pygame
import time
##敌方 子弹随机发射 敌军飞机向下移动
class HeroPlane(object):
"""初始化桌面"""
def __init__(self,screen):
self.x = 210
self.y = 600
self.screen = screen
self.image = pygame.image.load("./feiji/hero1.png")
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()
def move_left(self):
self.x -= 15
def move_right(self):
self.x += 15
def fire(self): # 我方飞机开火
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
class Bullet(object): #子弹
def __init__(self,screen,x,y):
self.x = x +40
self.y = y -20
self.screen = screen
self.image = pygame.image.load("./feiji/bullet.png") #子弹
self.image_enemy = pygame.image.load("./feiji/bullet-1.gif") #敌军子弹
def display(self):
self.screen.blit(self.image,(self.x,self.y)) #把子弹图片放到界面
def display_enemy(self): #敌军子弹放到界面
self.screen.blit(self.image_enemy, (self.x, self.y))
def move(self): #子弹的移动
self.y -= 15
def move_enemy(self): #敌军子弹发射方向 #敌军子弹反向移动
self.y += 15
class EnemyPlane(object):
"""敌军飞机"""
def __init__(self,screen,x,y):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load("./feiji/enemy0.png")
self.bullet_list2 = [] #储存发射出去的的子弹对象引用
self.direction = 'right' # 存储飞机默认方向
self.direction = 'down'
def display(self): #生成飞机
self.screen.blit(self.image,(self.x+18,self.y-50))
for bullet in self.bullet_list2: #生成敌军子弹
bullet.display_enemy() #设置敌军子弹样式
bullet.move_enemy()
def move(self): #左右移动 敌机的不断移动 左右碰撞会反弹 向下消失
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 400:
self.direction = "left"
elif self.x < 0:
self.direction = "right"
if self.direction == 'down':
self.y += 1
def fire(self):
random_num = randint(1,50) #控制子弹发射频率
if random_num == 1 or random_num == 10:
self.bullet_list2.append(Bullet(self.screen,self.x,self.y))
def main():
screen = pygame.display.set_mode((480,752),0,32)
hero = HeroPlane(screen)
backgrond = pygame.image.load('./feiji/background.png')
i = randint(1,6) #随机生成敌军飞机位置 x大于400是横着飞 否则竖着飞
enemy = EnemyPlane(screen,100*i,100)
pygame.init()
while True:
screen.blit(backgrond,(0,0))
hero.display()
enemy.display()
enemy.move()
enemy.fire()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
print('left')
hero.move_left()
elif event.key == pygame.K_w or event.key == pygame.K_UP:
hero.y -= 5
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
print('right')
hero.move_right()
elif event.key == pygame.K_w or event.key == pygame.K_UP:
hero.y -= 5
elif event.key == pygame.K_SPACE:
print('space') #按空格键开火
hero.fire()
time.sleep(0.01)
if __name__ == '__main__':
main()
这个版本在上面基础上加了个自爆效果 按b 敌机和自己都会爆炸 并退出游戏
# 按b键自爆 两个飞机都自爆
from random import randint
import pygame
import time
#敌方 子弹随机发射 敌军飞机向下移动
class HeroPlane(object):
"""初始化桌面"""
def __init__(self,screen):
self.x = 210
self.y = 600
self.screen = screen
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = [] #储存发射出去的的子弹对象引用
self.hit = False#判断是否爆炸
self.bomb_list = []
self.__crate_images() #调用这个方法向bomb_list添加图片
self.image_num = 0 # 记录while True的次数
self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号
def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))
def display(self):
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index],(self.x,self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index+=1
if self.image_index > 3:
time.sleep(3)
exit() # 退出游戏
else:
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 -= 15
def move_right(self):
self.x += 15
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y))
def bomb(self):
self.hit = True
class Bullet(object):
def __init__(self,screen,x,y):
self.x = x +40
self.y = y -20
self.screen = screen
self.image = pygame.image.load("./feiji/bullet.png")
self.image_enemy = pygame.image.load("./feiji/bullet-1.gif")
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def display_enemy(self):
self.screen.blit(self.image_enemy, (self.x, self.y))
def move(self):
self.y -= 15
def move_enemy(self): #敌军子弹发射方向
self.y += 15
class EnemyPlane(object):
def __init__(self,screen,x,y):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load("./feiji/enemy0.png")
self.bullet_list2 = [] #储存发射出去的的子弹对象引用
self.direction = 'right' # 存储飞机默认方向
self.direction = 'down'
self.hit = False#判断是否爆炸
self.bomb_list = []
self.__crate_images() #调用这个方法向bomb_list添加图片
self.image_num = 0 # 记录while True的次数
self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号
def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))
def display(self):
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
time.sleep(3)
exit() # 退出游戏
else:
self.screen.blit(self.image, (self.x, self.y))
def move(self): #左右移动
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 400:
self.direction = "left"
elif self.x < 0:
self.direction = "right"
if self.direction == 'down':
self.y += 1
def fire(self):
random_num = randint(1,100) #控制子弹发射频率
if random_num == 1 or random_num == 10:
self.bullet_list2.append(Bullet(self.screen,self.x,self.y))
def bomb(self):
self.hit = True
def main():
screen = pygame.display.set_mode((480,752),0,32)
hero = HeroPlane(screen)
backgrond = pygame.image.load('./feiji/background.png')
i = randint(1,6)
enemy = EnemyPlane(screen,80*i,100)
print(i)
pygame.init()
while True:
screen.blit(backgrond,(0,0))
hero.display()
enemy.display()
enemy.move()
enemy.fire()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
print('left')
hero.move_left()
elif event.key == pygame.K_w or event.key == pygame.K_UP:
hero.y -= 5
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
print('right')
hero.move_right()
elif event.key == pygame.K_w or event.key == pygame.K_UP:
hero.y -= 5
elif event.key == pygame.K_SPACE:
print('space')
hero.fire()
elif event.key == pygame.K_b:
print('b')
hero.bomb()
enemy.bomb()
time.sleep(0.01)
if __name__ == '__main__':
main()