clear-code-projects / PyDew-Valley

Proejct files for a Stardew Valley inspired game in Python
461 stars 171 forks source link

Why do I rewrite Sprite.update #1

Closed zhanghed closed 1 year ago

zhanghed commented 1 year ago

import pygame from settings import *

class Player(pygame.sprite.Sprite): def init(self, pos, group): super().init(group)

    # general setup
    self.image = pygame.Surface((32,64))
    self.image.fill('green')
    self.rect = self.image.get_rect(center = pos)

    # movement attributes
    self.direction = pygame.math.Vector2()
    self.pos = pygame.math.Vector2(self.rect.center)
    self.speed = 200

def input(self):
    keys = pygame.key.get_pressed()

    if keys[pygame.K_UP]:
        self.direction.y = -1
    elif keys[pygame.K_DOWN]:
        self.direction.y = 1
    else:
        self.direction.y = 0

    if keys[pygame.K_RIGHT]:
        self.direction.x = 1
    elif keys[pygame.K_LEFT]:
        self.direction.x = -1
    else:
        self.direction.x = 0

def move(self,dt):

    # normalizing a vector 
    if self.direction.magnitude() > 0:
        self.direction = self.direction.normalize()

    # horizontal movement
    self.pos.x += self.direction.x * self.speed * dt
    self.rect.centerx = self.pos.x

    # vertical movement
    self.pos.y += self.direction.y * self.speed * dt
    self.rect.centery = self.pos.y

def update(self, dt):
    self.input()
    self.move(dt)