Python Forum
Border detection - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Border detection (/thread-27091.html)



Border detection - Guzle03 - May-25-2020

import pygame
# basis definities

# kleuren
zwart = (0,0,0)
wit = (255,255,255)
licht_groen = (128,255,0)
donker_groen = (0,130,0)
rood = (255,0,0)
geel = (255,255,0)
zand = (194,178,128)
bruin = (210,105,30)
saddle_bruin = (139,69,19)
zand_bruin = (244,169,96)

# spelgegevens
screen_width = 800 # schermbreedte
screen_heigth = 600 # schermhoogte
FPS = 15 #frames per seconde

# tankgegevens
tank_width = 60
tank_heigth = 10
tank_wheel = 5
tank_speed = 2

tank_positionX = screen_width*0.1
tank_positionY = screen_heigth*0.9




pygame.init()
gameScreen = pygame.display.set_mode((screen_width,screen_heigth))
pygame.display.set_caption('Tank')
game = True
clock = pygame.time.Clock()


    
def tank_build(startX, startY):
    x = int (startX)
    y = int (startY)
    
    # circle(surface, color, center, radius)
    # 6 wielen
    pygame.draw.circle(gameScreen, zwart, (x-15, y),tank_wheel)
    pygame.draw.circle(gameScreen, zwart, (x-5, y),tank_wheel)
    pygame.draw.circle(gameScreen, zwart, (x+5, y),tank_wheel)
    pygame.draw.circle(gameScreen, zwart, (x+15, y),tank_wheel)
    pygame.draw.circle(gameScreen, zwart, (x-21, y-6),tank_wheel)
    pygame.draw.circle(gameScreen, zwart, (x+21, y-6),tank_wheel)
    # rect(surface, color, rect)
    # body
    pygame.draw.rect(gameScreen, bruin,(x-20, y-8,
                                               tank_width-20, tank_heigth))
    # polygon(surface, color, points)
    pygame.draw.polygon(gameScreen,bruin, ((x-30,y-8), (x+30, y-8),
                                                (x+20, y-15), (x-20, y-15))) 
   
    # gun
    # line (surface, color, line,width)
    pygame.draw.line (gameScreen, zwart, (x+15,y-18), (x+35,y-18), 2)
    # ellipse(surface, color, rect)
    pygame.draw.ellipse(gameScreen, bruin, ((x-13,y-22, 30,15)))

def game_loop():
    tankY = tank_positionY
    tankX = tank_positionX
    while game:
        
        gameScreen.fill(wit)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        
        tank_build(tankX,tank_positionY )
        pygame.draw.rect(gameScreen, licht_groen, (0,int(tankY)+5,
                                                   screen_width, 15))
        pygame.display.update()
        tankX += tank_speed
        if tankX > screen_width:
            tankX = -10
        
        clock.tick (FPS)
# Game
game_loop()



RE: Border detection - bowlofred - May-25-2020

Do you have a question about it? Does it do what you expect it to do?


RE: Border detection - Guzle03 - May-25-2020

Hello , this is a program I wrote, but I have a problem. I don't know how to add border detection. So when the tank touches the left or right of the road, it must go in the other direction. Can you help me please?