Python PyMunk acts weirdly - python

here is the problem :
My pymunk code is from https://pymunk-tutorial.readthedocs.io/en/latest/shape/shape.html
But when i launch the program, a weird force acts on the segment.
See the result : https://youtu.be/s4a0RLb6Y6k
See the code : (without useless part about dependencies)
import pymunk
import pymunk.pygame_util
import pygame
from pygame.locals import *
pygame.init()
width, height = 1280, 720
window = pygame.display.set_mode((width, height), FULLSCREEN)
draw_options = pymunk.pygame_util.DrawOptions(window)
window.fill((0,0,0))
run = True
space = pymunk.Space()
space.gravity = 0, -1000
b0 = space.static_body
sol = pymunk.Segment(b0, (0, 30), (width, 30), 10)
sol.elasticity = 1
body = pymunk.Body(mass=1, moment=1000)
body.position = (width/2, 50)
segment = pymunk.Segment(body, (width/2-20,50), (width/2+20,50), 1)
segment.elasticity = 0.95
space.add(sol, body, segment)
while run == True:
space.step(0.01)
window.fill((0,0,0))
pygame.draw.line(window, (0, 255, 0), (0, height-30), (width, height-30), 1)
space.debug_draw(draw_options)
pygame.display.flip()
pygame.time.Clock().tick(60)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
if event.type == QUIT:
run = False
mouse_x, mouse_y = pygame.mouse.get_pos()
pass
continue
I don't see where is the problem..
Thanks by advance

One common reason why strange behavior like that in the video happens is because center of gravity of the shape is not in the actual center. Try changing so that you make the Segment around the its center (0,0), where the body is.
Something like this maybe
segment = pymunk.Segment(body, (-20,0), (20,0), 1)
If you want to move it you can adjust the position of the body instead.

So the a an b values are relative from the center of mass of the body precited.
(with a and b from shape = pymunk.Segment(body, a, b, thickness) )
Thanks very much !

Related

Modify alpha of Surface pixels directly in pygame

