0

I am trying to make a game for a school project. I decided to do it in python because I have used it before, but I have taken a break from coding and don't remember much, and I cant figure out why my code wont work. I wanted to make a game similar to the dinosaur game (like the one on chrome) and my dinosaur wont jump when I think it should, I tried everything I can think of and searched for several hours for ways to fix it.

Here is my code:

import pygame, sys, random
from pygame.locals import QUIT

#variables
x=241

class Player(pygame.sprite.Sprite):

    def __init__(self, picture_path, pos_x, pos_y):
       super().__init__()
       self.image = pygame.image.load(picture_path)
       self.rect = self.image.get_rect()
       self.rect.center = [pos_x,pos_y]



class Weed(pygame.sprite.Sprite):

    def __init__(self, picture_path, pos_x, pos_y):
       super().__init__()
       self.image = pygame.image.load(picture_path)
       self.rect = self.image.get_rect()
       self.rect.center = [pos_x,pos_y]


#general setup
pygame.init()
clock = pygame.time.Clock()
#game screen
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('game')
second_surface = pygame.Surface((400, 100))
second_surface.fill((4, 130, 40))
player = Player('DinoSprites - tard.png', 40, x)
player_group = pygame.sprite.Group()
player_group.add(player)

weed_group = pygame.sprite.Group()
for weed in range(3):
    new_weed = Weed('tumble_weed.png', random.randrange(80,400), 241)
    weed_group.add(new_weed)

while True:


    screen.fill((168, 202, 255))
    screen.blit(second_surface, (0, 250))
    player_group.draw(screen)
    weed_group.draw(screen)


    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                x+=5


    pygame.display.update()
    clock.tick(15)

I wanted to make it so if the player pressed space the dinosaur would jump. (I didn't figure out how it would fall afterwards, but I wanted to make him go up.) When I tried that it didn't work. The code runs, but when I press space the sprite just stays in the same place. I'm not sure if its because the sprite wont update or if the code to change the dinosaur height is wrong.

hjpoe
  • 96
  • 10
  • You define `x` with value 241. Then you use it to create `player`, but that sets the x-position to `241`. It does not somehow bind it. I mean, that changing the `x` variable does not change the position of the `Player` instance. You are free to redefine `x` to anything without it impacting the player position. To do that you should most likely change the player.rect property. – Mitchell van Zuylen Jan 10 '23 at 01:36

1 Answers1

0

You have to change player.rect.x instead of x. x is just a variable used when creating the player. However, the internal attributes of player do not magically change when you change x. x and player.rect.x are not tied somehow:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_SPACE:
        player.rect.x += 5

Alos see How can I make a sprite move when key is held down

Rabbid76
  • 202,892
  • 27
  • 131
  • 174