[PyGame] Having 4 players(Sprites) all being able to jump - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] Having 4 players(Sprites) all being able to jump (/thread-17980.html) |
Having 4 players(Sprites) all being able to jump - ElijahCastle - May-02-2019 I'm new to programming and I'm barely starting. I'm trying to make a 4 player local Co-op fighting game to play with my friends and family so we could all play on the same keyboard. I got the first player to jump but when i put the code to jump for the 2nd player it doesn't work so I'm stuck. I haven't put the jump code for the 3rd and 4th player yet and my characters are squares right now not 2d sprites yet. I have assigned all different keys to each player. I'm also having a problem of the first player jumping right when i start the game up. Please help My Code is underneath this! import pygame pygame.init() screen = pygame.display.set_mode((1250, 650)) pygame.display.set_caption("Close to Death") screenwidth = 1000 x = 50 X = 50 y = 450 Y = 450 width = 50 Wide = 50 Height = 50 Tall = 50 vel = 10 Vel = 10 xx = 50 yy = 450 wider = 50 ht = 50 v = 10 XX = 50 YY = 450 HT = 50 WH = 50 V = 10 IsJump = True isJump = True jumpCount = 6 JumpCounts = 6 run = True while run: pygame.time.delay(50) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() screen.fill((0, 0, 0)) pygame.draw.rect(screen, (255, 0, 0,), (x, y, width, Height)) pygame.draw.rect(screen, (0, 255, 0,), (X, Y, Wide, Tall)) pygame.draw.rect(screen, (0, 0, 255,), (xx, yy, wider, ht)) pygame.draw.rect(screen, (0, 255, 255,), (XX, YY, WH, HT)) pygame.display.update() if keys[pygame.K_LEFT] and x > vel: x -= vel if keys[pygame.K_RIGHT] and x < 1249 - width - vel: x += vel if not isJump: if keys[pygame.K_UP] and y > vel: y -= vel if keys[pygame.K_DOWN] and y < 598 - vel: y += vel if keys[pygame.K_PAGEDOWN]: isJump = True else: if jumpCount >= -6: neg = +1 if jumpCount < 0: neg = - 1 y -= (jumpCount ** 2) * 1 * neg jumpCount -= 1 else: isJump = False jumpCount = 6 if keys[pygame.K_a] and X > Vel: X -= Vel if keys[pygame.K_d] and X < 1249 - Wide - Vel: X += Vel if not IsJump: if keys[pygame.K_w] and Y > Vel: Y -= Vel if keys[pygame.K_s] and Y < 598 - Vel: Y += Vel if keys[pygame.K_q]: IsJump = True else: if JumpCounts >= -6: Negative = +1 if JumpCounts < 0: Negative = - -1 Y -= (JumpCounts ** 2) * 1 * Negative JumpCounts -= 1 else: IsJump = False JumpCounts = 6 if keys[pygame.K_h] and xx > v: xx -= v if keys[pygame.K_k] and xx < 1240 - wider - v: xx += v if keys[pygame.K_u] and yy > v: yy -= v if keys[pygame.K_j] and yy < 455 - v: yy += v if keys[pygame.K_KP5] and XX > V: XX -= V if keys[pygame.K_KP_PLUS] and XX < 1240 - WH - V: XX += V if keys[pygame.K_KP9] and YY > V: YY -= V if keys[pygame.K_KP6] and YY < 455 - V: YY += V RE: Having 4 players(Sprites) all being able to jump - Windspar - May-02-2019 Always use code tags in forum. 1. Learn some basic python. Learn class, list, and dict. 2. Then learn pygame. Learn basic pygame.time.Clock, pygame.Rect, and pygame.Vector2. For images learn pygame.Surface, pygame.sprite.Sprite, and pygame.sprite.Group. RE: Having 4 players(Sprites) all being able to jump - SheeppOSU - May-02-2019 I definitely agree, no offense but this is very sloppy. Here is a basic example of what your game should look like - #init, variables, imports, etc. class Player(): def __init__(self, x, y, other paramters): self.x = x self.y = y #defining more selfs def draw(): #draw code def move(self): keyed = pygame.keys.get_pressed() if keyed[K_LEFT]: self.x -= 2 #etc. def main(): p1 = Player(0, 0, more parameters) p2 = Player(width, height, more parameters) p3 = Player(width, 0, more parameters) p4 = Player(0, heigth, more parameters) p1.draw() p2.draw() p3.draw() p4.draw() p1.move() p2.move() p3.move() p4.move() main() BTW once it is finished, you can learn socket and make this playable with friends and family from different clients/computers RE: Having 4 players(Sprites) all being able to jump - Windspar - May-02-2019 Here an example. import pygame # This is my simple boiler plate. That I use for examples. # Simple Scene Interface class Scene: def draw(self, surface, game): pass def event(self, event, game): pass def update(self, game): pass class Game: def __init__(self, caption, width, height): # Basic pygame setup pygame.display.set_caption(caption) self.rect = pygame.Rect(0, 0, width, height) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() self.running = False self.fps = 30 self.delta = 0 # Scene Interface self.scene = Scene() Game.info = self def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): self.scene.event(event, self) self.keys = pygame.key.get_pressed() self.scene.update(self) self.scene.draw(self.surface, self) pygame.display.flip() self.delta = self.clock.tick(self.fps) class Player(pygame.sprite.Sprite): def __init__(self, image): pygame.sprite.Sprite.__init__(self) # Variables form sprite class. self.image = image self.rect = image.get_rect() # custom variables self.moves = {} self.speed = 2 def update(self): keys = Game.info.keys if keys[self.moves["Left"]]: self.rect.x -= self.speed elif keys[self.moves["Right"]]: self.rect.x += self.speed elif keys[self.moves["Up"]]: self.rect.y -= self.speed elif keys[self.moves["Down"]]: self.rect.y += self.speed class PlayerBase: def __init__(self): self.sprites = pygame.sprite.Group() self.players = 0 self.moves = [ {"Left":pygame.K_LEFT, "Right":pygame.K_RIGHT, "Up":pygame.K_UP, "Down":pygame.K_DOWN}, {"Left":pygame.K_a, "Right":pygame.K_d, "Up":pygame.K_w, "Down":pygame.K_s}, ] def __call__(self, color, position): surface = pygame.Surface((20, 20)) surface.fill(color) player = Player(surface) player.rect.topleft = position player.moves = self.moves[self.players] player.add(self.sprites) self.players += 1 class FightScene(Scene): def __init__(self): self.players = PlayerBase() self.players(pygame.Color("Blue3"), (20, 20)) self.players(pygame.Color("Green3"), (300, 20)) def draw(self, surface, game): surface.fill(pygame.Color("Black")) self.players.sprites.update() self.players.sprites.draw(surface) def event(self, event, game): if event.type == pygame.QUIT: game.running = False def main(): pygame.init() game = Game("Example", 340, 100) game.scene = FightScene() game.mainloop() pygame.quit() main() RE: Having 4 players(Sprites) all being able to jump - ElijahCastle - May-07-2019 Thank You guys for Responding. But Can you guys type the code for jumping and make it assigned to all 4 players. like that's the problem I'm having trouble with the most. Can you guys put the keys i assigned my character's with and the jump code i need to put with them so they'll all jump but with the keys i showed in my code. Can you show me the actual code i need to put ? RE: Having 4 players(Sprites) all being able to jump - SheeppOSU - May-07-2019 You can use treading and have a jump and move function. I won't give you the actual code, but execute the move and jump. The jump consists of going up and then down. Use time.sleep to make the jump realistic. The threading is needed so the sleep won't affect the main. Used with the move function they can move and jump at the same time. If you want them to not move while jumping, just don't thread it. |