Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Border detection
#1
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()
Reply
#2
Do you have a question about it? Does it do what you expect it to do?
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make curses.border() use A_BOLD atttribute? pjfarley3 0 4,777 Feb-03-2021, 11:22 PM
Last Post: pjfarley3
  Openpyxl_adding a thick border around outside of page pcsailor 1 2,722 Oct-13-2018, 01:15 AM
Last Post: pcsailor

Forum Jump:

User Panel Messages

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