0

Im trying to make a game where you dodge missiles moving side to side, using python and pygame. The first "proto type" i made worked great, but there i didn't use classes and sprites. Now i've been trying to make the same game but now using classes and sprites. The input works okay i guess, but the movement looks like a tetris.

import pygame, sys

class Sky(pygame.sprite.Sprite):
    def __init__(self, width, height, pos_x, pos_y, color):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x, pos_y]

class Ground(pygame.sprite.Sprite):
    def __init__(self, width, height, pos_x, pos_y, color):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x, pos_y]

class Player(pygame.sprite.Sprite):
    def __init__(self, width, height, pos_x, pos_y, color):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.midbottom = [pos_x, pos_y]

class Missile(pygame.sprite.Sprite):
    def __init__(self, width, height, pos_x, pos_y, color):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.midbottom = [pos_x, pos_y]

pygame.init()
clock = pygame.time.Clock()

#display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

#sky
sky = Sky(screen_width, screen_height, 0, 0, ("#DDCDE4FF"))
sky_group = pygame.sprite.Group()
sky_group.add(sky)

#ground
ground = Ground(screen_width, 120, 0, 480, ("#483A4AFF"))
ground_group = pygame.sprite.Group()
ground_group.add(ground)

#player
player = Player(90, 30, screen_width/2, 480, ("#331A42FF"))
player_group = pygame.sprite.Group()
player_group.add(player)

#missile
missile = Missile(10, 10, screen_width/2, 480, ("#331A42FF"))
missile_group = pygame.sprite.Group()
missile_group.add(missile)

#speeds
player_speed = 0
missile_speed = 5

#game running
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                player_speed += 10
            if event.key == pygame.K_a:
                player_speed -= 10
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_d:
                player_speed -= 10
            if event.key == pygame.K_a:
                player_speed += 10

    
    
    player.rect.x += player_speed



    pygame.display.flip()

    #draw stuff
    sky_group.draw(screen)
    ground_group.draw(screen)
    player_group.draw(screen)
    clock.tick(60)

I think the problem is in the main loop after the input...

player.rect.x += player_speed

Is there a way to fix it?

Ps. The missile wont do anything yet :D

  • The problem is not reproducible. It works fine for me. (`pygame.display.flip()` should be done after drawing the objects, but this is not the problem). – Rabbid76 Sep 25 '22 at 17:50
  • Hmm... Then i dont know whats wrong. Thanks anyway! – Samurai6219 Sep 25 '22 at 17:58
  • Would it work with Vector2 top down movement and then just removing the y movement... – Samurai6219 Sep 25 '22 at 18:12
  • Sorry, but it works. There is no problem with your code. The frame rate is `clock.tick(60)` (60 frames per second) and you move 10 pixels per frame. – Rabbid76 Sep 25 '22 at 18:15
  • I don't even know what "top-down movement" you are talking about. There is no top-down movement in your code. – Rabbid76 Sep 25 '22 at 18:24

0 Answers0