Code cleanup
Update GameObject class Added some functions to User class Updated gameending highscore upload to database
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import pygame
|
||||
import random as rand
|
||||
|
||||
class GameObject():
|
||||
__color = (0,0,0)
|
||||
@@ -13,6 +14,7 @@ class GameObject():
|
||||
__is_player = False
|
||||
__is_rect = False
|
||||
__has_follower = False
|
||||
__score:int
|
||||
count = 0
|
||||
|
||||
__movedirection = {"up":False, "down":False, "left":False, "right":False}
|
||||
@@ -34,6 +36,7 @@ class GameObject():
|
||||
self.__pos_lf_y = 0
|
||||
self.__surface = surface
|
||||
self.__mainscreensize = mainscreensize
|
||||
self.__score = 0
|
||||
|
||||
#self.__rectobjekt = rect
|
||||
|
||||
@@ -133,6 +136,9 @@ class GameObject():
|
||||
def get_playermovedirection(self):
|
||||
return self.__movedirection
|
||||
|
||||
def get_player_score(self) -> int:
|
||||
return self.__score
|
||||
|
||||
def has_follower(self):
|
||||
return self.__has_follower
|
||||
|
||||
@@ -155,6 +161,11 @@ class GameObject():
|
||||
self.__pos_lf_x = x
|
||||
self.__pos_lf_y = y
|
||||
|
||||
def update_player_score(self, score_from_entity):
|
||||
if(score_from_entity > 0):
|
||||
self.__score += score_from_entity
|
||||
|
||||
|
||||
def collide(self, GameObject) -> bool:
|
||||
#print(GameObject)
|
||||
#print(GameObject.get_rect())
|
||||
@@ -175,9 +186,11 @@ class GameObject():
|
||||
|
||||
|
||||
|
||||
class Follower(GameObject):
|
||||
class Block(GameObject):
|
||||
__score = int()
|
||||
def __init__(self, name: str, surface, mainscreensize: tuple, posx_init: int = 0, posy_init: int = 0, move_speed: int = 0, is_player: bool = False, is_rect: bool = True, rect_size: tuple = ...) -> None:
|
||||
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, rect_size)
|
||||
self.__score = rand.randint(5, 50)
|
||||
|
||||
def set_position_player(self, pos:tuple, playermovedirection:list, debug=False):
|
||||
if(playermovedirection["up"]):
|
||||
@@ -193,4 +206,7 @@ class Follower(GameObject):
|
||||
super().set_position((pos[0]-super().get_rect_size()[0],pos[1]))
|
||||
print(pos)
|
||||
def test(self):
|
||||
print(super().get_position())
|
||||
print(super().get_position())
|
||||
|
||||
def get_score(self) -> int:
|
||||
return self.__score
|
||||
@@ -77,7 +77,6 @@ class User:
|
||||
row = c.execute(sql)
|
||||
res = row.fetchone()
|
||||
c.execute(sql_update_login_date)
|
||||
|
||||
self.__sqlhandle.commit()
|
||||
self.__uid = res[0]
|
||||
self.__username = res[1]
|
||||
@@ -90,6 +89,26 @@ class User:
|
||||
|
||||
return True
|
||||
|
||||
def update_user_highscore(self, score:int):
|
||||
"""Updates users highscore"""
|
||||
sql = "SELECT * FROM user WHERE username ='"+self.getusername()+"'"
|
||||
sql_update_highscore = "UPDATE user SET highscore = "+str(score)+" WHERE username = '"+self.getusername()+"'"
|
||||
try:
|
||||
c = self.__sqlhandle.cursor()
|
||||
row = c.execute(sql)
|
||||
res = row.fetchone()
|
||||
c.execute(sql_update_highscore)
|
||||
self.__sqlhandle.commit()
|
||||
self.__uid = res[0]
|
||||
self.__username = res[1]
|
||||
self.__highscore = res[2]
|
||||
self.__lastlogin = res[3]
|
||||
|
||||
except:
|
||||
print("Fehler beim Update des Highscores")
|
||||
return False
|
||||
return True
|
||||
|
||||
def getplayed_time(self):
|
||||
return self.__played_time
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ count_follower = 0
|
||||
|
||||
def append_follwer(x,y):
|
||||
print("x: "+str(x), "y: "+str(y))
|
||||
newfollower = Follower("Follower "+str(count_follower), MAINSCREEN, MAINSCREEN_SIZE, is_rect=True, rect_size=(x,y))
|
||||
newfollower = Block("Follower "+str(count_follower), MAINSCREEN, MAINSCREEN_SIZE, is_rect=True, rect_size=(x,y))
|
||||
colided_objects.append(newfollower)
|
||||
|
||||
User = User.User()
|
||||
@@ -67,7 +67,7 @@ def spawn_entities(x:int, list_of_objects:list):
|
||||
count = 0
|
||||
for y in range(x):
|
||||
rect_size = 60
|
||||
Entity = GameObject("Entitiy-"+str(count), MAINSCREEN, MAINSCREEN_SIZE, rand.randint(1,MAINSCREEN_SIZE[0]), rand.randint(1,MAINSCREEN_SIZE[1]), rand.randint(1,15), is_rect=True, rect_size=(rect_size,rect_size))
|
||||
Entity = Block("Entitiy-"+str(count), MAINSCREEN, MAINSCREEN_SIZE, rand.randint(1,MAINSCREEN_SIZE[0]), rand.randint(1,MAINSCREEN_SIZE[1]), rand.randint(1,15), is_rect=True, rect_size=(rect_size,rect_size))
|
||||
Entity.get_name(True)
|
||||
list_of_objects.append(Entity)
|
||||
count += 1
|
||||
@@ -109,13 +109,13 @@ while active == True:
|
||||
Player.change_direction("left")
|
||||
|
||||
Player.draw()
|
||||
Player.update_pos_lastframe(Player.get_position()[0], Player.get_position()[1], True)
|
||||
Player.update_pos_lastframe(Player.get_position()[0], Player.get_position()[1], False)
|
||||
Player.move(5)
|
||||
print("Aktuelle Position: " + str(Player.get_position()))
|
||||
#print("Aktuelle Position: " + str(Player.get_position()))
|
||||
#print("Aktuell kollidierte Objekte: ", str(count_hidden_entities))
|
||||
#Spawns the Entities
|
||||
for y in spawned_entities:
|
||||
y:GameObject
|
||||
y:Block
|
||||
y.draw()
|
||||
if(y.is_collided() == True):
|
||||
#Der erste Follower folgt dem Spieler, alle übrigen folgen den vorherien Verfolgern
|
||||
@@ -123,6 +123,9 @@ while active == True:
|
||||
pass
|
||||
if(y.is_collided() == False):
|
||||
if(y.collide(Player)):
|
||||
print(F"Score: {y.get_score()}")
|
||||
Player.update_player_score(y.get_score())
|
||||
print(F"Aktueller Playerscore: {Player.get_player_score()}")
|
||||
count_hidden_entities += 1
|
||||
xy = y.get_rect_size()
|
||||
append_follwer(xy[0], xy[1])
|
||||
@@ -137,10 +140,10 @@ while active == True:
|
||||
|
||||
if(colided_objects):
|
||||
pass
|
||||
|
||||
"""
|
||||
#TODO:Berechne Position follower Objekte anhand Position aus letztem Frame
|
||||
for follower in colided_objects:
|
||||
follower:Follower
|
||||
follower:Block
|
||||
if(i >= 0):
|
||||
colided_objects[0].set_position_player(Player.get_position(), Player.get_playermovedirection(), True)
|
||||
pass
|
||||
@@ -148,12 +151,15 @@ while active == True:
|
||||
#colided_objects[i].set_position_player(follower.set_position(colided_objects[i-1].get_position()), Player.get_playermovedirection(), True)
|
||||
pass
|
||||
follower.draw()
|
||||
i += 1
|
||||
i += 1"""
|
||||
|
||||
|
||||
|
||||
if(count_hidden_entities == count_spawend_enities):
|
||||
print("WIN!!!")
|
||||
if(Player.get_player_score() > User.gethighscore()):
|
||||
print("Neuner Highscore!!!")
|
||||
User.update_user_highscore(Player.get_player_score())
|
||||
menu.enable()
|
||||
menu.add.label("WIN", "win", 10)
|
||||
menu.mainloop(MAINSCREEN)
|
||||
|
||||
Reference in New Issue
Block a user