Mariano_1 0 Newbie Poster

Hey I'm coding Tron which is basically a two player snake game, I'm a noob when it come to pygame and python and need help with the collision logic and would love it if you could help me. (the comments are just ideas I had for the loops for the collisions that didn't work out) I need one loop for when one head touches the other's body which should say which crashed into who and also one loop that kills the snake that runs into himself.

import pygame
import time
import random
import os

pygame.init()

fps = pygame.time.Clock()

magenta = (255,0,255)
red = (255,0,0)
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
mygreen = (0,200,54)
green = (0,255,0)
smallfont = pygame.font.SysFont("Timesnewroman", 25)
medfont = pygame.font.SysFont("Timesnewroman", 50)
largefont = pygame.font.SysFont("comicsansms", 80)

pygame.display.set_caption("PyTron")
counter = 0
grow, growth_rate = 100,1
speed = 1
p1_size = 10,10
p2_size = 10,10

p1_current = 0
p2_length = 10
p2_width = 10
p1_length = 10
p1_width = 10
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))

def MsgTitleOver(msg,color, y_displace = 0):
    screen_text = medfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/2.7, display_height/7])
def MsgText1Over(msg,color, y_displace = 0):
    screen_text = smallfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/4, display_height/2])

def MsgOnRoundEnd(msg,color, y_displace = 0):
    screen_text = smallfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/3, (display_height/2) + 100])
def MsgTitle(msg,color, y_displace = 0):
    screen_text = medfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/4, display_height/7])
def MsgText_1(msg,color, y_displace = 0):
    screen_text = smallfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/7, display_height/3])
def MsgText_2(msg,color, y_displace = 0):
    screen_text = smallfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/9, display_height/2.5])
def MsgText_3(msg,color, y_displace = 0):
    screen_text = smallfont.render (msg , True, color)
    gameDisplay.blit(screen_text, [display_width/2, display_height/2])

def MsgOnScreen(msg, color, y_displace = 0, size = "small"): #message displayed to user through screen with parameters "msg": type of message , "color": color of message
    gameDisplay.blit#creo el object donde dibujar

def game_intro():
#or event in pygame.event.get():
            #if event.type == pygame.KEYDOWN:
                #if event.key == pygame.K_u:
    game_intro= True
    while game_intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    game_intro = False

                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()

        gameDisplay.fill(white)
        MsgTitle("Welcome to PyTron",blue,10)
        MsgText_1("You are a snake if you run into the other one you lose",mygreen,30)
        MsgText_2("Player 1 Controls: Arrow Keys     Player 2 Controls: WASD",mygreen,70)

        pygame.display.update()
        fps.tick(15)

