• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By juicyz
      Hey all,
      I've been slowly working on my game called AotW for a while now.  I have come to the conclusions that it would be nice to cooperate with 1 or 2 others to help finish it.  Ive been trying to keep my GDD up to date with my ideas and development so that would give a better overview of the game when the time comes.  Currently I have a basic skeleton of the RPG elements needed but everything can still be discussed and talked about and we can transform my idea to something the group likes.
      The premise of the game is a Diablo-like procedurally generated map with RPG elements that include sockets, inventory, classes, abilities, crafting, loot, items, sockets, and enchanting.  This will be done in a 2D iso view as I can't do 3D art and I enjoy 2D games a lot.
       
      I don't plan on releasing this as this is more of a hobby project for me and I have a full-time job.  Though I'd like to start putting more hours into development and having others definitely will be motivation.  I also want to be able to say I have finally "finished" a game idea to some degree.  If the time comes and we want to release it, then we can go ahead and do so but that's not my purpose or plan. 
       
      Discord:
      Juicyz#3683
       
       
      Thanks,
      Juicyz
    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
      WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; }  
      game.cpp
    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
       
      This is my C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; } My WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      game.cpp
    • By drago28
      I'm using construct 2 physics I have a working bike with everything setup and I'm able to do frontflip and backflip by adding angular velocity to the bike
      I want to increase score/ add points and display the "backflip / frontflip" text when the bike does it.
      so please help me I want to know the logic and how it's done.
      p.s. example about construct 2 is not necessary, just explain me how other games do it or anything else you have to share
      Thanks,
      Drago
    • By otgs17
      You have 12 hours to search for supplies, transportation or you can continue your journey.
      But as soon as night falls, you must find shelter, turn it into a fortress to stand against the hordes of monsters.
      Equip soldiers, use bonuses and do not forget to follow the energy level of your fighters. 
      In battle be useful to your transport, it would be nice to find a tank.
      Every day the number of monsters to get more, they mutate into stronger and more dangerous forms.
      https://play.google.com/store/apps/details?id=com.otgs.FortTD&hl=en
      https://itunes.apple.com/us/app/fortress-td/id1367094965?mt=8
      https://www.youtube.com/watch?v=QIkXPikiRqc





  • Advertisement
  • Advertisement

Kivy Window Resizing Messes up Screen Formatting

Recommended Posts

I want to make it so the location and size of the buttons I use are changed to suit the background image when the screen is resized.

Here is my code so far:

"""
Combines various .py files and
allows the game to run.
"""
from kivy.app import App
from kivy.uix.image import Image
from kivy.config import Config
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout


class MainMenu(FloatLayout):
    def __init__(self, **kwargs):
        super(MainMenu, self).__init__(**kwargs)


        start_button = Button(pos=(25, 75),
                              background_normal="Start.png",
                              background_down="Start_Down.png",
                              size_hint=(.1, .1))

        load_button = Button(pos=(225, 75),
                             background_normal="Load.png",
                             background_down="Load_Down.png",
                             size_hint=(.1, .1))

        options_button = Button(pos=(425, 75),
                                background_normal="Options.png",
                                background_down="Options_Down.png",
                                size_hint=(.150, .1))

        quit_button = Button(pos=(708, 75),
                             background_normal="Quit.png",
                             background_down="Quit_Down.png",
                             size_hint=(.1, .1))

        background = Image(source="Main_Menu.png",
                           pos=(0, 0))

        self.add_widget(background)
        self.add_widget(start_button)
        self.add_widget(load_button)
        self.add_widget(options_button)
        self.add_widget(quit_button)

class BanditKing(App):
    def build(self):
        self.title = "Bandit King"
        self.icon = "Window_Icon.png"
        return MainMenu()


def main():
    Config.set("graphics", "width", "1600")
    Config.set("graphics", "height", "900")
    Config.write()
    BanditKing().run()


if __name__ == "__main__":
    main()

Here is what it should always look like.

image.thumb.png.ee5f34aa22ad187aa686a457c6a23eee.png

 

Here is what it does:

image.thumb.png.6ed34c84db53eee9623a2e9bf6e94507.png

You might be able to tell the "Options" button is slightly off line with the other buttons. I would also like to know how to fix that.

Edited by RidiculousName

Share this post


Link to post
Share on other sites
Advertisement

I don't use Python but I will give you an answer that is pretty much universal. In C++ I do this in a number of ways, and it can depend on if I have objects within their own view, which keeps buttons spaced the same, or if I create new button views with different to account for different resolutions.

Just a basic example:

Assuming your screen is 800x600 and you have two buttons, each are 210x59 and positioned at (165, 270), and (425, 270) *Top left x,y. The first buttons top left point is at the 20.625% mark of the screen (left to right), and the second button is at the 53.125% mark of the screen for the x axis. The y will be at 45%. You get this calculation by taking the x or y and dividing into the width or height of the res. ie.

BUTTON 1 -> 165/800 = 0.20625, 270/600 = 0.45

BUTTON 2 -> 425/800 = 0.53125, 270/600 = 0.45

Now that you know the position % marks, you can resize with ratios.

If your screen res changes to 1024x600 you would just re-set the position of the buttons like so:

BUTTON1.x = 1024*0.20625 equals->211.20

BUTTON1.y = 600*0.45 equals -> 270

You'll see they have the same % -> 211.20/1024 = 0.20625 * 100 = 20.625%

This however will only re-position objects to contain the same x,y ratio when working in a different resolution, this will indeed add spacing between the objects unless you resize the width and height as well. This means you either have to be working with a high res image that you can scale down within the ratios evenly, or design different graphics for those off ratios.

The same principle can be applied like the above to generate width and height ratios. Let us take the width and height of the button which is 210x59. To get the ratio we do the same as we did for x, y.

210/800 = 0.2625 -> 26.25%

59/600 = 0.0983333333333333 -> 9.833333333333333%

Now since we didn't up the height on res change the graphic height would be the same.

You would now have to change the graphic width and height as follows to maintain the same ratio.

BUTTON1.width = 1024*0.2625 equals ->268.8

BUTTON1.height = 600*0.0983333333333333 equals -> rounds to 59

So on display 1 with 800x600 we have buttons as follows:

Button1 = 210x59 and positioned at (165, 270)

Button2 = 210x59 and positioned at (425, 270)

On display 2 with 1024*600 we have the buttons as follows:

So on display 1 with 800x600 we have buttons as follows:

Button1 = 268.8x59 and positioned at (211.2, 270)

Button2 = 268.8x59 and positioned at (544, 270)

The below screen shots will show they have the same ratios - ignore the gray borders I used a screen snip it tool - screen size is the green.

image.png.a18cb34ad93574362a8e1089c466aba4.png

image.thumb.png.382a685589846149dc9dd615b279ec93.png

 

There are different methods such as using a view to draw your highest res graphics to, then scaling that view down which in turn scales all the graphics inside with the res change. You just need to account for the new width and height of your objects, especially for things like bounding box collision. You can also just make a variable which holds the ratio, and sets all your objects size based on that ratio to account for any changes. You'll only run into problems with this if you're not using even ratios when scaling because image width and height will not scale evenly.

I hope this helps, if you have more questions let me know.

Share this post


Link to post
Share on other sites
10 minutes ago, RidiculousName said:

Thank you!

I am still searching for how to get the window height/width while the game's running, but your post helped.

No problem! Like I said I don't know python or Kivy, but are you able to call something like:

currentSize = Window.size  ??

Is your screen width and height dynamic to a device? If you're dealing with just static sizes, you can always keep two variables that store the initial width and height values, and when you do a resolution change, just update it and use those variables to do ratio calculations.

Edited by Rutin

Share this post


Link to post
Share on other sites

I'm sure I can get it with something like that. I just haven't been able to find anything on it.

I might choose to just force the game to have one or two sizes.

Share this post


Link to post
Share on other sites
1 hour ago, RidiculousName said:

I'm sure I can get it with something like that. I just haven't been able to find anything on it.

I might choose to just force the game to have one or two sizes.

This might be a good reference: https://kivy.org/docs/_modules/kivy/core/window.html

If you FIND keyword "size" you get the following:

I didn't look through everything but maybe:

 

def _set_system_size(self, size): self._size = size def _get_system_size(self): if self.softinput_mode == 'resize': return self._size[0], self._size[1] - self.keyboard_height return self._size system_size = AliasProperty( _get_system_size, _set_system_size, bind=('_size', )) '''Real size of the window ignoring rotation. .. versionadded:: 1.0.9 :attr:`system_size` is an :class:`~kivy.properties.AliasProperty`. ''' def _get_effective_size(self): '''On density=1 and non-ios displays, return system_size, else return scaled / rotated size. Used by MouseMotionEvent.update_graphics() and WindowBase.on_motion(). ''' w, h = self.system_size if platform == 'ios' or self._density != 1: w, h = self.size return w, h

I'm guessing your answer will be found in the above link.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement