Ich versuche, ein kleines Spiel zu bauen, das darauf basiert, das Kabel nicht zu berühren (Sie müssen einen Kreis um einen Draht/eine Linie manövrieren). Ich bin mir nicht sicher, wo ich anfangen soll, die Kollision zwischen dem Draht und der Kante des Kreises zu programmieren, weil sich der Draht zufällig von selbst bewegt. Muss ich eine Liste erstellen, in der alle Koordinaten der Linie gespeichert sind? Hier ist der Code für sie:PyGame Collsion Problem
import pygame
from random import randint
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
display_width = 600
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Wire")
font = pygame.font.SysFont("aerial", 20)
clock = pygame.time.Clock()
pygame.display.update()
def text_objects(text, colour):
textSurface = font.render(text, True, colour)
return textSurface, textSurface.get_rect()
def message_to_screen(msg, colour, xpos, ypos):
textSurf, textRect = text_objects(msg, colour)
textRect.center = (xpos), (ypos)
gameDisplay.blit(textSurf, textRect)
def gameLoop(FPS):
start = 0
from math import pi
gameExit = False
gameOver = False
num_top = 300
num_bot = 300
pygame.mouse.set_pos(display_width/2, display_height/2)
line_array =[]
line_thickness = 8
while not gameExit:
mouse = pygame.mouse.get_pos()
# print(mouse)
if (start == 0):
print("Game Started")
elif(mouse[0] <= 48) or (mouse[0] >= 547) or (mouse[1] <= 25) or (mouse[1] >=585):
gameOver = True
start += 1
if gameOver == True:
gameDisplay.fill(white)
message_to_screen("You Lost", red, 300, 300)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
rand_nums = [randint(-3, 3), randint(-3, 3)]
num_top += rand_nums[0]
num_bot += rand_nums[1]
message_to_screen("Press q or the x button to quit", blue, 100, 40)
pygame.display.update()
gameDisplay.fill(white)
pygame.draw.arc(gameDisplay, red, [mouse[0]-48, mouse[1]-25, 100, 40], 0, pi, 3)
pygame.draw.line(gameDisplay, black, [num_top, 0], [num_bot, 600], line_thickness)
pygame.draw.arc(gameDisplay, red, [mouse[0]-48, mouse[1]-25, 100, 40], 3 * pi, 2 * pi, 3)
clock.tick(FPS)
pygame.quit()
quit()
gameLoop(100)
Sie haben nur zwei Punkte von Kreis zu überprüfen, ob sie auf schwarze Linie ar - und es gibt eine mathematische Methode, um zu prüfen, ob ein Punkt online ist. Sie brauchen nur Start- und Endpunkt der Linie. Sie müssen nicht alle Punkte online auflisten. – furas
Wie mache ich das, wenn ich nur den Anfangs- und Endpunkt der Linie angegeben habe? Muss ich eine Liste der Pixel erstellen? –
http://gamedev.stackexchange.com/questions/26004/how-to-detect-2d-line-on-line-collision – Viney