Share your repls and programming experiences

← Back to all posts
bnml;'
2013007332 (0)

#Pygame library by Pygame community (https://www.pygame.org), public licen se

import pygame

Creates the types of game object

class Player_Character (pygame.sprite.Sprite):

    def __init__(self): 

        super(Player_Character, self).__init__() 

        self.surf = pygame.image.load("player.png").convert() 

        self.surf.set_colorkey((0, 0, 0),) 

        self.rect = self.surf.get_rect() 

class Computer_Character(pygame.sprite.Sprite):

     def __init__(self): 

         super(Computer_Character, self).__init__() 

         self.surf = pygame.image.load("computer.png").convert() 

         self.surf.set_colorkey((0, 0, 0),) 

         self.rect = self.surf.get_rect() 

Function to update the game display an output to the screen

def drawGame():

         spt_Player.rect.x = int_PlayerX 

         spt_Player.rect.y = int_PlayerY 

         spt_Computer.rect.x = int_ComputerX 

         spt_Computer.rect.y = int_ComputerY 

         str_Score = gme_Font.render("Score: {:.2f}".format(flt_Score),  True, (255, 255, 255)) 

         gme_Screen.fill((0, 0, 0)) 

         gme_Screen.blit(spt_Player.surf, spt_Player.rect) 

         gme_Screen.blit(spt_Computer.surf, spt_Computer.rect) 

         gme_Screen.blit(str_Score, (650, 25)) 

         pygame.display.flip() 

Starts the game, creates the screen, sprites and sets the variables

pygame.init()

gme_Font = pygame.font.Font(None, 25)

gme_Screen = pygame.display.set_mode((800, 500))

pygame.display.set_caption("Game name here: eg. Healthy Eating")

spt_Player = Player_Character()

spt_Computer = Computer_Character()

int_PlayerX = 200

int_PlayerY = 200

int_ComputerX = 600

int_ComputerY = 300

int_Player_Steps = 15

int_Computer_Steps = 10

bln_Game_Running = True

flt_Score = 0

Game loop

while bln_Game_Running:

Sets the game speed and increases score while the game is running

  pygame.time.delay(100) 

  flt_Score = flt_Score + 0.2 

Selection to change computer character X and y coordinates to move towards the player

  if int_ComputerX < int_PlayerX - 10: 

         int_ComputerX = int_ComputerX + int_Computer_Steps 

  elif int_ComputerX > int_PlayerX + 10: 

         int_ComputerX = int_ComputerX - int_Computer_Steps 

  elif int_ComputerY < int_PlayerY - 10: 

         int_ComputerY = int_ComputerY + int_Computer_Steps 

  elif int_ComputerY> int_PlayerY + 10: 

          int_ComputerY = int_ComputerY - int_Computer_Steps 

  else: 

          bln_Game_Running = False 

Detect keyboard button pressed and selection to change player X and y co-ordinates

  for event in pygame.event.get():    

   if event.type == pygame.QUIT: 

      bln_Game_Running = False 

   keys = pygame.key.get_pressed() 

   if keys[pygame.K_LEFT]: 

      int_PlayerX  = int_PlayerX - int_Player_Steps 

   if keys[pygame.K_RIGHT]: 

      int_PlayerX  = int_PlayerX + int_Player_Steps 

   if keys[pygame.K_UP]: 

      int_PlayerY  = int_PlayerY - int_Player_Steps 

   if keys[pygame.K_DOWN]: 

       int_PlayerY  = int_PlayerY + int_Player_Steps 

#Calls game display function

   drawGame() 

pygame.quit()

     
Comments
hotnewtop