-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tank.py~~
64 lines (51 loc) · 2.31 KB
/
Tank.py~~
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
import pygame, sys , math
from pygame.locals import *
class Tank(pygame.sprite.Sprite):
def __init__(self,X,Y):
pygame.sprite.Sprite.__init__(self)
self.health = 100
self.fireRate = 0.5 # fire rate is every half second
self.posX = X
self.posY = Y
self.tankImageLow = pygame.image.load('tanks/p1tank-str0.png')
self.tankImageMid = pygame.image.load('tanks/p1tank-med0.png')
self.tankImageHigh = pygame.image.load('tanks/p1tank-high0.png')
self.tankImage = self.tankImageMid
self.rect = self.tankImage.get_rect()
self.imageWidth =168
self.imageHeight = 71
self.speedX = 100
self.speedY = 100
#returns the tanks x position
def getX(self):
return self.posX
#returns the tanks Y position
def getY(self):
return self.posY
def getImage(self):
return self.tankImage
def IsInBounds(self,maxX,maxY,moveX,moveY):
if self.posX + moveX >= 0 and self.posX + self.imageWidth + moveX <= maxX:
self.posX = self.posX + moveX
if self.posY + moveY >= 0 and self.posY + self.imageHeight + moveY <= maxY:
self.posY = self.posY + moveY
#if self.tankImageh + moveY < screenH or self.tankImageh + moveY > screenH:return 0
def moveByKeyBoard(self,screenW,screenH):
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_d :
self.IsInBounds(screenW,screenH,self.speedX,0)
if event.key == K_a :
self.IsInBounds(screenW,screenH,-self.speedX,0)
if event.key == K_s :
self.IsInBounds(screenW,screenH,0,self.speedY)
if event.key == K_w :
self.IsInBounds(screenW,screenH,0,-self.speedY)
if event.key == K_1:
self.tankImage = self.tankImageLow
if event.key == K_2:
self.tankImage = self.tankImageMid
if event.key == K_3:
self.tankImage = self.tankImageHigh
def drawTank(self,screen):
screen.blit(self.tankImage,(self.posX,self.posY))