I resumed an old college-era project and was trying to convert my 2d snake to a 3d version . After installing OpenGL and PyGame libraries , when I try to start it , it crashes on a loading screen , basically as if it wants to start but fails ; the doubt is if this problem is related to apple silicon M1 ( in my case MBP M1 13” with 8 GB Ram ) . If this was not the problem , what could it be ?
If need be , I can also paste the old code of the 2D version .
I have not programmed for years now dealing with networking , however I would be curious to know if I did something wrong .
Thanks
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
# Constants
WIDTH, HEIGHT = 800, 600
FOOD_SIZE = 0.5
SNAKE_SIZE = 1.0
INITIAL_DELAY = 0.1
GRID_SIZE = 20
# Game Variables
delay = INITIAL_DELAY
score = 0
high_score = 0
direction = 'UP'
snake_positions = [(0, 0, 0)] # Start position at the origin
food_position = (random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE))
# Initialize Pygame and OpenGL
pygame.init()
pygame.display.set_mode((WIDTH, HEIGHT), DOUBLEBUF | OPENGL)
gluPerspective(45, (WIDTH / HEIGHT), 0.1, 50.0)
glTranslatef(0.0, 0.0, -30)
glEnable(GL_DEPTH_TEST)
# Function to draw a cube at a given position
def draw_cube(position, color):
x, y, z = position
vertices = [
(x - SNAKE_SIZE / 2, y - SNAKE_SIZE / 2, z - SNAKE_SIZE / 2),
(x + SNAKE_SIZE / 2, y - SNAKE_SIZE / 2, z - SNAKE_SIZE / 2),
(x + SNAKE_SIZE / 2, y + SNAKE_SIZE / 2, z - SNAKE_SIZE / 2),
(x - SNAKE_SIZE / 2, y + SNAKE_SIZE / 2, z - SNAKE_SIZE / 2),
(x - SNAKE_SIZE / 2, y - SNAKE_SIZE / 2, z + SNAKE_SIZE / 2),
(x + SNAKE_SIZE / 2, y - SNAKE_SIZE / 2, z + SNAKE_SIZE / 2),
(x + SNAKE_SIZE / 2, y + SNAKE_SIZE / 2, z + SNAKE_SIZE / 2),
(x - SNAKE_SIZE / 2, y + SNAKE_SIZE / 2, z + SNAKE_SIZE / 2),
]
edges = [
(0, 1), (1, 2), (2, 3), (3, 0),
(4, 5), (5, 6), (6, 7), (7, 4),
(0, 4), (1, 5), (2, 6), (3, 7)
]
glBegin(GL_QUADS)
for vertex in vertices:
glColor3fv(color)
glVertex3fv(vertex)
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
# Function to update the snake's position
def update_snake():
global snake_positions, food_position, score, delay
x, y, z = snake_positions[0]
if direction == 'UP':
y += SNAKE_SIZE
elif direction == 'DOWN':
y -= SNAKE_SIZE
elif direction == 'LEFT':
x -= SNAKE_SIZE
elif direction == 'RIGHT':
x += SNAKE_SIZE
elif direction == 'FORWARD':
z -= SNAKE_SIZE
elif direction == 'BACKWARD':
z += SNAKE_SIZE
new_head = (x, y, z)
snake_positions = [new_head] + snake_positions[:-1]
# Check for food collision
if distance(new_head, food_position) < FOOD_SIZE:
snake_positions.append(snake_positions[-1])
food_position = (random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE))
score += 10
delay = max(delay - 0.005, 0.05)
# Function to calculate the distance between two points
def distance(pos1, pos2):
return ((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2 + (pos1[2] - pos2[2]) ** 2) ** 0.5
# Function to handle keyboard events
def handle_keys():
global direction
keys = pygame.key.get_pressed()
if keys[K_w]:
direction = 'UP'
elif keys[K_s]:
direction = 'DOWN'
elif keys[K_a]:
direction = 'LEFT'
elif keys[K_d]:
direction = 'RIGHT'
elif keys[K_q]:
direction = 'FORWARD'
elif keys[K_e]:
direction = 'BACKWARD'
# Main game loop
def game_loop():
global score, high_score, delay
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
return
handle_keys()
update_snake()
# Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Draw the food
draw_cube(food_position, (1, 0, 0))
# Draw the snake
for segment in snake_positions:
draw_cube(segment, (0, 1, 0))
pygame.display.flip()
pygame.time.wait(int(delay * 1000))
# Check for collisions with the body or boundaries
if distance(snake_positions[0], food_position) < SNAKE_SIZE:
if score > high_score:
high_score = score
score = 0
delay = INITIAL_DELAY
snake_positions = [(0, 0, 0)]
food_position = (random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE), random.randint(-GRID_SIZE, GRID_SIZE))
if __name__ == "__main__":
game_loop()
Thanks
Raffaele S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.