I have recently made a game in python 2.7.6 with pygame and I am now converting all of the syntax and code to work with python 3.3.3. But I am having an issue with collision. Before I converted the collisions worked flawlessly, but in 3.3.3 all the collisions completely messed up. Here is my current code for the player, zombie and boss collisions:
def player_move(self):
# Line start
self.point = self.player.rect.x + 30, self.player.rect.y + 30
# add gravity
self.player.do_jump()
# simulate gravity
self.player.on_ground = False
if not self.player.on_ground and not self.player.jumping:
self.player.velY = 4
if self.player.rect.y >= 680:
self.player.lives -= 1
self.player.rect.x = 320
self.player.rect.y = 320
# Drops
for drop in self.gameDrops:
if pygame.sprite.collide_rect(self.player, drop):
if type(drop) == HealthDrop:
self.player.health += 50
self.gameDrops.remove(drop)
elif type(drop) == SuperHealthDrop:
self.player.health += 1000
self.gameDrops.remove(drop)
elif type(drop) == TriBullets:
self.player.power = POWER_TRIBULLETS
self.gameDrops.remove(drop)
elif type(drop) == ShieldDrop:
self.player.power = POWER_SHIELD
self.gameDrops.remove(drop)
if self.player.score >= self.next_level_score:
self.game_state = STATE_VICTORY
self.player.score = 0
self.zombieskilled = 0
self.alivetime = 0
self.lives = 3
self.health = 200
self.player.rect.x = 320
self.player.rect.y = 320
for zombie in self.zombies:
zombie.rect.x = random.randint(0, 1280)
zombie.rect.y = random.randint(0, 100)
# Health
for m in self.meteors:
if pygame.sprite.collide_rect(self.player, m):
if self.player.power == POWER_SHIELD:
self.player.health -= 25
else:
self.player.health -= 50
for zombie in self.zombies:
if pygame.sprite.collide_rect(self.player, zombie):
if self.player.power == POWER_SHIELD:
self.player.health -= 1
else:
self.player.health -= 5
for boss in self.bosses:
if pygame.sprite.collide_rect(self.player, boss):
if self.player.power == POWER_SHIELD:
self.player.health -= 50
else:
self.player.health -= 50
# check if we die
if self.player.health <= 0:
self.player.power = POWER_NONE
self.player.lives -= 1
self.player.rect.x = 320
self.player.rect.y = 320
self.player.health = 200
if self.player.lives <= 0:
self.player.lives += 3
self.player.score = 0
self.targetscore = 100
self.player.rect.x = 320
self.player.rect.y = 320
self.game_state = STATE_GAMEOVER
for zombie in self.zombies:
zombie.rect.x = random.randint(0, 1280)
zombie.rect.y = random.randint(0, 100)
# move player and check for collision at the same time
self.player.rect.x += self.player.velX
self.check_collision(self.player, self.player.velX, 0)
self.player.rect.y += self.player.velY
self.check_collision(self.player, 0, self.player.velY)
def zombie_move(self, zombie_sprite):
# add gravity
zombie_sprite.do_jump()
percentage = random.randint(0, 100)
# simualte gravity
zombie_sprite.on_ground = False
if not zombie_sprite.on_ground and not zombie_sprite.jumping:
zombie_sprite.velY = 8
if zombie_sprite.jumping:
zombie_sprite.velX = 0
for zombie in self.zombies:
if zombie.rect.y >= 680:
self.zombies.remove(zombie)
dx = zombie_sprite.rect.x - self.player.rect.x
if dx > 20 and not zombie_sprite.jumping:
zombie_sprite.velX = -5
elif dx < 20 and not zombie_sprite.jumping:
zombie_sprite.velX = +5
# Zombie damage
for zombie in self.zombies:
for m in self.meteors:
if pygame.sprite.collide_rect(m, zombie):
zombie.health -= 10
if zombie.health <= 0:
if (percentage >= 0) and (percentage < 40):
self.gameDrops.append(HealthDrop(zombie.rect.x + 10, zombie.rect.y + 30))
elif (percentage >= 0) and (percentage < 5):
self.gameDrops.append(SuperHealthDrop(zombie.rect.x + 20, zombie.rect.y + 30))
elif (percentage >= 1) and (percentage < 20):
self.gameDrops.append(TriBullets(zombie.rect.x + 30, zombie.rect.y + 30, self.player.direction))
elif (percentage >= 1) and (percentage < 50):
self.gameDrops.append(ShieldDrop(zombie.rect.x + 40, zombie.rect.y + 30))
self.zombieskilled += 0
self.player.score += 0
self.meteors.remove(m)
break
for zombie in self.zombies:
for b in self.bullets:
if pygame.sprite.collide_rect(b, zombie):
#The same bullet cannot be used to kill
#multiple zombies and as the bullet was
#no longer in Bullet.List error was raised
zombie.health -= 10
self.bullets.remove(b)
if zombie.health <= 0:
if (percentage >= 0) and (percentage < 40):
self.gameDrops.append(HealthDrop(zombie.rect.x + 10, zombie.rect.y + 30))
elif (percentage >= 0) and (percentage < 5):
self.gameDrops.append(SuperHealthDrop(zombie.rect.x + 20, zombie.rect.y + 30))
elif (percentage >= 1) and (percentage < 20):
self.gameDrops.append(TriBullets(zombie.rect.x + 30, zombie.rect.y + 30, self.player.direction))
elif (percentage >= 1) and (percentage < 50):
self.gameDrops.append(ShieldDrop(zombie.rect.x + 40, zombie.rect.y + 30))
self.zombieskilled += 1
self.player.score += 10
self.total_score += 10
self.zombies.remove(zombie)
break
# move zombie and check for collision
zombie_sprite.rect.x += zombie_sprite.velX
self.check_collision(zombie_sprite, zombie_sprite.velX, 0)
zombie_sprite.rect.y += zombie_sprite.velY
self.check_collision(zombie_sprite, 0, zombie_sprite.velY)
def boss_move(self, boss_sprite):
# add gravity
boss_sprite.do_jump()
percentage = random.randint(0, 100)
# simualte gravity
boss_sprite.on_ground = False
if not boss_sprite.on_ground and not boss_sprite.jumping:
boss_sprite.velY = 8
if boss_sprite.jumping:
boss_sprite.velX = 0
for boss in self.bosses:
if boss.rect.y >= 620:
self.bosses.remove(boss)
dx = boss_sprite.rect.x - self.player.rect.x
if dx > 20 and not boss_sprite.jumping:
boss_sprite.velX = -5
elif dx < 20 and not boss_sprite.jumping:
boss_sprite.velX = +5
# Zombie damage
for boss in self.bosses:
for m in self.meteors:
if pygame.sprite.collide_rect(m, boss):
boss.health -= 0
if boss.health <= 0:
if (percentage >= 0) and (percentage < 40):
self.gameDrops.append(HealthDrop(boss.rect.x + 10, boss.rect.y + 30))
elif (percentage >= 0) and (percentage < 5):
self.gameDrops.append(SuperHealthDrop(boss.rect.x + 20, boss.rect.y + 30))
elif (percentage >= 1) and (percentage < 20):
self.gameDrops.append(TriBullets(boss.rect.x + 30, boss.rect.y + 30, self.player.direction))
elif (percentage >= 1) and (percentage < 50):
self.gameDrops.append(ShieldDrop(boss.rect.x + 40, boss.rect.y + 30))
self.zombieskilled += 0
self.player.score += 0
self.meteors.remove(m)
break
for boss in self.bosses:
for b in self.bullets:
if pygame.sprite.collide_rect(b, boss):
#The same bullet cannot be used to kill
#multiple bosses and as the bullet was
#no longer in Bullet.List error was raised
boss.health -= 5
self.bullets.remove(b)
if boss.health <= 0:
if (percentage >= 0) and (percentage < 50):
self.gameDrops.append(SuperHealthDrop(boss.rect.x + 90, boss.rect.y + 90))
elif (percentage >= 0) and (percentage < 50):
self.gameDrops.append(TriBullets(boss.rect.x + 90, boss.rect.y + 90, self.player.direction))
self.zombieskilled += 1
self.player.score += 50
self.total_score += 50
self.bosses.remove(boss)
break
# move boss and check for collision
boss_sprite.rect.x += boss_sprite.velX
self.check_collision(boss_sprite, boss_sprite.velX, 0)
boss_sprite.rect.y += boss_sprite.velY
self.check_collision(boss_sprite, 0, boss_sprite.velY)
def check_collision(self, sprite, x_vel, y_vel):
# for every tile in Background.levelStructure, check for collision
for block in self.moon.get_surrounding_blocks(sprite):
if block is not None:
if pygame.sprite.collide_rect(sprite, block):
# we've collided! now we must move the collided sprite a step back
if x_vel < 0:
sprite.rect.x = block.rect.x + block.rect.w
if type(sprite) is Zombie:
# the sprite is a zombie, let's make it jump
if not sprite.jumping:
sprite.jumping = True
sprite.on_ground = False
else:
sprite.jumping = False
sprite.on_ground = True
if x_vel > 0:
sprite.rect.x = block.rect.x - sprite.rect.w
if type(sprite) is Zombie:
# the sprite is a zombie, let's make it jump
if not sprite.jumping:
sprite.jumping = True
sprite.on_ground = False
else:
sprite.jumping = False
sprite.on_ground = True
if y_vel < 0:
sprite.rect.y = block.rect.y + block.rect.h
if y_vel > 0 and not sprite.on_ground:
sprite.on_ground = True
sprite.rect.y = block.rect.y - sprite.rect.h
That is all of the code from the player, zombie and boss for them you collide and there functions so they know to collide. I have called the collision function too as you can see in the code for all three. I am really annoyed at this because I am not sure whether it is to do with syntax or what. Any help will be appreciated. Thank you.
I have finally found the fix! In my levelstructure file to randomly generate the terrain and position. In my generation class I had a get_surrounding_blocks as you can see in def check_collision. In that surrounding_blocks I had:
def get_surrounding_blocks(self, sprite):
# calculate the grid position of the sprite
sprite_x = sprite.rect.x / 64
sprite_y = sprite.rect.y / 64
And in python 2.7.6 one division sign (/) will give you an integer but in python 3.3.3 you must do // to get an integer otherwise you may get a float with just one /.
Related
Im trying to recreate Conways game of life where the average color of the surrounding cells will be the color of the new dead cell created, although I'm having issues trying to count the surrounding neighbors of a certain cell in order to determine if that cell should be dead or alive.
def count_neighbors(self, i, j):
neighbors = []
r_sum = 0
g_sum = 0
b_sum = 0
''' The range(-1, 2) in the for loop allows the loop to check the cells in the positions
relative to the current cell, which are the eight cells surrounding it.'''
for x in range(-1, 2):
for y in range(-1, 2):
if (x, y) == (0, 0):
continue
if 0 <= int(i) + x < len(self._board) and 0 <= int(j) + y < len(self._board):
if self._board[i + x][j + y] != (0, 0, 0):
neighbors.append(self._board[i + x][j + y])
r_sum += self._board[i + x][j + y][0]
g_sum += self._board[i + x][j + y][1]
b_sum += self._board[i + x][j + y][2]
num_neighbors = len(neighbors)
if num_neighbors == 0:
return 0, (0, 0, 0)
return num_neighbors, (r_sum // num_neighbors, g_sum // num_neighbors, b_sum // num_neighbors)
I am using opencvs findContour to find the points to describe an image made up of lines (not polygons) as such:
cv::findContours(src, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);.
If I understand correctly, the "cv2.connectedComponents" method gives what you are looking for. It assigns a label for each point in your image, the label is the same if points are connected. By doing this assignment there is no duplication happening. So, if your lines are one pixel wide (e.g output of an edge detector or a thinning operator) you get one point per location.
Edit:
As per the OP request, lines should be 1-pixel wide. To achieve this a thinning operation is applied before finding connected components. Steps images have been added too.
Please note that each connected component points are sorted in ascending order of y cords.
img_path = "D:/_temp/fig.png"
output_dir = 'D:/_temp/'
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
_, img = cv2.threshold(img, 128, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)
total_white_pixels = cv2.countNonZero(img)
print ("Total White Pixels Before Thinning = ", total_white_pixels)
cv2.imwrite(output_dir + '1-thresholded.png', img)
#apply thinning -> each line is one-pixel wide
img = cv2.ximgproc.thinning(img)
cv2.imwrite(output_dir + '2-thinned.png', img)
total_white_pixels = cv2.countNonZero(img)
print ("Total White Pixels After Thinning = ", total_white_pixels)
no_ccs, labels = cv2.connectedComponents(img)
label_pnts_dic = {}
colored = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
i = 1 # skip label 0 as it corresponds to the backgground points
sum_of_cc_points = 0
while i < no_ccs:
label_pnts_dic[i] = np.where(labels == i) #where return tuple(list of x cords, list of y cords)
colored[label_pnts_dic[i]] = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
i +=1
cv2.imwrite(output_dir + '3-colored.png', colored)
print ("First ten points of label-1 cc: ")
for i in range(10):
print ("x: ", label_pnts_dic[1][1][i], "y: ", label_pnts_dic[1][0][i])
Output:
Total White Pixels Before Thinning = 6814
Total White Pixels After Thinning = 2065
First ten points of label-1 cc:
x: 312 y: 104
x: 313 y: 104
x: 314 y: 104
x: 315 y: 104
x: 316 y: 104
x: 317 y: 104
x: 318 y: 104
x: 319 y: 104
x: 320 y: 104
x: 321 y: 104
Images:
1.Thresholded
Thinned
Colored Components
Edit2:
After a discussion with OP, I understood that having a list of (scattered) points is not enough. Points should be ordered so that they could be traced. To achieve that new logic should be introduced after applying thinning to the image.
Find extreme points (points with a single 8-connectivity neighbor)
Find connector points (points with 3-ways connectivity)
Find simple points (all other points)
Start tracing from an extreme point until reaching another extreme point or a connector one.
Extract the traveled path.
Check whether a connector point has turned into a simple point and update its status.
Repeat
Check if there are any closed-loops of simple points that have not been reached from any extreme point, extract each closed-loop as an additional waypoint.
Code for extreme/connector/simple point classification
def filter_neighbors(ns):
i = 0
while i < len(ns):
j = i + 1
while j < len(ns):
if (ns[i][0] == ns[j][0] and abs(ns[i][1] - ns[j][1]) <= 1) or (ns[i][1] == ns[j][1] and abs(ns[i][0] - ns[j][0]) <= 1):
del ns[j]
break
j += 1
i += 1
def sort_points_types(pnts):
extremes = []
connections = []
simple = []
for i in range(pnts.shape[0]):
neighbors = []
for j in range (pnts.shape[0]):
if i == j: continue
if abs(pnts[i, 0] - pnts[j, 0]) <= 1 and abs(pnts[i, 1] - pnts[j, 1]) <= 1:#8-connectivity check
neighbors.append(pnts[j])
filter_neighbors(neighbors)
if len(neighbors) == 1:
extremes.append(pnts[i])
elif len(neighbors) == 2:
simple.append(pnts[i])
elif len(neighbors) > 2:
connections.append(pnts[i])
return extremes, connections, simple
img_path = "D:/_temp/fig.png"
output_dir = 'D:/_temp/'
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
_, img = cv2.threshold(img, 128, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)
img = cv2.ximgproc.thinning(img)
pnts = cv2.findNonZero(img)
pnts = np.squeeze(pnts)
ext, conn, simple = sort_points_types(pnts)
for p in conn:
cv2.circle(img, (p[0], p[1]), 5, 128)
for p in ext:
cv2.circle(img, (p[0], p[1]), 5, 128)
cv2.imwrite(output_dir + "6-both.png", img)
print (len(ext), len(conn), len(simple))
Edit3:
A much more efficient implementation for classifying the points in a single pass by checking neighbors in a kernel-like way, thanks to eldesgraciado!
Note: Before calling this method the image should be padded with one pixel to avoid border checks or equivalently blackout pixels at the border.
def sort_points_types(pnts, img):
extremes = []
connections = []
simple = []
for p in pnts:
x = p[0]
y = p[1]
n = []
if img[y - 1,x] > 0: n.append((y-1, x))
if img[y - 1,x - 1] > 0: n.append((y-1, x - 1))
if img[y - 1,x + 1] > 0: n.append((y-1, x + 1))
if img[y,x - 1] > 0: n.append((y, x - 1))
if img[y,x + 1] > 0: n.append((y, x + 1))
if img[y + 1,x] > 0: n.append((y+1, x))
if img[y + 1,x - 1] > 0: n.append((y+1, x - 1))
if img[y + 1,x + 1] > 0: n.append((y+1, x + 1))
filter_neighbors(n)
if len(n) == 1:
extremes.append(p)
elif len(n) == 2:
simple.append(p)
elif len(n) > 2:
connections.append(p)
return extremes, connections, simple
An image visualizing extreme and connector points:
def solution(number):
x = 0
total = 0
while x < number:
if x % 3 == 0 or x % 5 == 0:
total = total + x
x = x + 1
print total
return total
solution(10)
Hello, when I run this code through IDE nothing happens.
What's wrong with it? There are no errors or anything.
I think you enter an infinite loop due to x incremental issue.
def solution(number):
x = 0
total = 0
while x < number:
if x % 3 == 0 or x % 5 == 0:
total = total + x
x = x + 1
print total
return total
Just increment x independently from the if condition that may prevent its incremental.
I have a large data set where I am looking to create groups based upon cumulative sum percent of the total. I have gotten this to work by using the map function see below code. Is there a better way to do this say if I wanted to make my groups even more granular? So for example now am looking at 5% increments...what if want to look at 1 % increments. Wondering if there is another way where I don't have to explicitly enter them into my "codethem" function.
def codethem(dl):
if dl < .05 : return '5'
elif .05 < dl <= .1: return '10'
elif .1 < dl <= .15: return '15'
elif .15 < dl <= .2: return '20'
elif .2 < dl <= .25: return '25'
elif .25 < dl <= .3: return '30'
elif .3 < dl <= .35: return '35'
elif .35 < dl <= .4: return '40'
elif .4 < dl <= .45: return '45'
elif .45 < dl <= .5: return '50'
elif .5 < dl <= .55: return '55'
elif .55 < dl <= .6: return '60'
elif .6 < dl <= .65: return '65'
elif .65 < dl <= .7: return '70'
elif .7 < dl <= .75: return '75'
elif .75 < dl <= .8: return '80'
elif .8 < dl <= .85: return '85'
elif .85 < dl <= .9: return '90'
elif .9 < dl <= .95: return '95'
elif .95 < dl <= 1: return '100'
else: return 'None'
my_df['code'] = my_df['sales_csum_aspercent'].map(code them)
Thank you!
there is a special method for that - pd.cut()
Demo:
create random DF:
In [393]: df = pd.DataFrame({'a': np.random.rand(10)})
In [394]: df
Out[394]:
a
0 0.860256
1 0.399267
2 0.209185
3 0.773647
4 0.294845
5 0.883161
6 0.985758
7 0.559730
8 0.723033
9 0.126226
we should specify bins when calling pd.cut():
In [404]: np.linspace(0, 1, 11)
Out[404]: array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
In [395]: pd.cut(df.a, bins=np.linspace(0, 1, 11))
Out[395]:
0 (0.8, 0.9]
1 (0.3, 0.4]
2 (0.2, 0.3]
3 (0.7, 0.8]
4 (0.2, 0.3]
5 (0.8, 0.9]
6 (0.9, 1]
7 (0.5, 0.6]
8 (0.7, 0.8]
9 (0.1, 0.2]
Name: a, dtype: category
Categories (10, object): [(0, 0.1] < (0.1, 0.2] < (0.2, 0.3] < (0.3, 0.4] ... (0.6, 0.7] < (0.7, 0.8] < (0.8, 0.9] < (0.9, 1]]
if we want to have a custom labels, we should explicitly specify them:
In [401]: bins = np.linspace(0,1, 11)
NOTE: bin labels must be one fewer than the number of bin edges
In [402]: labels = (bins[1:]*100).astype(int)
In [412]: labels
Out[412]: array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
In [403]: pd.cut(df.a, bins=bins, labels=labels)
Out[403]:
0 90
1 40
2 30
3 80
4 30
5 90
6 100
7 60
8 80
9 20
Name: a, dtype: category
Categories (10, int64): [10 < 20 < 30 < 40 ... 70 < 80 < 90 < 100]
Lets do it with the 5% step
In [419]: bins = np.linspace(0, 1, 21)
In [420]: bins
Out[420]: array([ 0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.8
5, 0.9 , 0.95, 1. ])
In [421]: labels = (bins[1:]*100).astype(int)
In [422]: labels
Out[422]: array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100])
In [423]: pd.cut(df.a, bins=bins, labels=labels)
Out[423]:
0 90
1 40
2 25
3 80
4 30
5 90
6 100
7 60
8 75
9 15
Name: a, dtype: category
Categories (20, int64): [5 < 10 < 15 < 20 ... 85 < 90 < 95 < 100]
I'm currently going through Real Python's pygame primer and I can't seem to render the player sprite. Here's the code I have:
import pygame
import random
from pygame.locals import *
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((50, 50))
self.surf.fill((225, 225, 225))
self.rect = self.surf.get_rect()
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right < 800:
self.rect.right = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom < 600:
self.rect.bottom = 600
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.Surface((20,10))
self.surf.fill((225, 225, 225))
self.rect = self.surf.get_rect(center= (820, random.randint(0, 600)))
self.speed = random.randint(1, 2)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
self.kill()
pygame.init()
screen = pygame.display.set_mode((800, 600))
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 500)
player = Player()
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE or event.type == QUIT:
running = False
elif(event.type == ADDENEMY):
new_enemy = Enemy()
enemies.add(new_enemy)
all_sprites.add(new_enemy)
screen.blit(background, (0, 0))
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
enemies.update()
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
if pygame.sprite.spritecollideany(player, enemies):
player.kill()
pygame.display.flip()
This is almost exactly what they have as an example in the tutorial (I made a few tweaks in values), but when I run the program the player sprite is not rendered. I'm not sure why because it is clearly added to my all_sprites group that gets blit every loop. I've gone through the code forwards and backwards and can't find the issue. This is my first question so please let me know what other information to include next time.
Thanks!
If you can't see your sprites on the screen it's sometimes useful to print out the current position by simply calling
print self.rect
inside the player's update() method.
This currently this yields you
<rect(-50, 550, 50, 50)>
which represents the x, y, width, and height of the rect and ie. of the sprite.
Because the update() method of the Player class is the only part in your program where you set this parameters, you know that there might be some errors: Instead of setting the pygame.Rect attributes left, right, top and bottom to the maximum screen size to keep them showing on the users display you positioned the player at -50, 550 from the sett-up.
This is because you alternated < and > and mixed up some numbers:
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right < 800:
self.rect.right = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom < 600:
self.rect.bottom = 600
should be
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 800:
self.rect.right = 800
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > 600:
self.rect.bottom = 600
Now your program should run as expected.