Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand Pymunk
#1
Here I have some code:

import pygame
import pymunk
import sys

pygame.init()

screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
space = pymunk.Space()

class Player():
	def __init__(self, x, y, w, h):
		self.x = x
		self.y = y
		self.w = w
		self.h = h
		self.body = pymunk.Body(body_type = pymunk.Body.KINEMATIC)
		self.body.position = x, y
		self.shape = pymunk.Poly.create_box(self.body, (w, h))
		self.shape.elasticity = 1
		space.add(self.body, self.shape)

	def draw(self):
		pygame.draw.rect(screen, (100,200,255), (self.x, self.y, self.w, self.h),0, 10)

	def moveUP(self, up_down):
		if up_down:
			self.body.velocity = 0, -600

		if not up_down:
			self.body.velocity = 0, 600

	def moveDOWN(self, left_right):
		if left_right:
			self.body.velocity = -600, 0

		if not left_right:
			self.body.velocity = 600, 0

	def stop(self):
		self.body.velocity = 0, 0

player = Player(100,200,50,50)

while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()

	player.draw()

	k = pygame.key.get_pressed()

	if k[pygame.K_w]:
		player.moveUP(True)
	elif k[pygame.K_s]:
		player.moveUP(False)
	else:
		player.stop()

	pygame.display.flip()
	screen.fill((0,0,0))
	clock.tick(60)
	space.step(1/60)
After setting the velocity to -600 in the moveUP function at Player Class, shouldn't the player move? Am I doing something wrong??
Reply
#2
Your problem is that you don't use the body.x and body.y information in Player.draw(). In Player.draw() you should use self.body.x instead of self.x and self.body.y instead of self.y.

I would make Player a subclass of Pymunk.body. Then I could use self.x because it would be pymunk.Body.x. Note that this is untested code.
import pygame
import pymunk
import sys
 
pygame.init()
 
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
space = pymunk.Space()
 
class Player(pyumunk.Body):
    def __init__(self, x, y, w, h):
        super().__init__(body_type = pymunk.Body.KINEMATIC)
        self.position = x, y
        self.w = w
        self.h = h
        self.shape = pymunk.Poly.create_box(self, (w, h))
        self.shape.elasticity = 1
        space.add(self, self.shape)
 
    def draw(self):
        pygame.draw.rect(screen, (100,200,255), (self.x, self.y, self.w, self.h),0, 10)
 
    def moveVertical(self, vel):
        self.velocity = (self.velocity[0], vel)
 
    def moveHorizontal(self, vel):
        self.velocity = (vel, self.velocity[1])
 
    def stop(self):
        self.velocity = (0, 0)
 
player = Player(100, 200, 50, 50)
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
 
    k = pygame.key.get_pressed()
    if k[pygame.K_w]:
        player.vertical(-600)
    elif k[pygame.K_s]:
        player.vertical(600)
    else:
        player.stop()
 
    screen.fill((0,0,0))   # Changed the order of fill/draw/flip.  Seems more logical to me.
    player.draw()
    pygame.display.flip()

    clock.tick(60)
    space.step(1/60)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020