[PyGame] newbie needs help with space invaders - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] newbie needs help with space invaders (/thread-28088.html) |
newbie needs help with space invaders - n0x - Jul-04-2020 So I've been working on my space invaders but I can't figure out how I have to solve that the bullet would stay on the same x coordinate when I press spcae again. Sorry English is not my first language and there is a mix of both English and Estonian ( kuul is bullet in Estonian and kiirus is speed). I have removed "spaceship" at the moment to see what the bullet does exactly behind the spaceship. import pygame from pygame.locals import * running = True pygame.init() screen = pygame.display.set_mode((1000, 800)) enemy_pos = [500, 70] ship_pos = [700, 500] speed = 0.4 red = (255, 0, 0) white = (255, 255, 255) blu = (0, 0, 255) kuul_img = pygame.image.load('bullet.png') kuul_x = 0 kuul_y = 700 kuul_kiirus = 1 kuul_state = "valmis" def fire_bullet(x, y): global bullet_state kuul_state = "fire" screen.blit(kuul_img, (x + 16, y + 10)) print("b") while running: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: ship_pos[1] -= speed elif event.key == pygame.K_d: ship_pos[1] += speed elif event.key == pygame.K_SPACE: if kuul_state is "valmis": print("a") kuul_x = ship_pos[1] fire_bullet(kuul_x, kuul_y) if kuul_state is "fire": kuul_y - kuul_kiirus if kuul_y is 0: kuul_y = 700 kuul_state = "valmis" #pygame.draw.rect(screen, white, [ship_pos[1], ship_pos[0], 50, 50]) pygame.draw.rect(screen, red, [enemy_pos[0], enemy_pos[1], 50, 50]) pygame.display.update() RE: newbie needs help with space invaders - metulburr - Jul-04-2020 screen.blit is what is drawing the image. And with the way you coded it, it is only in a callback function that only runs at the time you are pressing the spacebar down. So it is only being drawn when you have the spacebar down. I suggest to learn classes as well. Because it is far easier to maintain as well as understand code of a class Ship and a class Bullet than using globals. My tutorials series does this concept. import pygame as pg pg.init() class Laser: def __init__(self, loc): self.image = pg.Surface((5,40)).convert() self.image.fill((255,255,0)) self.rect = self.image.get_rect(center=loc) self.speed = 5 def update(self): self.rect.y -= self.speed def render(self, surf): surf.blit(self.image, self.rect) class Player: def __init__(self, screen_rect): self.screen_rect = screen_rect #self.image = pg.image.load('spaceship.png').convert() self.image = pg.Surface([50,50]) self.image.fill((0,0,255)) self.image.set_colorkey((255,0,255)) self.transformed_image = pg.transform.rotate(self.image, 180) start_buffer = 300 self.rect = self.image.get_rect( center=(screen_rect.centerx, screen_rect.centery + start_buffer) ) self.dx = 300 self.lasers = [] def get_event(self, event): if event.type == pg.KEYDOWN: if event.key == pg.K_SPACE: self.lasers.append(Laser(self.rect.center)) def update(self, keys, dt): self.rect.clamp_ip(self.screen_rect) if keys[pg.K_LEFT]: self.rect.x -= self.dx * dt elif keys[pg.K_RIGHT]: self.rect.x += self.dx * dt for laser in self.lasers: laser.update() def draw(self, surf): for laser in self.lasers: laser.render(surf) surf.blit(self.transformed_image, self.rect) screen = pg.display.set_mode((800,600)) screen_rect = screen.get_rect() player = Player(screen_rect) clock = pg.time.Clock() done = False while not done: keys = pg.key.get_pressed() for event in pg.event.get(): if event.type == pg.QUIT: done = True player.get_event(event) screen.fill((0,0,0)) delta_time = clock.tick(60)/1000.0 player.update(keys, delta_time) player.draw(screen) pg.display.update() RE: newbie needs help with space invaders - n0x - Jul-04-2020 I'll check it out thanks |