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


Messages In This Thread
Trying to understand Pymunk - by Prithak - Sep-13-2021, 11:59 AM
RE: Trying to understand Pymunk - by deanhystad - Sep-13-2021, 04:53 PM

Forum Jump:

User Panel Messages

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