[Python] 纯文本查看 复制代码
import pygame
import sys
import random
import math
# 初始化pygame
pygame.init()
# 设置窗口
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("坦克对战游戏")
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (128, 128, 128)
# 坦克类
class Tank:
def __init__(self, x, y, color, controls):
self.x = x
self.y = y
self.color = color
self.width = 40
self.height = 40
self.speed = 3
self.direction = "up" # 初始方向向上
self.health = 100
self.controls = controls # 控制键位
self.last_shot = 0
self.shoot_delay = 500 # 射击延迟(毫秒)
def draw(self, win):
# 绘制坦克主体
pygame.draw.rect(
win, self.color, (self.x, self.y, self.width, self.height))
# 绘制坦克炮管
if self.direction == "up":
pygame.draw.rect(
win, YELLOW, (self.x + self.width//2 - 2, self.y - 15, 4, 20))
elif self.direction == "down":
pygame.draw.rect(win, YELLOW, (self.x + self.width //
2 - 2, self.y + self.height - 5, 4, 20))
elif self.direction == "left":
pygame.draw.rect(
win, YELLOW, (self.x - 15, self.y + self.height//2 - 2, 20, 4))
elif self.direction == "right":
pygame.draw.rect(win, YELLOW, (self.x + self.width -
5, self.y + self.height//2 - 2, 20, 4))
# 绘制坦克生命值条
health_bar_length = 40
health_bar_height = 5
pygame.draw.rect(win, RED, (self.x, self.y - 10,
health_bar_length, health_bar_height))
health_ratio = self.health / 100
pygame.draw.rect(win, GREEN, (self.x, self.y - 10,
health_bar_length * health_ratio, health_bar_height))
def move(self, keys):
old_x, old_y = self.x, self.y
# 移动控制
if keys[self.controls["up"]]:
self.y -= self.speed
self.direction = "up"
if keys[self.controls["down"]]:
self.y += self.speed
self.direction = "down"
if keys[self.controls["left"]]:
self.x -= self.speed
self.direction = "left"
if keys[self.controls["right"]]:
self.x += self.speed
self.direction = "right"
# 边界检查
if self.x < 0:
self.x = 0
if self.x > WIDTH - self.width:
self.x = WIDTH - self.width
if self.y < 0:
self.y = 0
if self.y > HEIGHT - self.height:
self.y = HEIGHT - self.height
# 碰撞检查
for obstacle in obstacles:
if (self.x < obstacle.x + obstacle.width and
self.x + self.width > obstacle.x and
self.y < obstacle.y + obstacle.height and
self.y + self.height > obstacle.y):
self.x, self.y = old_x, old_y
break
def can_shoot(self, current_time):
return current_time - self.last_shot > self.shoot_delay
def shoot(self, current_time, bullets, bullet_color):
if self.can_shoot(current_time):
self.last_shot = current_time
if self.direction == "up":
bullet = Bullet(self.x + self.width//2 - 2,
self.y - 15, 4, 10, 8, "up", bullet_color)
elif self.direction == "down":
bullet = Bullet(self.x + self.width//2 - 2, self.y +
self.height + 5, 4, 10, 8, "down", bullet_color)
elif self.direction == "left":
bullet = Bullet(self.x - 15, self.y + self.height //
2 - 2, 10, 4, 8, "left", bullet_color)
elif self.direction == "right":
bullet = Bullet(self.x + self.width + 5, self.y +
self.height//2 - 2, 10, 4, 8, "right", bullet_color)
bullets.append(bullet)
return True
return False
# 子弹类
class Bullet:
def __init__(self, x, y, width, height, speed, direction, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = speed
self.direction = direction
self.color = color
def draw(self, win):
pygame.draw.rect(
win, self.color, (self.x, self.y, self.width, self.height))
def move(self):
if self.direction == "up":
self.y -= self.speed
elif self.direction == "down":
self.y += self.speed
elif self.direction == "left":
self.x -= self.speed
elif self.direction == "right":
self.x += self.speed
def off_screen(self):
return (self.x < 0 or self.x > WIDTH or
self.y < 0 or self.y > HEIGHT)
def check_collision(self, tank):
return (self.x < tank.x + tank.width and
self.x + self.width > tank.x and
self.y < tank.y + tank.height and
self.y + self.height > tank.y)
def check_obstacle_collision(self, obstacles):
for obstacle in obstacles:
if (self.x < obstacle.x + obstacle.width and
self.x + self.width > obstacle.x and
self.y < obstacle.y + obstacle.height and
self.y + self.height > obstacle.y):
return True
return False
# 障碍物类
class Obstacle:
def __init__(self, x, y, width, height, color=GRAY):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def draw(self, win):
pygame.draw.rect(
win, self.color, (self.x, self.y, self.width, self.height))