Python Forum
Bringing My Game Code Into Class Format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bringing My Game Code Into Class Format
#1
I'm in the very early stages of developing a duel-player tank game where there are two tanks, a map, and the objective is to shoot the other tank before they can to you. Right now, I'm really far off that goal as I've only been able to make a single tank rotate and set boundaries for the screen. The tank is just a small 16 x 16 sprite. My code seems a bit messy and I'm just wondering if someone can help me format by arranging the variables with classes and init (self) functions so I can add the projectiles, etc, more easily later on. Here is my code:

import pygame
import time
import math
import random

pygame.init()

display_width = 800
display_height = 600
white = (255, 255, 255)
black = (0,0,0)
tank_width = 20
tank_height = 20



gameDisplay = pygame.display.set_mode((display_width, display_height), 0, 32)
pygame.display.set_caption("Tank Game")
keys = [False, False, False, False, False]
tankPos = [100, 100]

tankImg = pygame.image.load('tank1.png')


clock = pygame.time.Clock()
    

def textBox(text,font):
    theText = font.render(text, True, black)
    return theText, theText.get_rect()

    
 
def redraw():
    angle = 0
    position = pygame.mouse.get_pos()
    angle = math.atan2(position[1] - (tankPos[1] + 32), position[0] - (tankPos[0] + 26))
    playerrot = pygame.transform.rotate(tankImg, 360 - angle*57.29)
    tankPos1 = (tankPos[0] - playerrot.get_rect().width/2, tankPos[1] - playerrot.get_rect().height / 2) 
    gameDisplay.blit(playerrot, tankPos1)
    
def gameLoop():
    x = (display_width * 0.03)
    y = (display_height * 0.01)
    xVelocity = 0
    yVelocity = 0

    tankImg = pygame.image.load('tank1.png')
    gameExit = False
    
 
    while gameExit == False:

        
        pygame.display.flip()
        
    
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    keys[0] = True
                elif event.key == pygame.K_LEFT:
                    keys[1] = True
                elif event.key == pygame.K_DOWN:
                    keys[2] = True
                elif event.key == pygame.K_RIGHT:
                    keys[3] = True
                elif event.key == pygame.K_SEMICOLON:
                    keys[4] == True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    keys[0] = False
                elif event.key == pygame.K_LEFT:
                    keys[1] = False
                elif event.key == pygame.K_DOWN:
                    keys[2] = False
                elif event.key == pygame.K_RIGHT:
                    keys[3] = False
                elif event.key == pygame.K_COMMA:
                    keys[4] = False
        if keys[0]:
            tankPos[1] -= 5
        elif keys[2]:
            tankPos[1] += 5
        elif keys[1]:
            tankPos[0] -= 5
        elif keys[3]:
            tankPos[0] += 5
        elif keys[4]:
            projectiles()

                  
            
            
        
      
        x += xVelocity
        y += yVelocity
        gameDisplay.fill(white)
        #tank(x,y,angle)
        if (tankPos[0] > display_width - tank_width):
            tankPos[0] = 766 
        if tankPos[0] < 0:
            tankPos[0] = 30
    
        if tankPos[1] < 0:
            tankPos[1] = 6
        
        if tankPos[1] > 596:
            tankPos[1] = 594
    
        
        
        clock.tick(60)
        redraw()

gameLoop()
Reply
#2
The best move would be to use the pygame Sprite class. Plenty of tutorials and examples out there. You can use one class for your tanks and one for your projectiles. This will also allow you to use the pygame sprite collision to tell when a tank gets hit.

www.101computing.net/creating-sprites-using-pygame/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My game's code works but Python still freezes at random intervals. game_slayer_99 13 7,411 Dec-09-2021, 11:23 AM
Last Post: metulburr
  Game “Pong” I have problems with the code BenBach18 2 3,452 Jan-10-2021, 05:16 PM
Last Post: michael1789
  Creating a “Player” class, and then importing it into game onizuka 4 2,977 Sep-01-2020, 06:06 PM
Last Post: onizuka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020