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.