import random
import sys
import pygame
# 屏幕的宽、高
WIDTH = 630
HEIGHT = 630
# 边界值
BORDER_LEN = 3
# 字体
FONTPATH = 'resources/font/font.ttf'
class Iron(pygame.sprite.Sprite):
"""
铁墙类
"""
# 定义精灵组,将所有的砖墙实例对象添加到里面
group = pygame.sprite.Group()
def __init__(self, position):
# 调用父类的初始化方法,这样才能够实现必要的初始化操作
super().__init__()
self.image = pygame.image.load("resources/images/scene/iron.png")
# 当使用碰撞判断方法时,pygame就需要知道当前要检测的物体的位置,所以这个rect属性一定要设置
self.rect = self.image.get_rect()
self.rect.topleft = position
# 添加到精灵组
self.group.add(self)
@classmethod
def show(cls, screen):
for temp in cls.group:
screen.blit(temp.image, temp.rect)
class Ice(pygame.sprite.Sprite):
"""
冰类
"""
# 定义精灵组,将所有的实例对象添加到里面
group = pygame.sprite.Group()
def __init__(self, position):
# 调用父类的初始化方法,这样才能够实现必要的初始化操作
super().__init__()
# 因为是12x12的小图片,所以需要制作一个24x24的image
image = pygame.Surface((24, 24))
for i in range(2):
for j in range(2):
image.blit(pygame.image.load("resources/images/scene/ice.png"), (12 * i, 12 * j))
self.image = image
# 当使用碰撞判断方法时,pygame就需要知道当前要检测的物体的位置,所以这个rect属性一定要设置
self.rect = self.image.get_rect()
self.rect.topleft = position
# 添加到精灵组
self.group.add(self)
@classmethod
def show(cls, screen):
for temp in cls.group:
screen.blit(temp.image, temp.rect)
class River(pygame.sprite.Sprite):
"""
河流类
"""
# 定义精灵组,将所有的实例对象添加到里面
group = pygame.sprite.Group()
def __init__(self, position):
# 调用父类的初始化方法,这样才能够实现必要的初始化操作
super().__init__()
# 因为是12x12的小图片,所以需要制作一个24x24的image
image = pygame.Surface((24, 24))
for i in range(2):
for j in range(2):
image.blit(pygame.image.load("resources/images/scene/river1.png"), (12 * i, 12 * j))
self.image = image
# 当使用碰撞判断方法时,pygame就需要知道当前要检测的物体的位置,所以这个rect属性一定要设置
self.rect = self.image.get_rect()
self.rect.topleft = position
# 添加到精灵组
self.group.add(self)
@classmethod
def show(cls, screen):
for temp in cls.group:
screen.blit(temp.image, temp.rect)
class Tree(pygame.sprite.Sprite):
"""
树类
"""
# 定义精灵组,将所有的实例对象添加到里面
group = pygame.sprite.Group()
def __init__(self, position):
# 调用父类的初始化方法,这样才能够实现必要的初始化操作
super().__init__()
# 因为是12x12的小图片,所以需要制作一个24x24的image
image = pygame.Surface((24, 24))
for i in range(2):
for j in range(2):
image.blit(pygame.image.load("resources/images/scene/tree.png"), (12 * i, 12 * j))
self.image = image
# 当使用碰撞判断方法时,pygame就需要知道当前要检测的物体的位置,所以这个rect属性一定要设置
self.rect = self.image.get_rect()
self.rect.topleft = position
# 添加到精灵组
self.group.add(self)
@classmethod
def show(cls, screen):
for temp in cls.group:
screen.blit(temp.image, temp.rect)
class Brick(pygame.sprite.Sprite):
"""
砖墙类
"""
# 定义精灵组,将所有的砖墙实例对象添加到里面
group = pygame.sprite.Group()
def __init__(self, position):
# 调用父类的初始化方法,这样才能够实现必要的初始化操作
super().__init__()
self.image = pygame.image.load("resources/images/scene/brick.png")
# 当使用碰撞判断方法时,pygame就需要知道当前要检测的物体的位置,所以这个rect属性一定要设置
self.rect = self.image.get_rect()
self.rect.topleft = position
# 添加到精灵组
self.group.add(self)
@classmethod
def show(cls, screen):
for temp in cls.group:
screen.blit(temp.image, temp.rect)
class Bullet(pygame.sprite.Sprite):
"""
子弹类
"""
# 定义精灵组,将所有的砖墙实例对象添加到里面
group = pygame.sprite.Group()
group_enemy = pygame.sprite.Group()
def __init__(self, _type, direction, position):
super().__init__()
# 子弹图片
if direction == "up":
image_path = "resources/images/bullet/bullet_up.png"
elif direction == "down":
image_path = "resources/images/bullet/bullet_down.png"
elif direction == "left":
image_path = "resources/images/bullet/bullet_left.png"
elif direction == "right":
image_path = "resources/images/bullet/bullet_right.png"
self.image = pygame.image.load(image_path)
# 子弹位置
self.rect = self.image.get_rect()
self.rect.center = position # 设置子弹的初始位置的中心点
# 子弹方向
self.direction = direction
# 子弹移动速度
self.speed = 8
# 将子弹添加到精灵组
if _type == "player":
self.group.add(self)
else:
self.group_enemy.add(self)
@classmethod
def auto_move(cls):
for temp in cls.group:
if temp.rect.x < BORDER_LEN or temp.rect.x > WIDTH - BORDER_LEN or temp.rect.y < BORDER_LEN or temp.rect.y > HEIGHT - BORDER_LEN:
cls.group.remove(temp)
print("子弹超出边界,移除子弹")
continue
if temp.direction == "up":
temp.rect = temp.rect.move((0, -temp.speed))
elif temp.direction == "down":
temp.rect = temp.rect.move((0, temp.speed))
elif temp.direction == "left":
temp.rect = temp.rect.move((-temp.speed, 0))
elif temp.direction == "right":
temp.rect = temp.rect.move((temp.speed, 0))
for temp in cls.group_enemy:
if temp.rect.x < BORDER_LEN or temp.rect.x > WIDTH - BORDER_LEN or temp.rect.y < BORDER_LEN or temp.rect.y > HEIGHT - BORDER_LEN:
cls.group_enemy.remove(temp)
print("子弹超出边界,移除子弹")
continue
if temp.direction == "up":
temp.rect = temp.rect.move((0, -temp.speed))
elif temp.direction == "down":
temp.rect = temp.rect.move((0, temp.speed))
elif temp.direction == "left":
temp.rect = temp.rect.move((-temp.speed, 0))
elif temp.direction == "right":
temp.rect = temp.rect.move((temp.speed, 0))
# 子弹碰砖墙(如果相碰,那么就移除当前子弹以及砖墙)
pygame.sprite.groupcollide(cls.group, Brick.group, True, True)
pygame.sprite.groupcollide(cls.group_enemy, Brick.group, True, True)
# 子弹碰铁墙(如果相碰,那么只移除子弹)
for bullet in cls.group:
if pygame.sprite.spritecollide(bullet, Iron.group, False, None):
cls.group.remove(bullet)
for bullet in cls.group_enemy:
if pygame.sprite.spritecollide(bullet, Iron.group, False, N
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
部分代码: import random import sys import pygame # 屏幕的宽、高 WIDTH = 630 HEIGHT = 630 # 边界值 BORDER_LEN = 3 # 字体 FONTPATH = 'resources/font/font.ttf' class Iron(pygame.sprite.Sprite): """ 铁墙类 """ # 定义精灵组,将所有的砖墙实例对象添加到里面 group = pygame.sprite.Group()
资源详情
资源评论
资源推荐
收起资源包目录
[PY]坦克大战源码.zip (64个子文件)
坦克大战
main.py 41KB
resources
audios
start.wav 863KB
add.wav 128KB
Gunfire.wav 2KB
hit.wav 790B
bang.wav 9KB
blast.wav 53KB
fire.wav 68KB
images
bullet
bullet_down.png 15KB
bullet_up.png 15KB
bullet_left.png 15KB
bullet_right.png 15KB
playerTank
tank_T1_2.png 16KB
tank_T1_0.png 16KB
tank_T1_1.png 16KB
tank_T2_2.png 16KB
tank_T2_0.png 16KB
tank_T2_1.png 16KB
others
gameover.png 1KB
boom_static.png 22KB
appear.png 15KB
gamebar.png 1KB
background.png 1.15MB
tip.png 3KB
mask.png 646B
protect.png 15KB
boom_dynamic.png 88KB
logo.png 130KB
scene
river2.png 3KB
ice.png 2KB
river1.png 3KB
iron.png 15KB
brick.png 17KB
tree.png 1012B
home
home2.png 22KB
home_destroyed.png 17KB
home1.png 18KB
enemyTank
enemy_2_3.png 16KB
enemy_2_2.png 16KB
enemy_4_2.png 16KB
enemy_3_3.png 16KB
enemy_2_0.png 16KB
enemy_3_0.png 16KB
enemy_1_2.png 16KB
enemy_3_1.png 16KB
enemy_4_1.png 16KB
enemy_1_3.png 16KB
enemy_4_0.png 16KB
enemy_2_1.png 16KB
enemy_1_0.png 16KB
enemy_4_3.png 16KB
enemy_1_1.png 16KB
enemy_3_2.png 16KB
food
food_gun.png 16KB
food_tank.png 16KB
food_star.png 16KB
food_iron.png 16KB
food_boom.png 16KB
food_protect.png 16KB
food_clock.png 16KB
font
font.TTF 1.72MB
levels
2.lvl 2KB
1.lvl 2KB
3.lvl 2KB
共 64 条
- 1
CODEZPC
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0