Dec-14-2019, 12:15 AM
I have this sprite here. The controls are dreadful, and my tinkering is fruitless. I copied it from an Asteroids game, and took out the friction, but I broke it.
Sometimes the "thrust" will cause it to slow/break.
How fast you go is effected strongly by your angle.
Here is the sprite and the event handling. I haven't success yet with this type of controls yet. Any advice would be appreciated.
Sometimes the "thrust" will cause it to slow/break.
How fast you go is effected strongly by your angle.
Here is the sprite and the event handling. I haven't success yet with this type of controls yet. Any advice would be appreciated.
ship_max_speed = 2 ship_max_rtspd = 3 class Ship(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.ship_img = pygame.Surface((15,10), pygame.SRCALPHA) self.ship_img.fill(red) self.image = self.ship_img self.rect = self.image.get_rect() self.hit_rect = ship_HIT_RECT self.hit_rect.center = self.rect.center self.rect.centerx = x self.rect.centery = y self.hspeed = 0 self.vspeed = 0 self.dir = 0 self.rtspd = 0 self.thrust = False def update(self): speed = math.sqrt(self.hspeed**2 + self.vspeed**2) if self.thrust: if speed < ship_max_speed: self.hspeed += math.cos(self.dir * math.pi / 180) self.vspeed += math.sin(self.dir * math.pi / 180) else: self.hspeed = ship_max_speed * math.cos(self.dir * math.pi / 180) self.vspeed = ship_max_speed * math.sin(self.dir * math.pi / 180) self.rect.centerx += self.hspeed self.rect.centery += self.vspeed self.dir = (self.dir + self.rtspd) % 360 self.image = pygame.transform.rotate(self.ship_img, -self.dir) self.rect = self.image.get_rect(center=self.rect.center) #################################################################################### def get_event(self, event): self.ship.rtspd = 0 if event.type == pygame.QUIT: gameState = "Exit" if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.ship.thrust = True if event.key == pygame.K_LEFT: self.ship.rtspd = -ship_max_rtspd if event.key == pygame.K_RIGHT: self.ship.rtspd = ship_max_rtspd if event.type == pygame.KEYUP: if event.key == pygame.K_UP: self.ship.thrust = False if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: self.ship.rtspd = 0