This is the 31st day of my participation in the August More Text Challenge
⚽ takeaway
Recently, after reading a game article by a big shot, I felt itchy and wanted to write a little game myself. Frustrated by the lack of game material and the fact that I had to pay to search online, I wrote a little to satisfy my cravings. * Good nonsense not to say we now try the game effect. Don’t say it if it’s not funny, hee hee hee. We are slowly rolling out the fun behind.
Video presentation address
⚽ code and explanation
⚽ Main part
For those who do not understand multithreading in Python, check out this articleIf you need a private message, I’ll let you in
.
⚽ source code plus detailed explanation
.
⚽ initialize tank Settings
pygame.init()# initialization
size=(width,heiht)=(800.600)
screen=pygame.display.set_mode(size)# Setup window
pygame.display.set_caption("Tank Wars.")Set the window name
fps=30# Refresh frequency
fpsclock=pygame.time.Clock()# define clock
color_back=(0.0.0)# Set the background color
my_tank_speed=[4.4]# Tank horizontal, vertical speed
tank_img=pygame.image.load("tanke.bmp")# load image
bullet_img=pygame.image.load("zidan.bmp")# load image
new_tank_img=tank_img#new is the tank used, it rotates behind it
tank_rect=tank_img.get_rect()# Rectangle around the image
tank_rect.topleft=[400.300]# Initial position of tank
left_count=right_count=top_count=bottom_count=0# Bullet fire button counts
my_rotate=0# Save current direction
my_bullet=0# Bullet count
my_value=0;# score
# write words
font=pygame.font.Font(None.32)
text=font.render("value:"+str(my_value),True, (255.0.0))
text_rect=text.get_rect()
text_rect.left=10# text position
text_rect.top=10
Copy the code
.
⚽ Define the tank class
class Tank(pygame.sprite.Sprite) :# Enemy Tank Elves
def __init__(self,filename,initial_position) :
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed_top=1# Enemy tank speed
self.speed_left = 1
def update(self, *args) :
self.rect.top=self.rect.top+self.speed_top
self.rect.left = self.rect.left + self.speed_left
if (self.rect.left < 0) or (self.rect.right > width):
self.speed_left=-self.speed_left
if (self.rect.top < 0) or (self.rect.bottom > heiht):
self.speed_top = -self.speed_top
Copy the code
.
⚽ Defines the bullet class
class bullet(pygame.sprite.Sprite) :# Bullet Elf
def __init__(self,filename,my_tank_rect,my_rotate) :
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.speed_top=5# Bullet speed
self.speed_left = 5
self.my_rotate=my_rotate
The initial firing position of the bullet
self.rect.left = my_tank_rect.left + (my_tank_rect.right - my_tank_rect.left) / 2
self.rect.right = self.rect.left + 12.5
self.rect.top = my_tank_rect.top + (my_tank_rect.bottom - my_tank_rect.top) / 2
self.rect.bottom = self.rect.top + 12.5
self.speed=[0.0]
def update(self, *args) :
pass
# Automatic bullet movement control
speed_size=self.speed_top
# Calculate the direction of the bullet's velocity
if self.my_rotate == 0:
self.speed = [0, -speed_size]
elif self.my_rotate == 180 :
self.speed = [0, speed_size]
elif self.my_rotate == 90 :
self.speed = [-speed_size, 0]
elif self.my_rotate == -90 :
self.speed = [speed_size, 0]
# Bullet Move
self.rect.top = self.rect.top + self.speed[1]
self.rect.left = self.rect.left + self.speed[0] # Rectangle box move
# Bullet boundary condition judgment
if (self.rect.left < 0) or (self.rect.right > width):
my_bullet = 0
self.kill()
if (self.rect.top < 0) or (self.rect.bottom > heiht):
my_bullet = 0
self.kill()
Copy the code
.
⚽ That leaves the main program
while True:
for event in pygame.event.get():Receive events
if event.type==QUIT:# Determine the exit event
pygame.quit()# exit
sys.exit()
# in the for loop, the value is used to determine the action of the key
if event.type == pygame.KEYUP:
# Fire bullets
if event.key == pygame.K_SPACE:
pass
bullet_sprite1 = bullet("zidan.bmp", tank_rect, my_rotate)
bullet_group.add(bullet_sprite1) # Add bullets
my_bullet = my_bullet + 1
# Keyboard movement control, placed outside the for loop, can continuously detect the state of the key pressed
tank_speed=my_tank_speed[0]
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
tank_rect = tank_rect.move(-tank_speed,0)
left_count=left_count+1Record the number of left keystrokes
if left_count==1:# If it is pressed for the first time, rotate the shape
my_rotate=90
new_tank_img=pygame.transform.rotate(tank_img,90)# Rotation figure
right_count=top_count=bottom_count=0Clear the rest
elif event.key==pygame.K_RIGHT:
tank_rect = tank_rect.move(tank_speed, 0)
right_count = right_count + 1
if right_count == 1:
my_rotate=-90
new_tank_img = pygame.transform.rotate(tank_img, -90)
left_count = top_count = bottom_count = 0
elif event.key == pygame.K_UP:
tank_rect = tank_rect.move(0, -tank_speed)
top_count = top_count + 1
if top_count == 1:
my_rotate=0
new_tank_img = pygame.transform.rotate(tank_img, 0)
left_count = right_count = bottom_count = 0
elif event.key == pygame.K_DOWN:
tank_rect = tank_rect.move(0, tank_speed)
bottom_count = bottom_count + 1
if bottom_count == 1:
my_rotate=180
new_tank_img = pygame.transform.rotate(tank_img, 180)
left_count = right_count = top_count = 0
# Control boundary conditions
if tank_rect.left<0:
tank_rect =tank_rect.move(1.0)
elif tank_rect.right>width:
tank_rect =tank_rect.move(-1.0)
elif tank_rect.top<0:
tank_rect =tank_rect.move(0.1)
elif tank_rect.bottom>heiht:
tank_rect =tank_rect.move(0, -1)
# Bullet collision monitoring
if pygame.sprite.groupcollide(bullet_group,tank_group,True.True) :# score values
my_value=my_value+1
text = font.render("value:" + str(my_value), True, (255.0.0))
print("value:",my_value)
# Bullet count
my_bullet = len(bullet_group)
print("bullet:",my_bullet)
# Add tanks
if len(tank_group)==0:
retank1 = Tank('tanke2.bmp', [np.random.random_integers(80.600), np.random.random_integers(80.600)])
retank2 = Tank('tanke3.bmp', [np.random.random_integers(80.600), np.random.random_integers(80.600)])
retank3 = Tank('tanke4.bmp', [np.random.random_integers(80.600), np.random.random_integers(80.600)])
retank4 = Tank('tanke5.bmp', [np.random.random_integers(80.600), np.random.random_integers(80.600)])
tank_group.add(retank1)
tank_group.add(retank2)
tank_group.add(retank3)
tank_group.add(retank4)
# Screen refresh control
screen.fill(color_back)# Window background is black, graphics behind this, otherwise it will be overwritten
screen.blit(new_tank_img,tank_rect)Fill the rectangle
screen.blit(text,text_rect)# add text
# Update Sprite group
tank_group.update()Update enemy tank Sprites
tank_group.draw(screen)# drawing
bullet_group.update()Update the Bullet Sprite group
bullet_group.draw(screen)
pygame.display.update()# refresh window
fpsclock.tick(fps)Set the refresh rate per second
Copy the code
.
⚽ play MP3
Here we use the playSound module which is very convenient to use
def tanke_mp3() :
playsound('tanke.mp3')
Copy the code
Well, that’s all for today’s article.