def MsgUser(msg,color,y_displacement =0, size = "small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = (display_width / 2), (display_height / 2)+y_displacement
    gameDisplay.blit(textSurf, textRect)
        #screen_text = smallfont.render(msg, True, color) #el mensaje que se va a escribir y el color del mensaje

#def message_to_screen (msg, color, y_displacement = 0 , size = "small"):
    #textSurf, textRect = text_objects(msg, color, size)
    #textRect.center = (display_width/2), (display_height/2) + y_displacement
    #gameDisplay.blit(textSurf, textRect)

def text_objects(text, color, size):
    if size == "small":
        textSurface = smallfont.render(text, True, color)
    elif size == "medium":
        textSurface = medfont.render(text, True, color)
    elif size == "large":
        textSurface = largefont.render(text, True, color)

def gameLoop():

    display_width = 800
    display_height = 600
    gameDisplay = pygame.display.set_mode((display_width, display_height))
    gameDisplay.fill(white)
    pygame.display.update()
    game_intro = False
    gameExit = False
    gameOver = False
    gameLoad = False
    counter = 0
    p2_list = []
    p1_list = []
    p1_x = 600
    p1_y = 300
    p1_x_change = 0
    p1_y_change = 0
    p1_head_x = 0
    p1_head_y = 0
    p2_x = 200
    p2_y = 300
    p2_x_change = 0
    p2_y_change = 0
    p2_block_size = 2
    p1_block_size = 2

    while not gameExit:

        def p1(p1_x, p1_y):
            for p1_elements in p1_list:
                pygame.draw.rect(gameDisplay, red, [p1_elements[0],p1_elements[1], p1_width, p1_length])

        def p2(p2_x, p2_y):
            for p2_elements in p2_list:
                pygame.draw.rect(gameDisplay, black, [p2_elements[0],p2_elements[1], p2_width, p2_length])

        #while gameLoad == True:
            #counter = counter + 1
            #MsgOnRoundEnd("Round End press SPACE to continue",black)
            #pygame.display.update()

            #for event in pygame.event.get():
                #if event.type == pygame.KEYDOWN:
                    #if event.key == pygame.K_SPACE:
                        #gameLoop()

        while gameOver == True:
            gameDisplay.fill(white)
            MsgTitleOver("Game Over",
                              red,
                              y_displace=-50,
                              )
            MsgText1Over("Press T to try again or Q to exit the game",
                              black,
                              y_displace = 50,
                              )

            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_t:
                        gameLoop()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_u:
                    pygame.mixer.music.load(os.path.join("OUAT", "OUAT Undertale.mp3")) #this imports the music first array is the folder next one is hte file
                    pygame.mixer.music.play(-1)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    p1_x_change = -p1_block_size
                    p1_y_change = 0

                elif event.key == pygame.K_RIGHT:
                    p1_x_change = p1_block_size
                    p1_y_change = 0
                elif event.key == pygame.K_DOWN:
                    p1_x_change = 0
                    p1_y_change = p1_block_size
                elif event.key == pygame.K_UP:
                    p1_x_change= 0
                    p1_y_change = -p1_block_size

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    p2_x_change = -p2_block_size
                    p2_y_change = 0
                elif event.key == pygame.K_d:
                    p2_x_change = p2_block_size
                    p2_y_change = 0
                elif event.key == pygame.K_s:
                    p2_x_change = 0
                    p2_y_change = p2_block_size
                elif event.key == pygame.K_w:
                    p2_x_change = 0
                    p2_y_change = -p2_block_size

        for eachSegment in p1_list[:-1]:
            if eachSegment == p1_head:
                gameOver = True

        p1_y += p1_y_change
        p1_x += p1_x_change
        p1_head_y = p1_y + p1_y_change
        p1_head_x = p1_x + p1_x_change
        p1_head_position = (p1_head_x, p1_head_y)
        #p1_body_x = p1_head_x - p1_head_position
        #p1_body_y = p1_head_y - p1_head_position
        #p1_body = (p1_body_x, p1_body_y)

        p1_current = (p1_head_x, p1_head_y)

        p2_head_y = p2_y + p2_y_change
        p2_head_x = p2_x + p2_x_change

        p2_y += p2_y_change
        p2_x += p2_x_change

        p1_head = []
        p1_head.append(p1_x)
        p1_head.append(p1_y)
        p1_list.append(p1_head)
        p1(p1_block_size, p1_list)

        #p2_body = (p2_body_x , p2_body_y )
        p2_head_position = (p2_head_x, p2_head_y)
        #p2_body_x = p2_head_position - p2_head_x
        #p2_body_y = p2_head_position - p2_head_y
        p2_head = []
        p2_head.append(p2_x)
        p2_head.append(p2_y)
        p2_list.append(p2_head)
        p2(p2_block_size, p2_list)
        p2_head_x_uncertainty_one = p2_head_x + 0.8
        p2_head_x_uncertainty_two = p2_head_x - 0.8
        p2_head_y_uncertainty_one = p2_head_y + 0.8
        p2_head_y_uncertainty_two = p2_head_y - 0.8
        if p1_x <0 or p1_x > 800 or p1_y > 600 or p1_y < 0:
            gameOver = True
        if p2_x <0 or p2_x > 800 or p2_y > 600 or p2_y < 0:
            gameOver = True

        #for p1_head_position in p1_head[:-1] : #crash
        #if p1_head_x == p2_head_x: #or p1_head_y >= p2_head_y and p1_head_y <= p2_head_y_uncertainty: #or p1_head_x == p2_body_x: #or p1_head_y == p2_head_y: #or p1_head_x == p2_head and p1_head_y == p2_head:
            #gameOver = True
        #if p1_head_x <= p2_head_x_uncertainty_one and p1_head_x >= p2_head_x_uncertainty_two:
            #gameOver = True

        #if p1_head == p1_list[-1]:
            #gameOver = True

        #if p1_head_y == p2_head_y or p1_head_y <= p2_head_y_uncertainty_one and p2_head_y_uncertainty_two:
            #gameOver = True

        #for p1_head_position in p1_list[:-1]:
            #if p1_head_position == p1_head:
                    #gameOver = True

        #elif p1_head_y >= p2_head_y and p1_head_y <= p2_head_y_uncertainty:
            #gameExit = True

        #if p1_head_position == p2_body: #or p1_head_position == p2_head_y:
            #gameExit = True
        #p2_head = (p2_head_x, p2_head_y)
        fps.tick(60)
        pygame.display.update()

    pygame.quit()
    quit()
game_intro()
gameLoop()

Thanks for taking the time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.