0

I especially want to focus on the event and update function.

In the event function I update all the platforms and move their y coordinates by 1, so it looks like I shift them down the page.

Then in the update function where I call the player.move(), left and right movement works.

However, the jump that is called in the event sometimes works and sometimes does not. Why could this be?

Code and class Game

import pygame as pg
 import socket
 from platforms import Platform
 from network import Network
 from player import Player
import pickle
from settings import *



class Game:
def __init__(self):
    pg.init()
    pg.mixer.init()
    self.screen = pg.display.set_mode((screen_width, screen_height))
    pg.display.set_caption(title)
    self.clock = pg.time.Clock()
    self.running = True
    self.send_more_platforms = False
    self.pushdown = 0


def new(self):
    self.network = Network()
    
    self.starting_info = self.network.getP() # get the information sent from the server

    playersPos = self.starting_info[0]
    player1Pos = playersPos[0]
    player2Pos = playersPos[1]
    
    platformPos = self.starting_info[1]

    self.totalSprites = pg.sprite.Group() # making sprite groups 
    self.platforms = pg.sprite.Group()
 
    self.player1 = Player(player1Pos[0],player1Pos[1],self,green) # PLAYER 1
    self.totalSprites.add(self.player1)

    self.player2 = Player(player2Pos[0],player2Pos[1],self,red)  
    self.totalSprites.add(self.player2)


    for i in range(len(platformPos)):

        p = Platform(*platformPos[i])       # *platformPos[i] is the same as plafrom[0],plafrom[1],plafrom[2],plafrom[3]
        self.totalSprites.add(p)
        self.platforms.add(p)
   
    self.run()


def run(self):
    self.run = True
    while self.run:
        self.clock.tick(fps)
        self.events()
        self.update()
        self.draw()


def events(self):
    
    info_recv = self.network.send(([int(self.player1.position.x), int(self.player1.position.y)],self.send_more_platforms))  #when you send player1, the network sends player 2 to this client, and viceversa for player2 
    self.send_more_platforms = False
    player2Pos = (info_recv[0])
    self.player2.position.x = player2Pos[0]
    self.player2.position.y = player2Pos[1] 
    self.player2.update()

    for event in pg.event.get():
        if event.type == pg.QUIT:
            if self.run:
                self.run = False
            self.running = False
        if event.type == pg.KEYDOWN:    
            if event.key == pg.K_SPACE:
                self.player1.jump()

    for platform in self.platforms:
        platform.rect.y += 1
   


def update(self):
 
    #print(self.player1.rect.midbottom)

    self.player1.move()
    
    # what this does, is it only checks collisions whilst falling, not whilst jumping
    if self.player1.velocity.y>= 0 :  
        hits = pg.sprite.spritecollide(self.player1, self.platforms, False)
        if hits :
            
            self.player1.position.y = hits[0].rect.top
            self.player1.velocity.y = 0


    
    
    

    # this is going to act like a camera shift when the player reaches around the top of the screen
    # and delete platforms that go off the screen

    # if self.player1.rect.top <= screen_height/4:
    #     self.pushdown +=  abs(self.player1.velocity.y)
    #     self.player1.position.y += abs(self.player1.velocity.y)
    #     self.player2.position.y += abs(self.player1.velocity.y)
    #     for platform in self.platforms:
    #         platform.rect.y += abs(self.player1.velocity.y)
            
    #         if platform.rect.top >= screen_height+(screen_height/4):
    #             platform.kill()

    # make new platforms 
    
def draw(self):
    self.screen.fill(white)
    self.totalSprites.draw(self.screen)
    pg.display.update() # updates the whole screen, try to limit the times you update screen as this is the most intensive code. slowing the animation by a lot



 game = Game()
 while game.running:
  game.new()

 pg.quit() 

Player class

 import pygame 
 from settings import *
 vec = pygame.math.Vector2 # vectors for easier handling when creating positions

 class Player(pygame.sprite.Sprite):
     def __init__(self,x,y,game,colour):
    pygame.sprite.Sprite.__init__(self)
    # self.x = x
    # self.y = y
    # self.width = width
    # self.height = height
    self.colour = colour
    self.game = game 

    self.image = pygame.Surface((40, 70))
    self.image.fill(colour)
    self.rect = self.image.get_rect()
    self.position = vec(x,y)
    #self.rect.midbottom = self.position 
    
  

    self.velocity = vec(0,0)
    self.acceleration = vec(0,0)
    
    
    self.jump_velocity = PLAYER_jumpvelocity
    self.isJump = False
    
    
    self.mass = 1
    self.turning_point = 1

def move(self):
    self.acceleration = vec(0,PLAYER_gravity)  # if keys arent being pressed put acceleration to 0 as its not moving, PLAYER_gravity affect the y axis, so it adds gravity constantly to the player
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        self.acceleration.x = -PLAYER_acceleration
    if keys[pygame.K_RIGHT]:
        self.acceleration.x = PLAYER_acceleration
    
    self.update()

   
# we seperate the update and moving function apart beacuse, if moving and updating the position of the players are in one function, then one client can control both players movements and be synchrnoized.
# which is why when we update the client code, we call move for player1, but only update for player2 so that player 2 cant move only when a second client joins    
def update(self):  

    
    # applying friction in movement
    self.acceleration.x += self.velocity.x * PLAYER_friction
    self.velocity += self.acceleration
    self.position += self.acceleration*0.5  + self.velocity
    
         
   # switching to other side of screen if it hits the edge
    if self.position.x > screen_width:
        self.position.x = 0
    if self.position.x < 0:
        self.position.x = screen_width

    self.rect.midbottom = self.position # sets the mid bottom of the rect to these coordinate the position of rectangle

#self.rect.x -= 1    

def jump(self):
    # jump allowed only if standing on a platform
    hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
    
    if hits:
        self.velocity.y =-20

Thoughts & what I tried

However, I know that the jump function works. Because if I remove following block, the jump works fine.

for platform in self.platforms:
    platform.rect.y += 1` 

What I think the issue might be:

  • whilst the for-loop is moving the platforms down by 1, within this for-loop it can't check for movements like jump.

Do you think that could be the reason ?

hc_dev
  • 5,553
  • 20
  • 27
Mal Hoti
  • 1
  • 1
  • 1
    Please add your comments to the question directly. Please improve code-formatting (proper indentation) so syntax is correct and we can reproduce easily. – hc_dev Dec 29 '21 at 16:05

0 Answers0