pygame
time sleep
random BG_COLOR = pygame.Color(, , )
SCREEN_WIDTH = SCREEN_HEIGHT = TEXT_COLOR = pygame.Color(, , ) Tank:
(, left: , top: ) -> :
.images = {
: pygame.image.load(),
: pygame.image.load(),
: pygame.image.load(),
: pygame.image.load(),
}
.direction = .image = .images.get(.direction)
.rect = .image.get_rect()
.rect.left = left
.rect.top = top
.speed = .remove = () -> :
.image = .images.get(.direction)
MainGame.window.blit(.image, .rect)
() -> :
.direction == :
.rect.left > :
.rect.left = .rect.left - .speed
.direction == :
.rect.left + .rect.width < SCREEN_WIDTH:
.rect.left = .rect.left + .speed
.direction == :
.rect.top > :
.rect.top = .rect.top - .speed
.direction == :
.rect.top + .rect.height < SCREEN_HEIGHT:
.rect.top = .rect.top + .speed
():
MyTank(Tank):
():
EnemyTank(Tank):
(,left,top,speed): .images = {
:pygame.image.load(),
:pygame.image.load(),
:pygame.image.load(),
:pygame.image.load(),
}
.direction = .rand_direction()
.image = .images.get(.direction)
.rect = .image.get_rect()
.rect.left = left
.rect.top = top
.speed = speed
() -> :
chice = random.randint(, ) chice == :
chice == :
chice == :
chice == :
Bullet:
():
():
():
Wall:
(): (): Explode:
(): (): Music:
(): ():
MainGame:
window = my_tank = enemy_tank_last = []
enemy_tank_count = () -> :
() -> :
pygame.display.init()
MainGame.window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption() MainGame.my_tank = Tank(, )
.create_enemy_tank()
:
sleep()
MainGame.window.fill(BG_COLOR) a = text = .get_text_surface(a) MainGame.window.blit(text, (, )) .get_event()
MainGame.my_tank.display_tank()
.display_enemy_tank()
MainGame.my_tank.remove:
MainGame.my_tank.move()
pygame.display.update() () -> :
.enemy_top = .enemy_speed = i (.enemy_tank_count):
left = random.randint(, )
e_tank = EnemyTank(left, .enemy_top, .enemy_speed)
.enemy_tank_list.append(e_tank)
() -> :
e_tank .enemy_tank_list:
e_tank.display_tank()
(, text: ) -> :
pygame.font.init() font = pygame.font.SysFont(, ) text_surface = font.render(text, , TEXT_COLOR) text_surface
() -> :
event_list = pygame.event.get()
event event_list:
event.type == pygame.QUIT:
.end_game()
event.type == pygame.KEYDOWN:
event.key == pygame.K_LEFT:
()
MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_RIGHT:
()
MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_UP:
()
MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_DOWN:
MainGame.my_tank.direction = ()
MainGame.my_tank.remove = event.type == pygame.KEYUP event.key (pygame.K_LEFT, pygame.K_RIGHT,
pygame.K_UP, pygame.K_DOWN):
MainGame.my_tank.remove = () -> :
()
() __name__ == :
MainGame().start_game()
老师帮我看一下哪里错了