Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Programming Languages
Python
Assistance with adding a second player to my Pygame :(
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Alice Smith" data-source="post: 5167888" data-attributes="member: 86051"><p>I am fairly new to Pygame, just started studying it this yezr infact and I was wondering if I could have some assistance with adding a second player to my Pygame program? I have tried a bunch of different things but can't seem to understand how to do it. </p><p>The code displayed below is what I am using for my player Sprite:</p><p>import pygame as pg</p><p>from settings import *</p><p>from random import choice, randrange, uniform</p><p>vec = pg.math.Vector2</p><p></p><p>class Spritesheet:</p><p> def __init__(self, filename):</p><p> self.spritesheet = pg.image.load(filename).convert()</p><p></p><p> def get_image(self, x, y, width, height):</p><p> image = pg.Surface((width, height))</p><p> image.blit(self.spritesheet, (0, 0), (x, y, width, height))</p><p> image = pg.transform.scale(image, (width // 2, height // 2))</p><p> return image</p><p>#This class is used fot loading and parsing the spritesheet used for this code</p><p>#It also grabs desired images from the Spritesheet</p><p></p><p>class Player(pg.sprite.Sprite):</p><p> def __init__(self, game):</p><p> self._layer = PLAYER_LAYER</p><p> self.groups = game.all_sprites</p><p> pg.sprite.Sprite.__init__(self, self.groups)</p><p> self.game = game</p><p> self.walking = False</p><p> self.jumping = False</p><p> self.current_frame = 0</p><p> self.last_update = 0</p><p> self.load_images()</p><p> self.image = self.standing_frames[0]</p><p> self.rect = self.image.get_rect()</p><p> self.rect.center = (40, HEIGHT - 100)</p><p> self.pos = vec(40, HEIGHT - 100)</p><p> self.vel = vec(0, 0)</p><p> self.acc = vec(0, 0)</p><p> </p><p> def load_images(self):</p><p> self.standing_frames = [self.game.spritesheet.get_image(614, 1063, 120, 191),</p><p> self.game.spritesheet.get_image(690, 406, 120, 201)]</p><p> for frame in self.standing_frames:</p><p> frame.set_colorkey(BLACK)</p><p> self.walk_frames_r = [self.game.spritesheet.get_image(678, 860, 120, 201),</p><p> self.game.spritesheet.get_image(692, 1458, 120, 207)]</p><p> self.walk_frames_l = []</p><p> for frame in self.walk_frames_r:</p><p> frame.set_colorkey(BLACK)</p><p> self.walk_frames_l.append(pg.transform.flip(frame, True, False))</p><p> self.jump_frame = self.game.spritesheet.get_image(382, 763, 150, 181)</p><p> self.jump_frame.set_colorkey(BLACK)</p><p></p><p> def jump_cut(self):</p><p> if self.jumping:</p><p> if self.vel.y < -3:</p><p> self.vel.y = -3</p><p></p><p> def jump(self):</p><p> # jump only if standing on a platform</p><p> self.rect.y += 2</p><p> hits = pg.sprite.spritecollide(self, self.game.platforms, False)</p><p> self.rect.y -= 2</p><p> if hits and not self.jumping:</p><p> self.jumping = True</p><p> self.vel.y = -PLAYER_JUMP</p><p></p><p> def update(self):</p><p> self.animate()</p><p> self.acc = vec(0, PLAYER_GRAV)</p><p> keys = pg.key.get_pressed()</p><p> if keys[pg.K_LEFT]:</p><p> self.acc.x = -PLAYER_ACC</p><p> if keys[pg.K_RIGHT]:</p><p> self.acc.x = PLAYER_ACC</p><p></p><p> # apply friction</p><p> self.acc.x += self.vel.x * PLAYER_FRICTION</p><p> # equations of motion</p><p> self.vel += self.acc</p><p> if abs(self.vel.x) < 0.1:</p><p> self.vel.x = 0</p><p> self.pos += self.vel + 0.5 * self.acc</p><p> # wrap around the sides of the screen</p><p> if self.pos.x > WIDTH + self.rect.width / 2:</p><p> self.pos.x = 0 - self.rect.width / 2</p><p> if self.pos.x < 0 - self.rect.width / 2:</p><p> self.pos.x = WIDTH + self.rect.width / 2</p><p></p><p> self.rect.midbottom = self.pos</p><p></p><p> def animate(self):</p><p> now = pg.time.get_ticks()</p><p> if self.vel.x != 0:</p><p> self.walking = True</p><p> else:</p><p> self.walking = False</p><p> # show walk animation</p><p> if self.walking:</p><p> if now - self.last_update > 180:</p><p> self.last_update = now</p><p> self.current_frame = (self.current_frame + 1) % len(self.walk_frames_l)</p><p> bottom = self.rect.bottom</p><p> if self.vel.x > 0:</p><p> self.image = self.walk_frames_r[self.current_frame]</p><p> else:</p><p> self.image = self.walk_frames_l[self.current_frame]</p><p> self.rect = self.image.get_rect()</p><p> self.rect.bottom = bottom</p><p> # show idle animation</p><p> if not self.jumping and not self.walking:</p><p> if now - self.last_update > 350:</p><p> self.last_update = now</p><p> self.current_frame = (self.current_frame + 1) % len(self.standing_frames)</p><p> bottom = self.rect.bottom</p><p> self.image = self.standing_frames[self.current_frame]</p><p> self.rect = self.image.get_rect()</p><p> self.rect.bottom = bottom</p><p> self.mask = pg.mask.from_surface(self.image)</p></blockquote><p></p>
[QUOTE="Alice Smith, post: 5167888, member: 86051"] I am fairly new to Pygame, just started studying it this yezr infact and I was wondering if I could have some assistance with adding a second player to my Pygame program? I have tried a bunch of different things but can't seem to understand how to do it. The code displayed below is what I am using for my player Sprite: import pygame as pg from settings import * from random import choice, randrange, uniform vec = pg.math.Vector2 class Spritesheet: def __init__(self, filename): self.spritesheet = pg.image.load(filename).convert() def get_image(self, x, y, width, height): image = pg.Surface((width, height)) image.blit(self.spritesheet, (0, 0), (x, y, width, height)) image = pg.transform.scale(image, (width // 2, height // 2)) return image #This class is used fot loading and parsing the spritesheet used for this code #It also grabs desired images from the Spritesheet class Player(pg.sprite.Sprite): def __init__(self, game): self._layer = PLAYER_LAYER self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.game = game self.walking = False self.jumping = False self.current_frame = 0 self.last_update = 0 self.load_images() self.image = self.standing_frames[0] self.rect = self.image.get_rect() self.rect.center = (40, HEIGHT - 100) self.pos = vec(40, HEIGHT - 100) self.vel = vec(0, 0) self.acc = vec(0, 0) def load_images(self): self.standing_frames = [self.game.spritesheet.get_image(614, 1063, 120, 191), self.game.spritesheet.get_image(690, 406, 120, 201)] for frame in self.standing_frames: frame.set_colorkey(BLACK) self.walk_frames_r = [self.game.spritesheet.get_image(678, 860, 120, 201), self.game.spritesheet.get_image(692, 1458, 120, 207)] self.walk_frames_l = [] for frame in self.walk_frames_r: frame.set_colorkey(BLACK) self.walk_frames_l.append(pg.transform.flip(frame, True, False)) self.jump_frame = self.game.spritesheet.get_image(382, 763, 150, 181) self.jump_frame.set_colorkey(BLACK) def jump_cut(self): if self.jumping: if self.vel.y < -3: self.vel.y = -3 def jump(self): # jump only if standing on a platform self.rect.y += 2 hits = pg.sprite.spritecollide(self, self.game.platforms, False) self.rect.y -= 2 if hits and not self.jumping: self.jumping = True self.vel.y = -PLAYER_JUMP def update(self): self.animate() self.acc = vec(0, PLAYER_GRAV) keys = pg.key.get_pressed() if keys[pg.K_LEFT]: self.acc.x = -PLAYER_ACC if keys[pg.K_RIGHT]: self.acc.x = PLAYER_ACC # apply friction self.acc.x += self.vel.x * PLAYER_FRICTION # equations of motion self.vel += self.acc if abs(self.vel.x) < 0.1: self.vel.x = 0 self.pos += self.vel + 0.5 * self.acc # wrap around the sides of the screen if self.pos.x > WIDTH + self.rect.width / 2: self.pos.x = 0 - self.rect.width / 2 if self.pos.x < 0 - self.rect.width / 2: self.pos.x = WIDTH + self.rect.width / 2 self.rect.midbottom = self.pos def animate(self): now = pg.time.get_ticks() if self.vel.x != 0: self.walking = True else: self.walking = False # show walk animation if self.walking: if now - self.last_update > 180: self.last_update = now self.current_frame = (self.current_frame + 1) % len(self.walk_frames_l) bottom = self.rect.bottom if self.vel.x > 0: self.image = self.walk_frames_r[self.current_frame] else: self.image = self.walk_frames_l[self.current_frame] self.rect = self.image.get_rect() self.rect.bottom = bottom # show idle animation if not self.jumping and not self.walking: if now - self.last_update > 350: self.last_update = now self.current_frame = (self.current_frame + 1) % len(self.standing_frames) bottom = self.rect.bottom self.image = self.standing_frames[self.current_frame] self.rect = self.image.get_rect() self.rect.bottom = bottom self.mask = pg.mask.from_surface(self.image) [/QUOTE]
Verification
Post reply
Forums
Programming Languages
Python
Assistance with adding a second player to my Pygame :(
Top