Nov-21-2024, 04:39 PM
hi everyone.
jst new to python and i written a few apps for my phone but im giving gaming a go.
trying to code a simple invaders game as practice.
ive created code for rows of invaders but while they go left to right of the screen, every time it goes down the Y axis the 1st invader in the second row wanders left ever slighty every Y axis move. placing invaders down using tuples.
anyone help cause im loosing my mind why just 1 invader in the tuple is wandering 1 pixel every y step
thanks in advace for any help
![[Image: view?usp=sharing]](https://drive.google.com/file/d/16PMGzxpAOPjuTSxlbQZWVTY6byVTZlEf/view?usp=sharing)
jst new to python and i written a few apps for my phone but im giving gaming a go.
trying to code a simple invaders game as practice.
ive created code for rows of invaders but while they go left to right of the screen, every time it goes down the Y axis the 1st invader in the second row wanders left ever slighty every Y axis move. placing invaders down using tuples.
anyone help cause im loosing my mind why just 1 invader in the tuple is wandering 1 pixel every y step
thanks in advace for any help
import pygame import random import math from pygame import mixer # initializing pygame pygame.init() alien_image = pygame.image.load("assets/invader.png") playership_image = pygame.image.load("assets/player.png") lazer_image = pygame.image.load("assets/lazer.png") alien1= pygame.transform.scale(alien_image,(50,40)) player1 = pygame.transform.scale(playership_image,(50,50)) lazer1= pygame.transform.scale(lazer_image,(50,50)) alien = player1.get_rect() class Invader(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((20, 20)) aliens = [] invaders = [] alien_width = alien1.get_width() alien_height = alien1.get_height() alien_horizontal_spacing = 10 alien_vertical_spacing = 10 alien_speed = 1 # Create the alien grid for row_number in range(5): for column_number in range(6): x = alien_horizontal_spacing + column_number * (alien_width + alien_horizontal_spacing) y = alien_vertical_spacing + row_number * (alien_height + alien_vertical_spacing) alien = alien_image.get_rect(x = alien_horizontal_spacing + column_number * (alien_width + alien_horizontal_spacing), y = alien_vertical_spacing + row_number * (alien_height + alien_vertical_spacing)) aliens.append(alien) a= row_number *20 b= column_number * 20 invader = Invader(x, y) aliens.append(alien) # creating screen screen_width = 1200 screen_height = 800 screen = pygame.display.set_mode((screen_width,screen_height)) bullet_state = "rest" obj_velocity_x = 2 obj_velocity_y = 10 running = True if running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #sys.quit() while True: screen.fill(0) for alien in aliens: alien.x += alien_speed if alien.x <= 0 or alien.x >= screen_width-alien_width: alien_speed *= -1 for alien in aliens: alien.y += 20 for alien in aliens: screen.blit(alien1,(alien)) pygame.display.update()