i have recently started working with the pyopengl library and to test my knowledge i have decided to make a game. I have been having some issues with finishing the last part of the game tho, adding segments to the snake/making them follow you, down below you can find the source code of the game, there is a multilined comment, that's what i've tried doing and it didn't work, would appreciate any help if possible.
import random
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
pygame.init()
edges = (
(0, 1),
(2, 1),
(2, 3),
(2, 7),
(6, 3),
(6, 4),
(6, 7),
(5, 1),
(5, 4),
(5, 7),
(0, 3),
(0, 4)
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0),
(1,1,1),
(0,1,1),
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0),
(1,1,1),
(0,1,1)
)
def Cube(InputX, InputY, InputZ, color):
verticies = (
(InputX + 1, InputY + -1, InputZ + -1),
(InputX + 1, InputY + 1, InputZ + -1),
(InputX + -1, InputY + 1, InputZ + -1),
(InputX + -1, InputY + -1, InputZ + -1),
(InputX + 1, InputY + -1, InputZ + 1),
(InputX + 1, InputY + 1, InputZ + 1),
(InputX + -1, InputY + -1, InputZ + 1),
(InputX + -1, InputY + 1, InputZ + 1)
)
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vortex in surface:
x += 1
glColor3fv(colors[color])
glVertex3fv(verticies[vortex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glColor3fv(colors[10])
glVertex3fv(verticies[vertex])
glEnd()
headx = 0
heady = 0
RandX = random.randint(0, 28/2) * 2
RandY = random.randint(0, 28/2) * 2
grid = [[0] * 10 for x in range(9)]
display = (750, 800)
screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(120, (display[0] / display[1]), 0.1, 50.0)
#First image to display
glTranslatef(-13, -13, -10)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cubes = [[Cube(2*x,2*y, -5, 9) for x in range(15)] for y in range(15)]#Grid
Cube(headx, heady, -5, 1)
Cube(RandX, RandY, -5, 6)
pygame.display.flip()
dir = (0, 0)
MOVEEVENT, t = pygame.USEREVENT+1, 1
pygame.time.set_timer(MOVEEVENT, t)
Score = 0
Time = -1000
TailPos = [[3,3], [2,3]]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]: dir = (0, -1)
if keys[pygame.K_a]: dir = (-1, 0)
if keys[pygame.K_s]: dir = (0, 1)
if keys[pygame.K_d]: dir = (1, 0)
for e in pygame.event.get():
if e.type == MOVEEVENT: # is called every 't' milliseconds
if(dir == (0, -1)):
heady += 2
elif (dir == (-1, 0)):
headx -= 2
elif (dir == (0, 1)):
heady -= 2
elif (dir == (1, 0)):
headx += 2
if (headx >= 30): # checking if the snake is going out of bounds
headx = 0
elif (headx <= -1):
headx = 28
elif (heady >= 30):
heady = 0
elif (heady <= -1):
heady = 28
if((headx == RandX) and (heady == RandY)):
RandX, RandY = random.randint(0, 28/2) * 2, random.randint(0, 28/2) * 2
Score += 10
#if(Time > 10):
# Time -= 10
# print(Time)
'''for i in range(len(TailPos) - 1, 0, -1):
TailPos[i][0] = TailPos[i - 1][0];
TailPos[i][1] = TailPos[i - 1][1];
for j in range(len(TailPos)-1):
Cube(TailPos[i][j], TailPos[i][j], -5, 1) # Snake
for x in range(len(TailPos)):
for segment in TailPos:
for part in segment:
Cube(part,part, -5, 1)'''
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cubes = [[Cube(2*x,2*y, -5, 9) for x in range(15)] for y in range(15)]#Grid
Cube(RandX, RandY, -5, 6)#Fruit
Cube(headx, heady, -5, 1) # Snake
pygame.time.wait(Time)
pygame.display.flip()