I have a Surface in PyGame. I want to modify the alpha values of pixels directly. I've tried doing it with the various methods that access the alpha values, but they don't seem to work.
from screeninfo import get_monitors
import pygame, os, numpy.random, pygame.surfarray
pygame.init()
FPS = 60
CLOCK = pygame.time.Clock()
monitor_info = get_monitors()
x = 0
y = 0
width = monitor_info[0].width
height = monitor_info[0].height
if len(monitor_info) > 1:
if monitor_info[0].x < 0:
x = 0
else:
x = monitor_info[0].width
width = monitor_info[1].width
height = monitor_info[1].height
os.environ['SDL_VIDEO_WINDOW_POS'] = "{},0".format(x)
pygame.display.init()
pygame.mouse.set_visible(False)
screen_info = pygame.display.Info()
screen_size = width, height
base_screen = pygame.display.set_mode(screen_size, pygame.NOFRAME)
base_screen.fill([100, 100, 100])
board_size = (int(min(width, height)*0.75), int(min(width, height)*0.75))
surface = pygame.Surface(board_size, pygame.SRCALPHA)
surface.fill([255, 255, 255, 255])
base_screen.blit(surface, (0,0))
pygame.display.flip()
pixels = numpy.random.uniform(low=0, high=255, size=(board_size[0], board_size[1], 3))
transparency = numpy.random.uniform(low=0, high=255, size=board_size).astype('uint8')
while True:
events = pygame.event.get()
ms = CLOCK.tick(FPS)
print('\r {} '.format(ms), end='')
# pygame.surfarray.blit_array(surface, pixels)
aa = pygame.surfarray.pixels_alpha(surface)
aa = numpy.random.uniform(low=0, high=255, size=board_size).astype('uint8')
del aa
# for i in range(board_size[0]):
# for j in range(board_size[1]):
# a = surface.get_at((i,j))
# a[3] = 0
# surface.set_at((i,j), a)
base_screen.blit(surface, (0,0))
pygame.display.flip()
I've tried both things in the loop (pixels_array, and get_at/set_at) but neither works--the image just stays white (if I set the initial alpha to 0, it stays transparent). Does anyone know how to set per-pixel alpha values for a Surface?
I found your problem!! The reason why you can't see alpha is:
a) you first set surface alpha to 255, surface.fill([255, 255, 255, 255])
b) I believe aa = pygame.surfarray.pixels_alpha(surface) aa = numpy.random.uniform(low=0, high=255, size=board_size).astype('uint8') aren't working, however pygame.surfarray.blit_array(surface, pixels) do work (produce colours) but I don't think they have any actual Alpha.
c) you need to fill the base_screen and THEN blit you surface. Such a common mistake but this is the main the problem.
And finally, Tim Robert's comment about the for loop, will definitely get you your alpha!
Here is it re-written to work (without screeninfo as I don't have that library currently):
import pygame, os, numpy.random, pygame.surfarray
from random import randint
pygame.init()
FPS = 60
CLOCK = pygame.time.Clock()
x = 50
y = 50
width = 500
height = 500
os.environ['SDL_VIDEO_WINDOW_POS'] = "{},0".format(x)
#pygame.display.init(), don't actually need this
pygame.mouse.set_visible(False)
screen_info = pygame.display.Info()
screen_size = width, height
base_screen = pygame.display.set_mode(screen_size, pygame.NOFRAME)
base_screen.fill([100, 100, 100])
board_size = (int(min(width, height)*0.75), int(min(width, height)*0.75))
surface = pygame.Surface(board_size, pygame.SRCALPHA)
surface.fill([255, 0, 0]) ###could also be surface.fill([255,0,0,255]) to set the whole surface's alpha straight up if you didn't want to change each pixel later
while True:
#just so you can quit this program
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
raise SystemExit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
raise SystemExit()
ms = CLOCK.tick(FPS)
for i in range(board_size[0]):
for j in range(board_size[1]):
a = surface.get_at((i,j))
a[3] = randint(0, 128)#OR SET TO REQUIRED ALPHA VALUE
surface.set_at((i,j), a)
##################################################
base_screen.fill([100, 100, 100]) #NEED TO DO THIS
##################################################
base_screen.blit(surface, (0,0))
pygame.display.flip()
(by the way, I used red as the second surface colour as I thought it would stand out better)
Edit
As Eternal Ambiguity stated in the comments, here is the much, much faster version, in place of the for loops:
aa = pygame.surfarray.pixels_alpha(surface)
aa[:] = numpy.random.uniform(low=0, high=255, size=board_size).astype('uint8')
del aa
pygame.Surface.blit() does not replace the pixels in the target surface with the source surface. It mixes (blends) the pixels of the surfaces. Actually you are blending the new surface with the old previous surface every frame. This means that the area of the surface appears uniformly colored within milliseconds. You have to clear the screen in every frame:
while True:
# [...]
base_screen.fill((0, 0, 0))
base_screen.blit(surface, (0,0))
pygame.display.flip()
Minimal example:
import pygame
pygame.init()
width, height = 200, 200
base_screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
board_size = (int(min(width, height)*0.75), int(min(width, height)*0.75))
surface = pygame.Surface(board_size, pygame.SRCALPHA)
surface.fill([255, 0, 0])
for x in range(board_size[0]):
for y in range(board_size[1]):
a = surface.get_at((x, y))
a[3] = round(y / board_size[1] * 255)
surface.set_at((x, y), a)
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
base_screen.fill((100, 100, 100))
base_screen.blit(surface, surface.get_rect(center = base_screen.get_rect().center))
pygame.display.flip()

Is there a way to get pygame to detect what color my cursor is on

I'm trying to make a game where it spawns another circle every time you click on a circle. And the error i'm getting is "TypeError: 'bool' object is not callable". I'm looking for a solution that doesn't completly change the code since i'm new and want to understand the code myself. But at this point i'll take any help.
import pygame
import random
import time
from pygame.math import Vector2
# Define some colors
BLACK = (0, 0, 0)
WHITE = (247, 247, 247)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (225,225,0)
tuple1 = (247, 247, 247, 255)
# Setup
pygame.init()
# Set the width and height of the screen [width,height]
surface = pygame.display.set_mode( (2560, 1440) )
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Hide the mouse cursor
pygame.mouse.set_visible(0)
# Speed in pixels per frame
x_speed = 0
y_speed = 0
# Current position
cursor = pygame.image.load('cursor.png').convert_alpha()
pygame.image.load("pattern.jpg")
background_image = pygame.image.load("pattern.jpg").convert()
circposy = 770
circposx = 1280
# -------- Main Program Loop -----------
while done ==False:
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
done = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Drawing Code
surface.fill(WHITE)
# First, clear the screen to WHITE. Don't put other drawing commands
# above this, or they will be erased with this command.\
player_position = pygame.mouse.get_pos()
a = 0
b = 1
p=player_position[a]
o=player_position[b]
player_position = (p,o)
pygame.draw.circle(surface,RED,[circposx,circposy], 40)
tuple2 = surface.get_at(pygame.mouse.get_pos())
print (tuple2)
q = p - 2545
w = o - 2545
surface.blit( cursor, (q, w) )
a=0
result = tuple(map(int, tuple2)) > tuple1
print (result)
while event.type == pygame.MOUSEBUTTONDOWN:
done = True
if result == True():
a+1
surface.fill(WHITE)
pygame.draw.circle(surface,RED,[circposx + randint, circposy+randint],40)
print (a)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit frames per second
clock.tick(144)
# Close the window and quit.
pygame.quit()
cursor.png
The short answer is that the code is trying to call True(), which isn't a function.
while event.type == pygame.MOUSEBUTTONDOWN:
done = True
if result == True(): # <<-- HERE
a+1
surface.fill(WHITE)
pygame.draw.circle(surface,RED,[circposx + randint, circposy+randint],40)
Simply change this to True.
But you will also need to define what randint is a few lines lower. Did you mean random.randint( 0, 500 ) or suchlike? And fixing this leads to another error, because the surrounding loop to this code, is an infinite loop:
while event.type == pygame.MOUSEBUTTONDOWN: # <<-- HERE
done = True
if result == True:
a+1
surface.fill(WHITE)
rand_x = random.randint( 0, 500 )
rand_y = random.randint( 0, 500 )
pygame.draw.circle(surface,RED,[circposx + rand_x, circposy+rand_y],40)
Because there is no way event.type can ever change inside that loop. This should probably read:
if event.type == pygame.MOUSEBUTTONDOWN:
If I may make some suggestions:
Put all your event handling to a single place.
There's some doubling-up of event handling, and it would have prevented that infinite loop.
Move your screen dimensions into variables
SCREEN_WIDTH = 2560
SCREEN_HEIGHT = 1440
Instead of having constant numbers through the code (e.g.: 2545) make these functions of the screen size.
SCREEN_MARGIN = SCREEN_WIDTH - round( SCREEN_WIDTH * 0.10 )
q = p - SCREEN_MARGIN
If you want to detect what color your cursor is on, you can use pyautogui. Make sure you have pyautogui installed. type pip install pyautogui. If it doesn't install successfully, you already have it installed.
# Import everything
import pygame
import pyautogui
from pyautogui import *
# Initialize
pygame.init()
# Get mouse position
mouse_pos = pygame.mouse.get_pos()
x = mouse_pos[0]
y = mouse_pos[1]
# Get Color
r = pyautogui.pixel(x,y)[0]
g = pyautogui.pixel(x,y)[1]
b = pyautogui.pixel(x,y)[2]
color = [r,g,b]
Hopefully, you found this helpful!

change of impulse direction Pymunk (python)

Hi i would like to create a small drone simulator. Two engines on the left and right. I was based on examples from the pymunk library. But I have no idea how to add engine power to make it hit the point, i.e. make the object rotate. This can be done easily with the help of this library? Thank you for any help
import sys,math
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
from pymunk.vec2d import Vec2d
import pymunk.pygame_util
width, height = 690,400
fps = 60
dt = 1./fps
JUMP_HEIGHT = 16.*2
def main():
### PyGame init
pygame.init()
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
running = True
### Physics stuff
space = pymunk.Space()
space.gravity = 0,-1000
draw_options = pymunk.pygame_util.DrawOptions(screen)
# box walls
static = [ pymunk.Segment(space.static_body, (10, 50), (680, 50), 3)
, pymunk.Segment(space.static_body, (680, 50), (680, 370), 3)
, pymunk.Segment(space.static_body, (680, 370), (10, 370), 3)
, pymunk.Segment(space.static_body, (10, 370), (10, 50), 3)
]
for s in static:
s.friction = 1.
s.group = 1
space.add(static)
# player
body = pymunk.Body(5, pymunk.inf)
body.position = 100,100
left = pymunk.Poly(body, [(-30,25), (0, 25), (0, 0), (-30, 0)] )
right = pymunk.Poly(body, [(0,0), (0, 25), (30, 25), (30, 0)] )
left.color = 46,25,1,46
right.color = 1,25,1,25
space.add(body, left, right)
while running:
for event in pygame.event.get():
if event.type == QUIT or \
event.type == KEYDOWN and (event.key in [K_ESCAPE, K_q]):
running = False
elif event.type == KEYDOWN and event.key == K_LEFT:
# here is the problem
jump_v = math.sqrt(2.0 * JUMP_HEIGHT * abs(space.gravity.y))
power = max(min(jump_v, 1000), 10) * 1.5
impulse = power * Vec2d(0, 3)
impulse.rotate(body.angle - 45)
body.apply_impulse_at_world_point(impulse, (0,0) )
elif event.type == KEYDOWN and event.key == K_RIGHT:
# here is the problem
jump_v = math.sqrt(2.0 * JUMP_HEIGHT * abs(space.gravity.y))
power = max(min(jump_v, 1000), 10) * 1.5
impulse = power * Vec2d(0, 3)
impulse.rotate(body.angle + 45)
body.apply_impulse_at_world_point(impulse, (0,0) )
### Clear screen
screen.fill(pygame.color.THECOLORS["black"])
### Helper lines
for y in [50,100,150,200,250,300]:
color = pygame.color.THECOLORS['darkgrey']
pygame.draw.line(screen, color, (10,y), (680,y), 1)
# ### Draw stuff
space.debug_draw(draw_options)
pygame.display.flip()
### Update physics
space.step(dt)
clock.tick(fps)
if __name__ == '__main__':
sys.exit(main())
Your main issue is that your Body has infinite moment, which means that it can't rotate. Try creating the Body as something like:
body = pymunk.Body(5, 500)
You also need to apply the impulse somewhere that makes sense. Try:
body.apply_impulse_at_local_point(impulse, (5, 0))
and:
body.apply_impulse_at_local_point(impulse, (-5, 0))
for left and right.

[Pygame]: Move surface towards position

Is there an algorithm to change a set of coordinates to make it move towards another set of coordinates?
Like, if I have ax,ay=(20,30) a
#fonctions
import pygame, sys
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
windows= pygame.display.set_mode((1200,500), 0, 32)
pygame.display.set_caption('testing')
size = width, height = (32, 32)
PNGImg = pygame.Surface(size)
sob_X=575
sob_Y=250
FPS = 15 # frames per second setting
RED= (255, 0, 0) #red color
while True:
windows.fill((54, 141, 197))
pygame.draw.circle(windows, RED, (70, 80), 20, 0)
windows.blit(PNGImg, (sob_X,sob_Y))
dis_X=sob_X-600 #cicle X_Cordinate
dis_Y=sob_X-870 #cicle Y_Cordinate
if dis_X<0:
sob_X=sob_X+ dis_X
if sob_X==70 and dis_Y<0:
sob_Y=sob_X+dis_Y
elif sob_X==70 and dis_Y>0:
sob_Y=sob_Y-dis_Y
elif dis_X>0:
sob_X=sob_X -dis_X
if sob_X==70 and dis_Y<0:
sob_Y=sob_Y+dis_Y
elif sob_X==70 and dis_Y>0:
sob_Y=sob_Y-dis_Y
windows.blit(PNGImg, (sob_X,sob_Y))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
nd bx,by=(40,60)
How can I change ax and ay (coordinate of the image"surface") to equal bx and by? (Preferably an algorithm achievable in python.)
i tried to make algorithme but in the end don't work
thanks you
Replying here as I can't comment yet, but from the question itself, do you mean to do ax, ay = bx, by ? You can simply do that, or even individual ones like ax = by.
With an example from the terminal:
>>> ax,ay=(20,30)
>>> bx,by=(40,60)
>>> ax, ay = bx, by
>>> ax, ay
(40, 60)

How To Find Out If A .Rect() Has Been Clicked In Pygame

I'm trying to find out if a user has clicked on a .rect(). I've read a bunch of tutorials, but I'm still running into problems. I have two files: One that is the main python file, and one that defines the .rect().
#main.py
import pygame,os,TextBox
from pygame.locals import *
pygame.init()
myTextBox = TextBox.TextBox()
WHITE = (255, 255, 255)
size = (400, 200)
screen = pygame.display.set_mode(size)
done = False
boxes = [myTextBox]
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True
elif event.type == MOUSEMOTION: x,y = event.pos
for box in boxes:
if myTextBox.rect.collidepoint(x,y): print ('yay')
screen.fill(WHITE)
myTextBox.display(screen, 150, 150, 20, 100)
pygame.display.flip()
pygame.quit()
#TextBox.py
import pygame
from pygame.locals import *
class TextBox:
def __init__(self):
self.position_x = 100
self.position_y = 100
self.size_height = 10
self.size_width = 50
self.outline_color = ( 0, 0, 0)
self.inner_color = (255, 255, 255)
def display(self, screen, x, y, height, width):
self.position_x = x
self.position_y = y
self.size_height = height
self.size_width = width
pygame.draw.rect(screen, self.outline_color, Rect((self.position_x - 1, self.position_y - 1, self.size_width + 2, self.size_height + 2)))
pygame.draw.rect(screen, self.inner_color, Rect((self.position_x, self.position_y, self.size_width, self.size_height)))
The error I get is AttributeError: 'TextBox' object has no attribute 'rect'
How do I solve this?
You're TextBox class doesn't have a rect. Add this somewhere at the bottom of the __init__ method of the TextBox class:
self.rect = pygame.Rect(self.position_x,self.position_y,self.size_width,self.size_height)
Do the same in the update method.

Categories