how to make a crosshair with x and y coordinates python code example
Example: how to make a crosshair with x and y coordinates python
#this imports all the nerd stuff so you can use the code without errors
import pygame
import time
pygame.init()
#all of the variables names and meaning
#1):red red is just the colour red.. used to paint the rectangles.. i dont know why
#2):rect_z is the active size of the middle rectangle / rect_z
#3):dick is the minus value of the middle rectangle / rect_z
#4):screen is the variable for the pygame window.. it is used a lot to make rectangles
#5):done is the variable for if the pygame window is running or not
#6):xmousepos is the active value of the x value of your cursors position
#7):ymousepos is the active value of the x value of your cursors position
#8):keys is the variable of any keypress on your keyboard
#pre-written variables
red = (255,0,0)
rect_z = 35
dick = 20
done=False
green = (0,128,0)
blue = (0,0,255)
black = (100,0,0)
rect_z2 = 45
dick2 = 30
#the pre-written code to open a pygame window
screen=pygame.display.set_mode((800, 800))
pygame.display.set_caption("Crosshair x, y, z,")
#this is what happends forever while the code is running
while not done:
#this to make sure you can close the pygame window after used
#so you dont have to restart your pc after executing code on it
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
#this makes the screen black
screen.fill((255,255,255))
screen.fill((0,0,0))
#this tracks the position of your mouse as long as the code is running
xmousepos=pygame.mouse.get_pos()[0]
ymousepos=pygame.mouse.get_pos()[1]
#this draws all the needed rectangles for you and updates them when they move
#so they dont just sit still all the time
screen.fill(red, rect=[xmousepos-5,ymousepos-1000,7,2500])
screen.fill(green, rect=[xmousepos-1000,ymousepos-5,2500,7])
#screen.fill(black, rect=[xmousepos-dick2,ymousepos-dick2,rect_z2,rect_z2])
screen.fill(blue, rect=[xmousepos-dick,ymousepos-dick,rect_z,rect_z])
pygame.display.update()
#this prints the coordinates in the teminal for you
print(xmousepos,",",ymousepos,",",rect_z)
#this detects if you press KEY_UP or KEY_DOWN and then makes the middle rectangle bigger / smaller
#depending on what key you press
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if rect_z < 91:
#if rect_z2 < 100:
dick += 1
#dick2 += 1
rect_z += 2
#rect_z2 += 2
time.sleep(0.008)
if keys[pygame.K_DOWN]:
if rect_z > 21:
#if rect_z2 > 30:
dick -= 1
#dick2 += 1
rect_z -= 2
#rect_z2 -= 2
time.sleep(0.008)