1.实现了坦克的移动(按住不放可以移动)
`
import pygame
import sys
import time
import random
BACK = (250,250,250)
SCREEN_X = 1400
SCREEN_Y = 750
class Tank:
def init(self,screen,team=0):
if team == 0:
self.imgs = {
'w': pygame.image.load('E:/图片/heroW.png'),
's': pygame.image.load('E:/图片/heroS.png'),
'a': pygame.image.load('E:/图片/heroA.png'),
'd': pygame.image.load('E:/图片/heroD.png')
}
else:
self.imgs = {
'w': pygame.image.load('E:/图片/eW.png'),
's': pygame.image.load('E:/图片/eS.png'),
'a': pygame.image.load('E:/图片/eA.png'),
'd': pygame.image.load('E:/图片/eD.png')
}
self.team = team
self.direction = 'w'
self.img = self.imgs[self.direction]
# tank初始化位置
self.rect = self.img.get_rect()
if team == 0:
self.rect.topleft = [300, 600]
else:
self.rect.topleft = [300, 500]
self.speed = 5
self.screen = screen
def control(self):
# 按键控制
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
self.direction = 'w'
self.rect.top -= self.speed
if key_pressed[pygame.K_s]:
self.direction = 's'
self.rect.top += self.speed
if key_pressed[pygame.K_a]:
self.direction = 'a'
self.rect.left -= self.speed
if key_pressed[pygame.K_d]:
self.direction = 'd'
self.rect.left += self.speed
if key_pressed[pygame.K_j]:
pass
def ai(self):
a = random.randint(1,4)
b = random.randint(1,5)
if a == 1:
self.direction = 'w'
self.rect.top -= b
elif a == 2:
self.direction = 's'
self.rect.top -= b
elif a == 3:
self.direction = 'a'
self.rect.top -= b
else:
self.direction = 'd'
self.rect.top -= b
def display(self):
self.img = self.imgs[self.direction]
self.screen.blit(self.img, self.rect)
class Manager:
def init(self):
self.screen = pygame.display.set_mode((SCREEN_X,SCREEN_Y))
def main(self):
pygame.init()
t1 = Tank(self.screen)
e1 = Tank(self.screen,1)
while True:
self.screen.fill(BACK)
t1.control()
# e1.ai()
# e1.display()
t1.display()
time.sleep(0.1)#防止坦克跑得太快
pygame.display.update()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
m = Manager()
m.main()
`
下次最好设置:不能同时两个方向键
2.增加了可以自主移动的敌方坦克(不过这个小Ai写的不太好)
team = 0 表示自己控制的所有坦克
team = 1,2,。。。表示1,2.。。玩家各自控制的所有坦克
team = -1,-2.。。 表示1,2.。电脑各自控制的所有坦克
`
import pygame
import sys
import time
import random
BACK = (250,250,250)
SCREEN_X = 1400
SCREEN_Y = 750
class Tank:
def init(self,screen,team=0):
self.screen = screen
if team == 0:
self.imgs = {
'w': pygame.image.load('E:/图片/heroW.png'),
's': pygame.image.load('E:/图片/heroS.png'),
'a': pygame.image.load('E:/图片/heroA.png'),
'd': pygame.image.load('E:/图片/heroD.png')
}
else:
self.imgs = {
'w': pygame.image.load('E:/图片/eW.png'),
's': pygame.image.load('E:/图片/eS.png'),
'a': pygame.image.load('E:/图片/eA.png'),
'd': pygame.image.load('E:/图片/eD.png')
}
self.team = team
self.direction = 'w'
self.img = self.imgs[self.direction]
# tank初始化位置
self.rect = self.img.get_rect()
if team == 0:
self.speed = 5
self.rect.topleft = [300, 600]
else:
self.ran = [3,0]
self.speed = random.randint(1, 5)
self.rect.topleft = [random.random()SCREEN_X, random.random()SCREEN_Y]
def control(self):
# 按键控制
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
self.direction = 'w'
self.rect.top -= self.speed
if key_pressed[pygame.K_s]:
self.direction = 's'
self.rect.top += self.speed
if key_pressed[pygame.K_a]:
self.direction = 'a'
self.rect.left -= self.speed
if key_pressed[pygame.K_d]:
self.direction = 'd'
self.rect.left += self.speed
if key_pressed[pygame.K_j]:
pass
def ai(self):
self.ran[1] += 1
if self.ran[0] % 3 == 0:
self.direction = 'w'
self.rect.top -= self.speed
if self.ran[1] % 9 == 0:
self.ran[0] += 1
elif self.ran[0] % 3 == 1:
self.direction = 's'
self.rect.top += self.speed
if self.ran[1] % 16 == 0:
self.ran[0] += 1
elif self.ran[0] % 3 == 2:
self.direction = 'a'
self.rect.left -= self.speed
if self.ran[1] % 31 == 0:
self.ran[0] += 2
else:
self.direction = 'd'
self.rect.left += self.speed
if self.ran[1] % 65 == 0:
self.ran[0] += 1
def display(self):
self.img = self.imgs[self.direction]
self.screen.blit(self.img, self.rect)
class Manager:
def init(self):
self.screen = pygame.display.set_mode((SCREEN_X,SCREEN_Y))
def main(self):
pygame.init()
t1 = Tank(self.screen)
enemy = [Tank(self.screen, 1) for _ in range(7)]
while True:
self.screen.fill(BACK)
t1.control()
t1.display()
for i in range(6):
enemy[i].ai()
enemy[i].display()
time.sleep(0.1)#防止坦克跑得太快
pygame.display.update()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
m = Manager()
m.main()
`