Kill The Hive Game By albro

This is the first game I want to make. I named this game "kill the hive". In this game, hives appear on the screen that we have to destroy by clicking the mouse. The level of this game is simple. I'll make more advanced games in the future.
Kill The Hive Game
As you can guess from the name of this game, we are supposed to have hives in the game and we want to kill them. The image of the game is below.

The complete code of the game is also below:
import pygame, sys, random
pygame.init()
SCREEN = pygame.display.set_mode((1280,650))
Clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
# images
BG = pygame.image.load('background.png')
HIVE_SURFACE = pygame.image.load('hive.png')
CROSSHAIR = pygame.image.load('crosshair.png')
# font
GAME_FONT = pygame.font.Font(None,60)
TEXT_SURFACE = GAME_FONT.render('You Won!',True,(0,0,0), (255,255,255))
TEXT_RECT = TEXT_SURFACE.get_rect(center = (640,300))
hive_list = []
x = range(20)
for hive in x:
hive_position_x = random.randrange(50,1200)
hive_position_y = random.randrange(120,600)
hive_rect = HIVE_SURFACE.get_rect(center = (hive_position_x, hive_position_y))
hive_list.append(hive_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEMOTION:
CROSSHAIR_rect = CROSSHAIR.get_rect(center = event.pos)
if event.type == pygame.MOUSEBUTTONDOWN:
for index,hive_rect in enumerate(hive_list):
if hive_rect.collidepoint(event.pos):
del hive_list[index]
SCREEN.blit(BG,(0,0))
for hive_rect in hive_list:
SCREEN.blit(HIVE_SURFACE,hive_rect)
if len(hive_list) <= 0:
SCREEN.blit(TEXT_SURFACE,TEXT_RECT)
SCREEN.blit(CROSSHAIR,CROSSHAIR_rect)
pygame.display.update()
Clock.tick(120)
Import libraries
As usual, to use a library, it must be entered into the program, so we have:
import pygame, sys, random
random is used to generate random numbers. In this game, I use it to generate random places for hives. I like that the hives appear in different places every time I run the game. Then I init pygame. If I don't, pygame won't work.
pygame.init()
Creating a window
SCREEN = pygame.display.set_mode((1280,650))
Clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
I'll use the pygame.time.Clock module to determine the execution speed of the program. The pygame.mouse.set_visible module is used to hide or reveal the mouse pointer. To hide the mouse pointer, I send False value to this module.
Import images into the program
# images
BG = pygame.image.load('background.png')
HIVE_SURFACE = pygame.image.load('hive.png')
CROSSHAIR = pygame.image.load('crosshair.png')
To import images into the program, I use the pygame.image.load module and send the name and location of the image to it. The name of the photo must be a string and be placed in quotation marks. The images should be in the same place as the python file.
Font
# font
GAME_FONT = pygame.font.Font(None,60)
TEXT_SURFACE = GAME_FONT.render('You Won!',True,(0,0,0), (255,255,255))
TEXT_RECT = TEXT_SURFACE.get_rect(center = (640,300))
The player wins when he/she kills all the hives on the screen. After the game ends and the player wins, the appropriate message should be shown to him/her. I use pygame.font.Font to display the text. To display the text, I have to go through the above three steps. These three steps are as follows:
- Step one Create the font object (I do this with
pygame.font.Font). - Step two is to render the text and determine its color. The first parameter of the
rendermethod is the text that I want to display. The second parameter is a boolean parameter. If this parameter isFalse, the characters will have smooth edges. The third parameter of therendermethod is to determine the color. - The third step is to place the text in a rectangle with the
get_rectmethod. Actually, I put a rectangle around the text. Then I determine the place of the text. I put the text at(640,300).
Building hives
hive_list = []
x = range(20)
for hive in x:
hive_position_x = random.randrange(50,1200)
hive_position_y = random.randrange(120,600)
hive_rect = HIVE_SURFACE.get_rect(center = (hive_position_x, hive_position_y))
hive_list.append(hive_rect)
First I create an array for all the hives. I'll have twenty hives in the game. So I put the number twenty in the range function. The game is two-dimensional, so each hive will have two coordinates. The names of these coordinates will be hive_position_x and hive_position_y. I'll use random.randrange to generate various or random locations for the hives. I put the x-coordinate of the hives between 50 and 1200 and the y-coordinate of the hives between 120 and 600:
hive_position_x = random.randrange(50,1200)
hive_position_y = random.randrange(120,600)
In the game, I place a rectangle around the image of the hive. Because it will be easier to recognize the collision between the mouse and the hive because the image of the crosshair and the hive don't have smooth corners. If I don't do this, it won't be easy to detect the collision.
hive_rect = HIVE_SURFACE.get_rect(center = (hive_position_x, hive_position_y))
Finally, I attach the image of the hives, which are twenty in number, to the fly_list array.
hive_list.append(hive_rect)
Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEMOTION:
CROSSHAIR_rect = CROSSHAIR.get_rect(center = event.pos)
if event.type == pygame.MOUSEBUTTONDOWN:
for index,hive_rect in enumerate(hive_list):
if hive_rect.collidepoint(event.pos):
del hive_list[index]
SCREEN.blit(BG,(0,0))
for hive_rect in hive_list:
SCREEN.blit(HIVE_SURFACE,hive_rect)
if len(hive_list) <= 0:
SCREEN.blit(TEXT_SURFACE,TEXT_RECT)
SCREEN.blit(CROSSHAIR,CROSSHAIR_rect)
pygame.display.update()
Clock.tick(120)
The main tasks of the game are done in this loop. Events are managed here. The events I have here are the QUIT event to exit the game, the MOUSEMOTION event to move the mouse, and the MOUSEBUTTONDOWN event to click the mouse. If the QUIT event occurs, I exit the game.
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
If the MOUSEMOTION event occurs, it means the mouse is moving. At every moment of mouse movement I put its position in CROSSHAIR_rect. Mouse position is important. I use this variable to detect the collision between the mouse and the hive.
if event.type == pygame.MOUSEMOTION:
CROSSHAIR_rect = CROSSHAIR.get_rect(center = event.pos)
If the MOUSEBUTTONDOWN event occurs, it means the mouse has been clicked. I need to detect whether the mouse has clicked on the hive or not. If the mouse has clicked on the hive, the clicked hive should be deleted. I have to use the collidepoint method to detect the collision. I need to check if hive_rect hits the mouse or not. I use the if hive_rect.collidepoint(event.pos) line for this. If a collision has occurred, I delete the hive with the del hive_list[index] line. index in the code below refers to the hive index in the hive_list array. For each hive_rect item and index index in the hive_list array, I need to check the collision. Any hive should not be removed. Only the hive I clicked on should be removed.
if event.type == pygame.MOUSEBUTTONDOWN:
for index,hive_rect in enumerate(hive_list):
if hive_rect.collidepoint(event.pos):
del hive_list[index]
In the next step, I draw the images on the page:
SCREEN.blit(BG,(0,0))
for hive_rect in hive_list:
SCREEN.blit(HIVE_SURFACE,hive_rect)
if len(hive_list) <= 0:
SCREEN.blit(TEXT_SURFACE,TEXT_RECT)
SCREEN.blit(CROSSHAIR,CROSSHAIR_rect)
Finally, I update all the changes and display them. This loop is executed 120 times per second. I set the game speed with Clock.tick(120).
pygame.display.update()
Clock.tick(120)









Comments