Yenz1
Yenz1

Reputation: 94

Moving only one cube out of 2 in Python with PyOpenGL

So, I am very new to PyOpenGL and I want to make a simple 3D game.

I created a bigger cube (It should represent player) and a smaller one (It should represent the enemy). I need a player to be able to move left and right and enemies to come from the front. The problem is that they are in the same position.

I am using glTranslatef(x, y, z) to move a player but the enemy moves to the same position as player does.

Here is my code:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import*


class display:
    def __init__(self, width, height, window):
        self.width = width
        self.height = height
        self.window = window

    def update(self):
        pygame.display.flip()
        pygame.time.wait(10)

    def clear(self):
        glClearColor(0.15, 0.15, 0.15, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)


display = display(800, 600, pygame.display.set_mode((800, 600), DOUBLEBUF|OPENGL))


class player:
    def __init__(self, vertices, edges):
        self.vertices = vertices
        self.edges = edges

    def begin(self):
        glTranslatef(0.0, -5.0, -12.0)
        glRotatef(20, 1, 0.0, 0.0)

    def move(self):
        self.k_press = pygame.key.get_pressed()
        if self.k_press[pygame.K_LEFT]:
            glTranslatef(-0.2, 0, 0)
        if self.k_press[pygame.K_RIGHT]:
            glTranslatef(0.2, 0, 0)

    def draw(self):
        glBegin(GL_LINES)
        for edge in player.edges:
            for vertex in edge:
                glVertex3fv(player.vertices[vertex])
        glEnd()


player = player(
    ((1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1)),
    ((0, 1), (0, 3), (0, 4), (2, 1), (2, 3), (2, 7), (6, 3), (6, 4), (6, 7), (5, 1), (5, 4), (5, 7))
)


class enemy:
    def __init__(self, vertices, edges):
        self.vertices = vertices
        self.edges = edges

    def draw(self):
        glBegin(GL_LINES)
        for edge in enemy.edges:
            for vertex in edge:
                glVertex3fv(enemy.vertices[vertex])
        glEnd()


enemy = enemy(
    ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5)),
    ((0, 1), (0, 3), (0, 4), (2, 1), (2, 3), (2, 7), (6, 3), (6, 4), (6, 7), (5, 1), (5, 4), (5, 7))
)


gluPerspective(70, (display.width/display.height), 0.1, 50.0)

player.begin()

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

    display.clear()

    player.move()
    player.draw()
    enemy.draw()

    display.update()

pygame.quit()

For now, I just want the enemy and player to move without being in the same position. Anyways, thanks for help!

Upvotes: 1

Views: 979

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

First of all use glMatrixMode to distinguish between the projection matrix and the model matrix. The projection matrix should be set to the current projection matrix and the view matrix to the current model view matrix.

# set projection matrix
glMatrixMode(GL_PROJECTION)
gluPerspective(70, (display.width/display.height), 0.1, 50.0)

# set view matrix
glMatrixMode(GL_MODELVIEW)
glTranslatef(0.0, -5.0, -12.0)
glRotatef(20, 1, 0.0, 0.0)

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

    display.clear()

    player.move()
    player.draw()
    enemy.draw()

    display.update()

This setup will allow to change the point of view later.

An an individual movement, animation nad position for each object in the scene, can be achieved by an model matrix for each object. Store the current model view matrix before an object is draw and restore the matrix after the object is drawn by glPushMatrix/glPopMatrix.
This it is possible to set an individual position for each object. Store the current position of an object in attribute (self.pos = [0, 0, 0]). manipulate the position in move (e.g. self.pos[0] -= 0.2) and multiply the model translation matrix to the current model view matrix before the object is drawn (glTranslatef(*self.pos)). e.g:

class player:
    def __init__(self, vertices, edges):
        self.vertices = vertices
        self.edges = edges
        self.pos = [0, 0, 0]

    def move(self):
        self.k_press = pygame.key.get_pressed()
        if self.k_press[pygame.K_LEFT]:
            self.pos[0] -= 0.2
        if self.k_press[pygame.K_RIGHT]:
            self.pos[0] += 0.2

    def draw(self):
        glPushMatrix()
        glTranslatef(*self.pos)
        glBegin(GL_LINES)
        for edge in player.edges:
            for vertex in edge:
                glVertex3fv(player.vertices[vertex])
        glEnd()
        glPopMatrix()
class enemy:
    def __init__(self, vertices, edges):
        self.vertices = vertices
        self.edges = edges
        self.pos = [0, 0, 0]

    def draw(self):
        glPushMatrix()
        glTranslatef(*self.pos)
        glBegin(GL_LINES)
        for edge in enemy.edges:
            for vertex in edge:
                glVertex3fv(enemy.vertices[vertex])
        glEnd()
        glPopMatrix()

Upvotes: 1

Related Questions