Skip to content

Commit

Permalink
Smooth mouse movement update.
Browse files Browse the repository at this point in the history
v 1.4
  • Loading branch information
gowth6m committed Nov 4, 2018
1 parent 6252731 commit b8f8ae2
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 11 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file removed __pycache__/.DS_Store
Binary file not shown.
Binary file added __pycache__/main.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/settings.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/sprites.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/tilemap.cpython-37.pyc
Binary file not shown.
Binary file modified img/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ def draw(self):
# self.all_sprites.draw(self.screen)

# TEST CODE TO SEE COORDINATES
# GET_FPS = "{0:.2f}".format(self.clock.get_fps())

rendered = FONT.render("X: "+str(int(self.player.pos.x)), True, WHITE)
self.screen.blit(rendered, (10, 10))
rendered = FONT.render("Y: "+str(int(self.player.pos.y)), True, WHITE)
self.screen.blit(rendered, (10, 30))
rendered2 = FONT.render("MousePos: "+str(pg.mouse.get_pos()), True, WHITE)
self.screen.blit(rendered2, (10, 50))

rendered4 = FONT.render("FPS: "+str(round(self.clock.get_fps(), 2)), True, GREEN)
self.screen.blit(rendered4, (10, 80))

rendered3 = FONT.render("Wealth: 0", True, GREEN)
self.screen.blit(rendered3, (WIDTH-100, 10))
# UPDATE
Expand Down
4 changes: 3 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# IMPORTS AND FILES
import pygame as pg
import math
# from main import *
pg.init()
pg.font.init()

Expand Down Expand Up @@ -28,10 +29,11 @@
# PLAYER PROPERTIES
# PLAYER_ACC = 8.0
# PLAYER_FRICTION = -0.3
PLAYER_SPEED = 150
# PLAYER_IMG = 'slime.png'
PLAYER_SPEED = 150
PLAYER_IMG = 'main_player.png'
PLAYER_ROT_SPEED = math.pi
PLAYER_HIT_RECT = pg.Rect(0, 0, 32, 32)

# FONT
FONT = pg.font.SysFont("None", 25)
Expand Down
24 changes: 14 additions & 10 deletions sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pygame as pg
import math
from settings import *
from tilemap import collide_hit_rect
# from main import Game
vec = pg.math.Vector2

Expand All @@ -15,6 +16,8 @@ def __init__(self, game, x, y):
self.orig_image = self.image
# self.image.blit(P_IMG, (0, 0))
self.rect = self.image.get_rect()
self.hit_rect = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0, 0)
self.pos = vec(x, y) * TILESIZE
self.mouse_pos = pg.mouse.get_pos()
Expand All @@ -37,23 +40,23 @@ def get_keys(self):

def collide_with_walls(self, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
if hits:
if self.vel.x > 0:
self.pos.x = hits[0].rect.left - self.rect.width
self.pos.x = hits[0].rect.left - self.hit_rect.width/2
if self.vel.x < 0:
self.pos.x = hits[0].rect.right
self.pos.x = hits[0].rect.right + self.hit_rect.width/2
self.vel.x = 0
self.rect.x = self.pos.x
self.hit_rect.centerx = self.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
if hits:
if self.vel.y > 0:
self.pos.y = hits[0].rect.top - self.rect.height
self.pos.y = hits[0].rect.top - self.hit_rect.height/2
if self.vel.y < 0:
self.pos.y = hits[0].rect.bottom
self.pos.y = hits[0].rect.bottom + self.hit_rect.height/2
self.vel.y = 0
self.rect.y = self.pos.y
self.hit_rect.centery = self.pos.y

def rotate_player(self): #JUST NORMAL COORDs
mouse_x, mouse_y = pg.mouse.get_pos()
Expand All @@ -73,11 +76,12 @@ def rotate_player(self): #JUST NORMAL COORDs
def update(self):
self.get_keys()
self.pos += self.vel * self.game.dt
self.rect.centerx = self.pos.x
self.hit_rect.centerx = self.pos.x
self.collide_with_walls('x')
self.rect.centery = self.pos.y
self.hit_rect.centery = self.pos.y
self.collide_with_walls('y')
self.rotate_player()
self.rect.center = self.hit_rect.center
# self.player_rotate()

# WRAP AROUND SCREEN SIDE
Expand Down
3 changes: 3 additions & 0 deletions tilemap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pygame as pg
from settings import *

def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)

class Map:
def __init__(self, filename):
self.map_data = []
Expand Down

0 comments on commit b8f8ae2

Please sign in to comment.