Advertisement
snowden_web

Untitled

Aug 14th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. from pygame import *
  2. from random import randint
  3.  
  4. BLACK = (0,0,0)
  5. CELL_WIDTH = 8
  6. CELL_HEIGHT= 8
  7.  
  8. class Tank:
  9.     def __init__(self, direction, x, y, file_image):
  10.         self.direction = direction
  11.         self.health = 100
  12.         self.x = x
  13.         self.y = y
  14.         self.speed = [0, 0]
  15.         self.image = image.load(file_image).convert()
  16.         self.init_image = image.load(file_image).convert()
  17.         self.rect = self.image.get_rect()
  18.  
  19.     def draw(self):
  20.         global screen
  21.         screen.blit(self.image, (self.x, self.y))
  22.        
  23. class Level:
  24.     def __init__(self, file, wall_image):
  25.         self.level = open(file).read().rstrip().split("\n")
  26.         self.image = image.load(wall_image).convert()
  27.         self.walls = []
  28.         x = 0
  29.         y = 0
  30.         for line in self.level:
  31.             for cell in line:
  32.                 if cell == "*":
  33.                     self.walls += [Rect(x, y, CELL_WIDTH, CELL_HEIGHT)]
  34.                 x += CELL_WIDTH
  35.             y += CELL_HEIGHT
  36.             x = 0
  37.     def draw(self):
  38.         x = 0
  39.         y = 0
  40.         for line in self.level:
  41.             for cell in line:
  42.                 if cell == "*":
  43.                     screen.blit(self.image,(x,y))
  44.                 x += CELL_WIDTH
  45.             y += CELL_HEIGHT
  46.             x = 0        
  47.        
  48. class Game:
  49.     def __init__(self):
  50.         global screen
  51.         init()
  52.         display.set_caption("Battle City")
  53.         screen = display.set_mode((480, 416))
  54.         self.clock = time.Clock()
  55.        
  56.         self.level = Level("level01.txt", "images/wall1.png")
  57.         self.player = Tank(0, 100, 100, "images/mytank.png")
  58.        
  59.     def draw(self):
  60.         global screen
  61.        
  62.         screen.fill(BLACK)
  63.         self.player.draw()
  64.         self.level.draw()
  65.         # Bullets
  66.         # Score/Health
  67.        
  68.         display.flip()
  69.        
  70.     def start(self):
  71.         while True:
  72.             time_passed = self.clock.tick(60)
  73.            
  74.             for e in event.get():
  75.                 if e.type == QUIT:
  76.                     quit()
  77.                 elif e.type == KEYDOWN:
  78.                     if e.key == K_LEFT:
  79.                         self.player.speed = [-1, 0]
  80.                         self.player.image = transform.rotate(self.player.init_image, 90)
  81.                        
  82.                     if e.key == K_RIGHT:
  83.                         self.player.speed = [1, 0]
  84.                         self.player.image = transform.rotate(self.player.init_image, -90)
  85.                        
  86.                     if e.key == K_UP:
  87.                         self.player.speed = [0, -1]
  88.                         self.player.image = self.player.init_image
  89.                     if e.key == K_DOWN:
  90.                         self.player.speed = [0, 1]
  91.                         self.player.image = transform.rotate(self.player.init_image, 180)
  92.                 elif e.type == KEYUP:
  93.                     self.player.speed = [0, 0]
  94.             self.player.x += self.player.speed[0]
  95.             self.player.y += self.player.speed[1]
  96.             self.draw()
  97.            
  98.        
  99. screen = None
  100. game = Game()
  101. game.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement