1

I am currently writing a program for my first python game (a simple top down shooter) and I have so far gotten to here using tutorials and examples online to help me build the code.

My character is able to rotate using vectors and that is all working how I want it to.

However, I cannot seem to make the player move using the arrow keys. I converted the velocity to a vector and when running the code and using the arrow keys, the players coordinates are actually changing and so in my head the player should be moving but isn't.

My initial thoughts are that player isn't being redrawn to the surface each time and hence staying in the same position, but I am not sure if this is correct.

What amendments/changes can I make in order to make this work as this problem has been driving me crazy the last few days.

import math
from random import randint
import pygame
from pygame.math import Vector2

pygame.init()

screen = pygame.display.set_mode((600, 600))
screen_width = 600
screen_height = 600
black = (0, 0, 0)
pygame.display.set_caption("gang")
clock = pygame.time.Clock()


class Character:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height


class Player(pygame.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = pygame.Surface((50, 30), pygame.SRCALPHA)
        pygame.draw.polygon(self.image, pygame.Color('steelblue2'),
                            [(0, 0), (50, 15), (0, 30)])
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=pos)
        self.pos = Vector2(pos)
        self.velocityx = Vector2(12, 0)
        self.velocityy = Vector2(0, 12)

    def update(self):
        self.rotate()

    def rotate(self):
        direction = pygame.mouse.get_pos() - self.pos
        radius, angle = direction.as_polar()
        self.image = pygame.transform.rotate(self.orig_image, -angle)
        self.rect = self.image.get_rect(center=self.rect.center)


class Enemy(Character):
    def __init__(self, x, y, width, height):
        Character.__init__(self, x, y, width, height)

    def draw(self, win):
        pygame.draw.rect(win, (0, 255, 0), (jeff.x, jeff.y, jeff.width, jeff.height))


def draw_window():
    screen.fill((30, 30, 30))
    jeff.draw(screen)
    all_sprites.update()
    all_sprites.draw(screen)
    pygame.display.update()


def xor(a, b):
    if bool(a) != bool(b):
        return True
    else:
        return False


def game_end():
    pygame.font.init()
    text = pygame.font.Font('freesansbold.ttf', 32)
    text_surface = text.render('Game Over', False, (0, 0, 0))
    text_rect = text_surface.get_rect()
    text_rect.center = (86, 86)
    screen.blit(text_surface, (172, 172))
    pygame.display.update()


mag = Player((150, 150))
playersprite = pygame.sprite.RenderPlain(mag)
all_sprites = pygame.sprite.Group(Player((300, 220)))
jeff = Enemy(randint(300, 500), randint(300, 500), 60, 60)
bullets = []


def main():
    run = True
    while run:
        clock.tick(60)

        keys = pygame.key.get_pressed()

        if xor(keys[pygame.K_a], keys[pygame.K_LEFT]):
            mag.pos -= mag.velocityx
            print(mag.pos)
        elif xor(keys[pygame.K_d], keys[pygame.K_RIGHT]):
            mag.pos += mag.velocityx
            print(mag.pos)
        elif xor(keys[pygame.K_w], keys[pygame.K_UP]):
            mag.pos -= mag.velocityy
            print(mag.pos)
        elif xor(keys[pygame.K_s], keys[pygame.K_DOWN]):
            mag.pos += mag.velocityy
            print(mag.pos)

        draw_window()


main()

domefist
  • 79
  • 3

1 Answers1

0

There are 2 issues in your code.

The Player object mag is not rendered at all, because you've created a 2nd Player object:

mag = Player((150, 150))
playersprite = pygame.sprite.RenderPlain(mag)
all_sprites = pygame.sprite.Group(Player((300, 220)))

Create 1 Player object and add it to the pygame.sprite.Group all_sprites:

mag = Player((150, 150))
playersprite = pygame.sprite.RenderPlain(mag)
all_sprites = pygame.sprite.Group(mag)  

When the pygame.sprite.Sprite of a .sprite.Group are drawn by draw(), then the .image is drawn at the location which is defined by .rect.
You've to update the position of the .rect attribute by .pos in .update:

class Player(pygame.sprite.Sprite):  

    # [...]

    def update(self):

        # update position
        self.rect.center = (int(self.pos.x), int(self.pos.y))       

        # update orientation
        self.rotate()

    def rotate(self):  
        direction = pygame.mouse.get_pos() - self.pos
        radius, angle = direction.as_polar()
        self.image = pygame.transform.rotate(self.orig_image, -angle)
        self.rect = self.image.get_rect(center=self.rect.center)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Ah! I see, not sure why I had created a separate object there. Thank you very much all is working well :) – domefist Apr 06 '19 at 13:07