ulvis.paste.net - pastebin

Paste Search Dynamic
Recent pastes
pygame
  1. import pygame
  2. import os
  3.  
  4. pygame.init()
  5.  
  6. screenWidth = 852
  7. screenHeight = 493
  8.  
  9. window = pygame.display.set_mode((screenWidth,screenHeight))
  10. pygame.display.set_caption("Tank Game")
  11.  
  12. char = pygame.image.load('images/tank.png')
  13. bg = pygame.image.load('images/bg.png')
  14.  
  15. clock = pygame.time.Clock()
  16.  
  17.  
  18. class Tank(object):
  19.     def __init__(self, x, y, width, height):
  20.         self.x = x
  21.         self.y = y
  22.         self.width = width
  23.         self.height = height
  24.         self.vel = 5
  25.         self.left = false
  26.         self.right = false
  27.  
  28.     def draw(self, window):
  29.         window.blit(char, (self.x, self.y))
  30.  
  31.  
  32. def redraw_game_window():
  33.     window.blit(bg, (0, 0))  # apply background
  34.     playerTank.draw(window)  # draw playerTank into window
  35.     pygame.display.update()  # update the screen
  36.     for bullet in bullets:
  37.         bullet.draw(window)
  38.  
  39.  
  40. class Bulletz(object):
  41.     def __init__(self, x, y, radius, colour, direction):
  42.         self.x = x
  43.         self.y = y
  44.         self.radius = radius
  45.         self.colour = colour
  46.         self.dir = direction
  47.         self.vel = 8 * direction
  48.  
  49.     def draw(self, window):
  50.         pygame.draw.circle(window, self.colour, (self.x, self.y), self.radius)
  51.  
  52.  
  53. # main loop
  54. playerTank = Tank(0,365,128,128)
  55. run = true
  56. bullets = []  # empty list to track bullets
  57.  
  58. while run:
  59.  
  60.     redraw_game_window()
  61.     clock.tick(60)
  62.  
  63.     for bullet in bullets: #For every bullet in the list
  64.         if bullet.x < 852 and bullet.x > 0: #check if it is on screen
  65.             bullet.x += bullet.vel #add the velocity to it so it moves
  66.         else:
  67.             bullets.pop(bullets.index(bullet))#stop deletes the bullet, index finds which one it is in list
  68.  
  69.     for event in pygame.event.get():
  70.         if event.type == pygame.QUIT:
  71.             run = false
  72.             pygame.quit()
  73.  
  74.     keys = pygame.key.get_pressed() #creates list
  75.  
  76.     if keys[pygame.K_LEFT] and playerTank.x > playerTank.vel: #check if key pressed was left arrow and if the co-ords allow to move / stay on screen
  77.         playerTank.x -= playerTank.vel #move left
  78.         playerTank.left = true #-1 is used to represent left
  79.         playerTank.right = false
  80.     if keys[pygame.K_RIGHT] and playerTank.x < (screenWidth-playerTank.width):
  81.         playerTank.x += playerTank.vel
  82.         playerTank.right = true
  83.         playerTank.left = false#1 is used to represent right
  84.     if keys[pygame.K_SPACE]:#when space is pressed
  85.         if playerTank.left:
  86.             dir = -1
  87.         else:
  88.             dir = 1
  89.  
  90.         if len(bullets) < 10: #check if there are less then 10 bullets on screen
  91.             bullets.append(Bulletz(round(playerTank.x + playerTank.width // 2), round(playerTank.y, playerTank.y // 2), 6, (255, 255, 255), dir)) #//= int division. spawn x/y co-ords, radius, colour, dir
Parsed in 0.042 seconds