写个坦克大战的python代码
时间: 2023-10-04 13:14:45 浏览: 93
好的,请注意以下的代码仅作为参考,可能存在错误和不足之处,需要根据实际情况进行修改和完善。
```python
import pygame
from pygame.locals import *
pygame.init()
WIDTH, HEIGHT = 640, 480
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Tank(pygame.sprite.Sprite):
def __init__(self, x, y, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 20))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
def move(self, dx, dy):
self.rect.x += dx * self.speed
self.rect.y += dy * self.speed
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((5, 5))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.direction = direction
self.speed = 10
def update(self):
if self.direction == 'up':
self.rect.y -= self.speed
elif self.direction == 'down':
self.rect.y += self.speed
elif self.direction == 'left':
self.rect.x -= self.speed
elif self.direction == 'right':
self.rect.x += self.speed
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 20))
self.image.fill(GRAY)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Game():
def __init__(self):
self.all_sprites = pygame.sprite.Group()
self.tanks = pygame.sprite.Group()
self.bullets = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
self.clock = pygame.time.Clock()
self.player1 = Tank(100, 100, GREEN)
self.player2 = Tank(500, 400, RED)
self.all_sprites.add(self.player1)
self.all_sprites.add(self.player2)
self.tanks.add(self.player1)
self.tanks.add(self.player2)
for i in range(20):
wall = Wall(i * 20, 0)
self.all_sprites.add(wall)
self.walls.add(wall)
wall = Wall(i * 20, 460)
self.all_sprites.add(wall)
self.walls.add(wall)
for i in range(1, 23):
wall = Wall(0, i * 20)
self.all_sprites.add(wall)
self.walls.add(wall)
wall = Wall(620, i * 20)
self.all_sprites.add(wall)
self.walls.add(wall)
def run(self):
running = True
while running:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_w:
self.player1.move(0, -1)
elif event.key == K_s:
self.player1.move(0, 1)
elif event.key == K_a:
self.player1.move(-1, 0)
elif event.key == K_d:
self.player1.move(1, 0)
elif event.key == K_SPACE:
bullet = Bullet(self.player1.rect.x + 10, self.player1.rect.y + 10, 'up')
self.all_sprites.add(bullet)
self.bullets.add(bullet)
elif event.key == K_UP:
self.player2.move(0, -1)
elif event.key == K_DOWN:
self.player2.move(0, 1)
elif event.key == K_LEFT:
self.player2.move(-1, 0)
elif event.key == K_RIGHT:
self.player2.move(1, 0)
elif event.key == K_RETURN:
bullet = Bullet(self.player2.rect.x + 10, self.player2.rect.y + 10, 'down')
self.all_sprites.add(bullet)
self.bullets.add(bullet)
for bullet in self.bullets:
bullet.update()
if bullet.rect.x < 0 or bullet.rect.x > WIDTH or bullet.rect.y < 0 or bullet.rect.y > HEIGHT:
bullet.kill()
if pygame.sprite.spritecollide(bullet, self.walls, True):
bullet.kill()
if pygame.sprite.spritecollide(bullet, self.tanks, False):
bullet.kill()
self.all_sprites.update()
SCREEN.fill(BLACK)
self.all_sprites.draw(SCREEN)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
game = Game()
game.run()
```
这个代码实现了一个简单的坦克大战游戏,包括了两个玩家坦克、子弹、墙壁等元素,可以通过键盘控制玩家坦克的移动和发射子弹。具体实现细节可以根据需要进行修改和完善。
阅读全文