0

I am new to Python and tried to make a 3D game similar to the ones I used to make with Processing, the code may be a bit unorthodox because of this. I used the basic moving formula I always use (xPos = sin(turn)*modificator), where I define the modificator in that way, that it is set to e.g. 5 while a KEYDOWN event is happening, and back to 0 when a KEYUP event happens. However, if I press the w key to make my figure to go forward, it only reacts, when I press the s key, too. Same with a and d. I tried it before with coding this in the main loop:

 keys = pygame.key.get_pressed()
        if keys.[pygame.K_w] :
            w_pressed = True

If w_pressed is True, the modificator is bigger than 0. I coded a reaction to a KEYUP event to that, too. It still made this problem with this code. So here is the whole code, I made a sphere drawing method, so one could get some orientation.

import pygame
import OpenGL
from OpenGL.GL import*
from OpenGL.GLU import *
import sys
import math

pygame.init()

screen = pygame.display.set_mode((1000,700), pygame.DOUBLEBUF|pygame.OPENGL)
pygame.display.set_caption("First Pycharm Game")

w_pressed = False
s_pressed = False
a_pressed = False
d_pressed = False
xPos = 0
zPos = 0
yPos = 0
remindturn = 0


class Figure :


    def draw_figure(self) :

        glPushMatrix()
        glTranslate(xPos,yPos,zPos)
        glRotatef(remindturn,0,1,0)
        glColor3f(1,0,0)
        glBegin(GL_TRIANGLES)
        glVertex3f(0,0,0)
        glVertex3f(0,250,0)
        glVertex3f(250,0,0)
        glEnd()
        glPopMatrix()

    def modificators(self) :

        global modificator1
        global modificator2
        if w_pressed == True :
            modificator1 = -15
        if s_pressed == True :
            modificator1 = 15
        if a_pressed == True :
            modificator2 = 15
        if d_pressed == True :
            modificator2 = -15
        if w_pressed == False :
            modificator1 = 0
        if s_pressed == False :
            modificator1 = 0
        if a_pressed == False :
            modificator2 = 0
        if d_pressed == False :
            modificator2 = 0


    def mouseturn(self) :

        global mouseturnX
        global mouseturnY
        mouse_pos = pygame.mouse.get_pos()
        if mouse_pos[0] > 500 :
            mouseturnX = (mouse_pos[0] - 500)*0.01
        if mouse_pos[0] < 500 :
            mouseturnX = (-500 + mouse_pos[0])*0.01
        if mouse_pos[1] > 350 :
            mouseturnY = (mouse_pos[1] - 350)*0.05
        if mouse_pos[1] < 350 :
            mouseturnY = (-350 + mouse_pos[1])*0.05

    def move_figure(self) :

        global xPos
        global zPos
        xPos = math.sin(mouseturnX)*modificator1+xPos
        zPos = math.cos(mouseturnX)*modificator1+zPos
        xPos = math.cos(mouseturnX)*modificator2+xPos
        zPos = math.sin(mouseturnX)*modificator2+zPos

    def remind_turn(self) :

        global remindturn
        if modificator1 or modificator2 != 0 :
            remindturn = mouseturnX

    def camera(self) :
        global xCam
        global yCam
        global zCam
        global camdist
        camdist = 400
        xCam = math.sin(mouseturnX)*camdist+xPos
        yCam = yPos + mouseturnY
        zCam = math.cos(mouseturnX)*camdist+zPos
        glViewport(0,0,1000, 700)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60, (1000/700), 0.1, 10000.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(xCam,yCam,zCam,xPos,yPos,zPos,0,-1,0)


def ball() :
    glPushMatrix()
    glTranslate(0,0,0)
    glColor3f(1,1,1)
    form = gluNewQuadric()
    gluSphere(form, 100, 10, 10)
    glPopMatrix()

running = True
while running == True :
    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            running = False
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN :
            if event.key == pygame.K_w :
                w_pressed = True
        if event.type == pygame.KEYDOWN :
            if event.key == pygame.K_s :
                s_pressed = True
        if event.type == pygame.KEYDOWN :
            if event.key == pygame.K_a :
                a_pressed = True
        if event.type == pygame.KEYDOWN :
            if event.key == pygame.K_d :
                d_pressed = True
        if event.type == pygame.KEYUP :
            if event.key == pygame.K_w :
                w_pressed = False
        if event.type == pygame.KEYUP :
            if event.key == pygame.K_s :
                s_pressed = False
        if event.type == pygame.KEYUP :
            if event.key == pygame.K_a :
                a_pressed = False
        if event.type == pygame.KEYUP :
            if event.key == pygame.K_d :
                d_pressed = False
        ball()
        testfigure = Figure()
        testfigure.mouseturn()
        testfigure.modificators()
        testfigure.move_figure()
        testfigure.draw_figure()
        testfigure.camera()
        pygame.display.flip()
        glClearColor(0,1,1,1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

Thank you for your time. Thank you for your help

Edit: No, I am not asking how I can move an object. My question is a lot mor specific: My object moves, but it only does so, when the key which sets the modificator to the opposite is pressed simultaneously to the original key. I tried finding the problem, and I can not find it, so I am asking this question.

  • 1
    It is a matter of [Indentation](https://docs.python.org/3/reference/lexical_analysis.html). You have to run the code in the application loop instead of the event loop. – Rabbid76 May 26 '22 at 19:54
  • I guess the application loop is the loop opened by the while statement, so I tried it and put all the code where I define the keydown and keyup events one indentation back. When I ran the code, nothing happened when I pressed the keys, then I put the code below this into the application loop, it worked like it worked in the event loop, still the problem with the keys was there. – programming_noob May 26 '22 at 20:26
  • 1
    No. All your code is in the event loop (`for event in pygame.event.get() :`) – Rabbid76 May 26 '22 at 20:28

0 Answers0