Python Forum
Gravity doesn't works, and my character just moves right and really fast
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gravity doesn't works, and my character just moves right and really fast
#1
So I could fix my last error thanks to an user, but when I run the game my character just sticks in the air and only can move left when I press rioght and when I press right just moves a little, but when I press it again just moves so quickly to the right that leaves the screen.
The lines that I wrote so you guys can locate them easly: (49-70) collision movement, (111) code so the def move works
import pygame
import sys
from pygame import *

pygame.init()

# Fps
clock = pygame.time.Clock()

# Title
pygame.display.set_caption('HandsomeBeta')
# Screen
window = (1280, 720)
screen = pygame.display.set_mode(window)
display = pygame.Surface((1280, 720))

# player image
player = pygame.image.load('assets4plyr/Handsome1.png')
handsomeman = pygame.transform.scale(player, (55, 55))
handsomeman.set_colorkey((255, 255, 255))

grassimg = pygame.image.load('assets4game/handsomegrass.png')
tile_size = grassimg.get_height()
dirtimg = pygame.image.load('assets4game/handsomedirt.png')

game_map = [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2'],
            ['1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']]

def collision_test(rect, tiles):
    hit_list = []
    for tile in tiles:
        if rect.colliderect(tile):
            hit_list.append(tile)
    return hit_list

def move(rect, movement, tiles):
    collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
    rect.x += movement[0]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[0] > 0:
            rect.right = tile.left
            collision_types['right'] = True
        elif movement[0] < 0:
            rect.left = tile.right
            collision_types['left'] = True
    rect.y += movement[1]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[1] > 0:
            rect.bottom = tile.top
            collision_types['bottom'] = True
        elif movement[1] < 0:
            rect.top = tile.bottom
            collision_types['top'] = True

    return rect, collision_types


moving_right = False
moving_left = False

playerpos = [640, 200]
playerY_momentum = 0

plyrrect = pygame.Rect(50, 50, handsomeman.get_width(), handsomeman.get_height())
test_rect = pygame.Rect(640, 360, 100, 50)

# GameLoop
while True:

    display.fill((146, 244, 255))

    tile_rects = []
    y = 0
    for row in game_map:
        x = 0
        for tile in row:
            if tile == '1':
                display.blit(dirtimg, (x * tile_size, y * tile_size))
            if tile == '2':
                display.blit(grassimg, (x * tile_size, y * tile_size))
            if tile == '0':
                tile_rects.append(pygame.Rect(x * tile_size, y * tile_size, tile_size, tile_size))
            x += 1
        y += 1

    player_movement = [0, 0]
    if moving_right:
        player_movement[0] += 2
    if moving_left:
        player_movement[0] -= 2
    player_movement[1] += playerY_momentum
    playerY_momentum += 0.2
    if playerY_momentum > 3:
        playerY_momentum = 3

    plyrrect, collisions = move(plyrrect, player_movement, tile_rects)

    display.blit(handsomeman, (plyrrect.x, plyrrect.y))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False
    surf = pygame.transform.scale(display, window)
    screen.blit(surf, (0, 0))
    pygame.display.update()
    clock.tick(60)
Reply
#2
Forget about collisions and see if you can get moving around to work. The subject mentions gravity, but there is not a gravity calculation anywhere in your code.
izmamonke likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code works but doesn't give the right results colin_dent 2 710 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,007 Dec-18-2021, 02:38 AM
Last Post: knight2000
  [solved] unexpected character after line continuation character paul18fr 4 3,388 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,158 Jul-13-2020, 07:05 PM
Last Post: snippsat
  works but it doesn't benlyboy 3 2,076 Feb-03-2020, 02:05 AM
Last Post: benlyboy
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,732 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,174 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 18,547 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

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