简体   繁体   中英

What is the easiest way to make a triangles

I'm making a game in pygame that is just like the zigzag thing in geometry dash. The problem here is that you can't use rectangles as obstacles, because it looks like you are going diagionally, so the surfaces of the obstacles have to be in like 45° angle. So, what is the easiest way to make a slope in pygame?

Try to use:

pygame.draw.polygon(screen, color, (point-x, point-y, point-z))

Here's an example:

import pygame
pygame.init()

screen = pygame.display.set_mode([500, 500])

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))
    pygame.draw.polygon(screen, (0, 255, 255), ((25,75),(320,125),(250,375)))

    pygame.display.flip()

pygame.quit()

You can create whatever triangle you want using some math. Here is a function that takes in how big the triangle needs to be (scale), angle between the points (internalAngle) and the rotation.

def makeTriangle(scale, internalAngle, rotation):
    #define the points in a uint space
    ia = (radians(internalAngle) * 2) - 1
    p1 = (0, -1)
    p2 = (cos(ia), sin(ia))
    p3 = (cos(ia) * -1, sin(ia))

    #rotate the points
    ra = radians(rotation) 
    rp1x = p1[0] * cos(ra) - p1[1] * sin(ra)
    rp1y = p1[0] * sin(ra) + p1[1] * cos(ra)                 
    rp2x = p2[0] * cos(ra) - p2[1] * sin(ra)
    rp2y = p2[0] * sin(ra) + p2[1] * cos(ra)                        
    rp3x = p3[0] * cos(ra) - p3[1] * sin(ra)                         
    rp3y = p3[0] * sin(ra) + p3[1] * cos(ra)
    rp1 = ( rp1x, rp1y )
    rp2 = ( rp2x, rp2y )
    rp3 = ( rp3x, rp3y )

    #scale the points 
    sp1 = [rp1[0] * scale, rp1[1] * scale]
    sp2 = [rp2[0] * scale, rp2[1] * scale]
    sp3 = [rp3[0] * scale, rp3[1] * scale]
                    
    return Triangle(sp1, sp2, sp3)

One limitation is that it can only make equilateral triangle, but its easy enough to modify the function to remove this limitation. You could take three different scale values and scale the three points accordingly.

Example:

import pygame
from math import sin, cos, pi, radians
from sys import exit as _exit

#stores three points of the triangle
class Triangle:
    def __init__(self, p1, p2, p3):
        self.p1 = p1
        self.p2 = p2
        self.p3 = p3

def makeTriangle(scale, internalAngle, rotation):
    #define the points in a uint space
    ia = (radians(internalAngle) * 2) - 1
    p1 = (0, -1)
    p2 = (cos(ia), sin(ia))
    p3 = (cos(ia) * -1, sin(ia))

    #rotate the points
    ra = radians(rotation)
    rp1x = p1[0] * cos(ra) - p1[1] * sin(ra)
    rp1y = p1[0] * sin(ra) + p1[1] * cos(ra)                 
    rp2x = p2[0] * cos(ra) - p2[1] * sin(ra)
    rp2y = p2[0] * sin(ra) + p2[1] * cos(ra)                        
    rp3x = p3[0] * cos(ra) - p3[1] * sin(ra)                         
    rp3y = p3[0] * sin(ra) + p3[1] * cos(ra)
    rp1 = ( rp1x, rp1y )
    rp2 = ( rp2x, rp2y )
    rp3 = ( rp3x, rp3y )

    #scale the points 
    sp1 = [rp1[0] * scale, rp1[1] * scale]
    sp2 = [rp2[0] * scale, rp2[1] * scale]
    sp3 = [rp3[0] * scale, rp3[1] * scale]
                    
    return Triangle(sp1, sp2, sp3)

def drawTriangle(tri, color=(0, 0, 0)):
    pygame.draw.line(window, color, tri.p1, tri.p2)
    pygame.draw.line(window, color, tri.p2, tri.p3)
    pygame.draw.line(window, color, tri.p3, tri.p1)

def offsetTriangle(triangle, offsetx, offsety):
    triangle.p1[0] += offsetx;  triangle.p1[1] += offsety;
    triangle.p2[0] += offsetx;  triangle.p2[1] += offsety;
    triangle.p3[0] += offsetx;  triangle.p3[1] += offsety;

pygame.init()
window = pygame.display.set_mode((600, 600))

#create whatever type of triangle you want
mytriangle1 = makeTriangle(150, 30, 10)
offsetTriangle(mytriangle1, 100, 100)
mytriangle2 = makeTriangle(100, 45, 270)
offsetTriangle(mytriangle2, 300, 300)
mytriangle3 = makeTriangle(70, 45, 180)
offsetTriangle(mytriangle3, 400, 400)

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            _exit();


    window.fill((255, 255, 255))

    #draw the triangles
    drawTriangle(mytriangle1)
    drawTriangle(mytriangle2)
    drawTriangle(mytriangle3)
    
    pygame.display.flip()

For your game, you cab create "models" of whatever triangles you want and store it once. Then just move those triangles around in the screen according to the needs of your game later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM