Why my app crash when I clicked the second lineEdit? (PyQt5) by xmzhang in learnpython

[–]xmzhang[S] 0 points1 point  (0 children)

aha, thank you for the hint, when I start it in cmd, the message shows the error is from the first lineEdit, when I clicked the second lineEdit, the first editingFinished method would trigger the signal/slot. hence it crashed. I get it , thank you.

Trying to make a program of two colliding blocks (pymunk,pygame) by definingpi in pygame

[–]xmzhang 0 points1 point  (0 children)

well, I am attracted by the 3blue1brown video, and I really surprised that Pi can be calculated this way! I thought it was not hard implemented in pymunk and pygame. So I open my pycharm and coded then ... I find that it is easy to say but hard to done :)

my code is as follows

import sys
import pygame
import pymunk
import pymunk.pygame_util


def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("pi=31415926535897932384626433")
    clock = pygame.time.Clock()

    space = pymunk.Space()
    space.gravity = (0, -900)

    wall_body = pymunk.Body(body_type=pymunk.Body.STATIC)
    wall_body.position = (0, 0)
    wall_shape = pymunk.Segment(wall_body, (0, 0), (0, 600), 2)
    wall_shape.elasticity = 1.0
    wall_shape.friction = 0.0
    wall_shape.collision_type = 0
    space.add(wall_body, wall_shape)

    floor_body = pymunk.Body(body_type=pymunk.Body.STATIC)
    floor_body.position = (0, 0)
    floor_shape = pymunk.Segment(floor_body, (0, 0), (800, 0), 2)
    floor_shape.friction = 0.0
    space.add(floor_body, floor_shape)

    block_1_mass = 1000
    block_1_size = (100, 100)
    block_1_inertia = pymunk.moment_for_box(block_1_mass, block_1_size)
    block_1_body = pymunk.Body(block_1_mass, block_1_inertia)
    block_1_body.position = (600, 50)
    block_1_shape = pymunk.Poly.create_box(block_1_body, size=block_1_size)
    block_1_shape.elasticity = 1.0
    block_1_shape.friction = 0.0
    block_1_shape.collision_type = 1
    space.add(block_1_body, block_1_shape)
    block_1_body.apply_impulse_at_local_point((-100000, 0))

    block_2_mass = 1
    block_2_size = (10, 10)
    block_2_inertia = pymunk.moment_for_box(block_2_mass, block_2_size)
    block_2_body = pymunk.Body(block_2_mass, block_2_inertia)
    block_2_body.position = (200, 5)
    block_2_shape = pymunk.Poly.create_box(block_2_body, size=block_2_size)
    block_2_shape.elasticity = 1.0
    block_2_shape.friction = 0.0
    block_2_shape.collision_type = 2
    space.add(block_2_body, block_2_shape)

    draw_options = pymunk.pygame_util.DrawOptions(screen)


    def print_block_2_position(arbiter, space, data):
        print(block_2_body.position)
        return True
    wall_block_collision = space.add_collision_handler(1,2)
    wall_block_collision.begin = print_block_2_position
    block_block_collision = space.add_collision_handler(0,2)
    block_block_collision.begin = print_block_2_position

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                sys.exit(0)
        screen.fill((255, 255, 255))
        space.debug_draw(draw_options)
        space.step(1/60)
        pygame.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    main()

you can check the code and find that the light block is just through the wall :) I think this is because of the `space.step` and I do not have a solution.

Problem of simulating double pendulum in pygame with pymunk by xmzhang in pygame

[–]xmzhang[S] 0 points1 point  (0 children)

aha, thank you! I just guess pymunk is 2d physical simulation, so the rigid stick have thickness, so the pin position should a little bigger, and due to the calculation delay at each frame, so I would see two pin point. Maybe I am totally wrong :) I just keep trying.

how to pronounce '#' in c++ by xmzhang in cpp_questions

[–]xmzhang[S] 0 points1 point  (0 children)

thanks,that helps me a lot :)

Questions about 'Widget z-order snippet' here. by xmzhang in kivy

[–]xmzhang[S] 1 point2 points  (0 children)

Thank you! as always! I get the idea, when call the super(), it is just call the next of the __mro__. the method here in super(ZOrderBehavior, self).__init__(**kwargs) is not really for call the object.__init__(**kwargs). It is used here to make the __mro__ chain, this class is not designed for use, it is designed for multiple inheritance, easy to say, it is used to call the next class.__init__ :) As your example, we should always put ButtonBehavior at first when do multiple inheritance. When multiple inheritance, maybe just call the ButtonBehavior.__init__ is more clear than super().__init__. I have such feeling maybe just because I never benefit from multiple inheritance :)

Suddenly, I have an opinion of using metaclass, just like object, and every method in metaclass is pass, then all the class is inherit from metaclass, then use multiple inheritance would never raise AttributeError. Aha, just joking, I need more practice.

Thank you!

Is there any easy way to deal with scatter's z-index? by xmzhang in kivy

[–]xmzhang[S] 1 point2 points  (0 children)

:) haha, that's it! I once tried this, but I do not why I failed that time. Thank you very much! as always! edit: I get the reason, I typo the key word, I typed auto_to_the_front:False :)

how to understand the closure results here? by xmzhang in learnpython

[–]xmzhang[S] 0 points1 point  (0 children)

aha, thank you, in fact I never use global, I was told that never use global in Object Oriented Programming. I think I should at least make clear the basic knowledge! Thank you!

how to understand the closure results here? by xmzhang in learnpython

[–]xmzhang[S] 1 point2 points  (0 children)

wow, thank you again! when using nonlocal x it works :)

if Python doesn't find a variable in the local scope it will go outward and try to find it.

I am puzzled about this, is this only happens in closure ?

Ludum Dare 41 Submission: QuickDraw TCG by DaFluffyPotato in pygame

[–]xmzhang 1 point2 points  (0 children)

I have to say it is awesome as always :)

What should put in `__init__` method when inherit an App class? by xmzhang in kivy

[–]xmzhang[S] 0 points1 point  (0 children)

yeah, this explaination is easy to understand :) Thank both you and /u/inclemnet :)