-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtanktrouble1.py
More file actions
65 lines (62 loc) · 1.83 KB
/
tanktrouble1.py
File metadata and controls
65 lines (62 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#import libraries
import pygame
#initilize pygame
pygame.init()
#define sprite images
right = pygame.image.load('right.png')
left = pygame.image.load('left.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
direction = "right"
#set game window size and title
screen_width = 1500
screen_height = 800
win = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("TT1")
#tank class object seup
class tank(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
#redraw function for orienting sprites and redrawing
def redrawGameWindow():
global direction
win.fill((0, 0, 0,))
if direction == "left":
win.blit(left, (tank.x, tank.y))
elif direction == "right":
win.blit(right, (tank.x, tank.y))
elif direction == "up":
win.blit(up, (tank.x, tank.y))
elif direction == "down":
win.blit(down, (tank.x, tank.y))
pygame.display.update()
#main loop
tank = tank(50, 50, 50, 50)
run = True
while run:
pygame.time.delay(16)
#quit upon x click
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#checking for keypress
keys = pygame.key.get_pressed()
#moving tank and defining direction for orientation.
if keys[pygame.K_LEFT] and tank.x > 0:
tank.x -= tank.vel
direction = "left"
elif keys[pygame.K_RIGHT] and tank.x < screen_width - tank.width:
tank.x += tank.vel
direction = "right"
elif keys[pygame.K_UP] and tank.y > 0:
tank.y -= tank.vel
direction = "up"
elif keys[pygame.K_DOWN] and tank.y < screen_height - tank.height:
tank.y += tank.vel
direction = "down"
redrawGameWindow()
pygame.quit()