moved position system over to use vector2
This commit is contained in:
parent
4acb99cc84
commit
8c544556ed
55
Game_Client/Classes/Objects/BoardField.py
Normal file
55
Game_Client/Classes/Objects/BoardField.py
Normal file
@ -0,0 +1,55 @@
|
||||
import pygame
|
||||
|
||||
class BoardField():
|
||||
__name:str
|
||||
__type:str
|
||||
__pos:pygame.Vector2
|
||||
__size:tuple
|
||||
__color:tuple = (255,255,255)
|
||||
_rect:pygame.Rect
|
||||
|
||||
def __init__(self, name:str, type:str, pos:pygame.Vector2, size:tuple, color:tuple):
|
||||
self.__name = name
|
||||
self.__type = type
|
||||
self.__pos = pos
|
||||
self.__size = size
|
||||
self.__color = color
|
||||
self.__rect = pygame.Rect(pos.x, pos.y, size[0], size[1])
|
||||
|
||||
def getName(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def getType(self) -> str:
|
||||
return self.__type
|
||||
|
||||
def getPos(self) -> pygame.Vector2:
|
||||
return self.__pos
|
||||
|
||||
def getSize(self) -> tuple:
|
||||
return self.__size
|
||||
|
||||
def getColor(self) -> tuple:
|
||||
return self.__color
|
||||
|
||||
def getRect(self) -> pygame.Rect:
|
||||
return self.__rect
|
||||
|
||||
def setName(self, name:str) -> str:
|
||||
self.__name = name
|
||||
return self.__name
|
||||
|
||||
def setType(self,type:str) -> str:
|
||||
self.__type = type
|
||||
return self.__type
|
||||
|
||||
def setPos(self, pos:pygame.Vector2) -> pygame.Vector2:
|
||||
self.pos = pos
|
||||
return self.__pos
|
||||
|
||||
def setSize(self, size:tuple) -> tuple:
|
||||
self.__size = size
|
||||
return self.__size
|
||||
|
||||
def setColor(self, color:tuple) -> tuple:
|
||||
self.__color = color
|
||||
return self.__color
|
Binary file not shown.
53
Game_Client/Classes/Objects/World.py
Normal file
53
Game_Client/Classes/Objects/World.py
Normal file
@ -0,0 +1,53 @@
|
||||
import pygame
|
||||
from Classes.Objects.BoardField import BoardField
|
||||
|
||||
class World():
|
||||
__boardFields:list
|
||||
__cardWidth:int = 200
|
||||
__cardHeight:int = 250
|
||||
__cardOffset:int = 400
|
||||
|
||||
def __init__(self, cardWidth:int=200, cardHeight:int=250, cardOffset:int=400):
|
||||
self.__boardFields = []
|
||||
self.__cardWidth = cardWidth
|
||||
self.__cardHeight = cardHeight
|
||||
self.__cardOffset = cardOffset
|
||||
self.buildGameWorld()
|
||||
|
||||
def buildGameWorld(self):
|
||||
# TODO: rebuild these to use the BoardField Class and to append them to the __boardFields list
|
||||
# construct elements arround the playerfield
|
||||
# Todo add lifepoint label for player and enemy and make them scriptable
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), 15)
|
||||
self.__boardFields.append(BoardField("EnemyDeck", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), (self.__cardHeight + 25))
|
||||
self.__boardFields.append(BoardField("EnemyGraveyard", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + 60))
|
||||
self.__boardFields.append(BoardField("PlayerDeck", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
||||
self.__boardFields.append(BoardField("PlayerGraveyard", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
||||
|
||||
for i in range(5):
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + 60))
|
||||
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i),"MonsterField",pVector,(self.__cardWidth, self.__cardHeight),(255,255,255)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
||||
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i), "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1)), 15), (self.__cardWidth, self.__cardHeight))
|
||||
self.__boardFields.append(BoardField("EnemyMonsterField-"+str(i), "MonsterField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||
|
||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1))), (self.__cardHeight + 25))
|
||||
self.__boardFields.append(BoardField("EnemySpellTrapField-"+str(i), "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||
|
||||
def getBoardFields(self) -> list:
|
||||
return self.__boardFields
|
||||
|
||||
def getCardWidth(self) -> int:
|
||||
return self.__cardWidth
|
||||
|
||||
def getCardHeifht(self) -> int:
|
||||
return self.__cardHeight
|
Binary file not shown.
BIN
Game_Client/Classes/Objects/__pycache__/World.cpython-312.pyc
Normal file
BIN
Game_Client/Classes/Objects/__pycache__/World.cpython-312.pyc
Normal file
Binary file not shown.
@ -4,8 +4,7 @@ from pygame.locals import *
|
||||
from Classes.Objects.Cards.MonsterCard import MonsterCard
|
||||
from Classes.System.Window import Window
|
||||
from Classes.System.InputHandler import InputHandler
|
||||
from Classes.System.World import World
|
||||
|
||||
from Classes.Objects.World import World
|
||||
|
||||
class App:
|
||||
|
||||
@ -13,9 +12,19 @@ class App:
|
||||
__running:bool = True
|
||||
__FPS = 60
|
||||
__clock = pygame.time.Clock()
|
||||
__myFont:pygame.font
|
||||
__world:World
|
||||
__inputHandler: InputHandler
|
||||
|
||||
def __init__(self, width:int=1920, height:int=1080, title:str="default title"):
|
||||
pygame.font.init()
|
||||
self.__myFont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
self.__window = Window(width=width, height=height, title=title)
|
||||
self.__inputHandler = InputHandler()
|
||||
|
||||
# game word
|
||||
self.__world = World()
|
||||
|
||||
self.startGameLoop()
|
||||
self.onCleanup()
|
||||
|
||||
@ -24,60 +33,17 @@ class App:
|
||||
# create sprite groups
|
||||
cards = pygame.sprite.Group()
|
||||
|
||||
# game word
|
||||
# the field
|
||||
playerMonsterGroup = []
|
||||
playerSpellGroup = []
|
||||
enemyMonsterGroup = []
|
||||
ememySpellGroup = []
|
||||
enemyOtherGroup = []
|
||||
playerOtherGroup = []
|
||||
|
||||
cardWith = 200
|
||||
cardHeight = 250
|
||||
cardOffset = 400
|
||||
|
||||
# player card fields
|
||||
for i in range(5):
|
||||
playerMonsterGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * i), (2 * cardHeight) + 60, cardWith, cardHeight))
|
||||
playerSpellGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * i), (2 * cardHeight) + cardHeight + 70, cardWith, cardHeight))
|
||||
|
||||
enemyMonsterGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * (i+1)), 15, cardWith, cardHeight))
|
||||
ememySpellGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * (i+1)), cardHeight + 25, cardWith, cardHeight))
|
||||
|
||||
enemyOtherGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * 0), 15, cardWith, cardHeight))
|
||||
enemyOtherGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * 0), cardHeight + 25, cardWith, cardHeight))
|
||||
|
||||
playerOtherGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * 5), (2*cardHeight) + 60, cardWith, cardHeight))
|
||||
playerOtherGroup.append(pygame.Rect(cardOffset + ((cardWith + 10) * 5), (2*cardHeight) + cardHeight + 70, cardWith, cardHeight))
|
||||
|
||||
testMonsterCard = MonsterCard((500,500), "Assets/Cards/MonsterCards/testmonstercard/")
|
||||
cards.add(testMonsterCard)
|
||||
|
||||
while self.__running:
|
||||
|
||||
self.__clock.tick(self.__FPS)
|
||||
|
||||
self.__window.getScreen().fill((0,0,0))
|
||||
# world.draw(self.__window.getScreen())
|
||||
|
||||
for card in playerOtherGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (0,255,0), card)
|
||||
|
||||
for card in playerMonsterGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (255,255,255), card)
|
||||
|
||||
for card in playerSpellGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (255,255,255), card)
|
||||
|
||||
for card in enemyOtherGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (255,0,0), card)
|
||||
|
||||
for card in enemyMonsterGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (255,0,255), card)
|
||||
|
||||
for card in ememySpellGroup:
|
||||
pygame.draw.rect(self.__window.getScreen(), (255,0,255), card)
|
||||
# render world
|
||||
self.__inputHandler.getMouseHover(self.__world)
|
||||
self.__window.drawWorld(self.__world)
|
||||
|
||||
# update sprite groups
|
||||
cards.update()
|
||||
@ -96,6 +62,10 @@ class App:
|
||||
for event in events:
|
||||
if event.type == pygame.QUIT:
|
||||
self.onCleanup()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
pass
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
pass
|
||||
|
||||
# sets the running state for the gameloop
|
||||
def setRunning(self, running:bool):
|
||||
|
@ -1,5 +1,8 @@
|
||||
import pygame
|
||||
|
||||
from Classes.Objects.World import World
|
||||
from Classes.Objects.BoardField import BoardField
|
||||
|
||||
class InputHandler:
|
||||
# returns pressed key
|
||||
def getPressed():
|
||||
@ -22,3 +25,21 @@ class InputHandler:
|
||||
|
||||
return tuple((xvel, yvel))
|
||||
|
||||
def getMousePos(self) -> pygame.Vector2:
|
||||
return pygame.Vector2(pygame.mouse.get_pos())
|
||||
|
||||
# get field under mousbutton
|
||||
def getMouseHover(self, world:World) -> BoardField:
|
||||
mouse_pos = self.getMousePos()
|
||||
xPos = [int(v//world.getCardWidth()) for v in mouse_pos.x]
|
||||
yPos = [int(v//world.getCardHeifht()) for v in mouse_pos.y]
|
||||
|
||||
try:
|
||||
for field in world.getBoardFields():
|
||||
for x in xPos:
|
||||
for y in yPos:
|
||||
if x == field.getPos().x and y == field.getPos().y:
|
||||
return field
|
||||
except IndexError: pass
|
||||
|
||||
return None
|
@ -1,6 +1,9 @@
|
||||
import pygame
|
||||
from pygame.locals import *
|
||||
|
||||
from Classes.Objects.BoardField import BoardField
|
||||
from Classes.Objects.World import World
|
||||
|
||||
class Window:
|
||||
__width:int = 800
|
||||
__height:int = 600 # takes 80% of width which tranlates to 640
|
||||
@ -38,3 +41,8 @@ class Window:
|
||||
# draws a passed sprite group to the screen
|
||||
def drawSpriteGroup(self, group:pygame.sprite.Group):
|
||||
group.draw(self.__screen)
|
||||
|
||||
# draws a given group of rectangles onto the screen
|
||||
def drawWorld(self, world:World):
|
||||
for field in world.getBoardFields():
|
||||
pygame.draw.rect(self.__screen, field.getColor(), field.getRect())
|
@ -1,9 +0,0 @@
|
||||
import pygame
|
||||
|
||||
class World():
|
||||
|
||||
def __init__(self, mapImage):
|
||||
self.__map = mapImage
|
||||
|
||||
def draw(self, surface:pygame.Surface):
|
||||
surface.blit(self.__map, (0,0))
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Game_Client/Classes/System/__pycache__/World.cpython-312.pyc
Normal file
BIN
Game_Client/Classes/System/__pycache__/World.cpython-312.pyc
Normal file
Binary file not shown.
115
ref.py
Normal file
115
ref.py
Normal file
@ -0,0 +1,115 @@
|
||||
import pygame
|
||||
|
||||
TILESIZE = 32
|
||||
BOARD_POS = (10, 10)
|
||||
|
||||
def create_board_surf():
|
||||
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
|
||||
dark = False
|
||||
for y in range(8):
|
||||
for x in range(8):
|
||||
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
|
||||
pygame.draw.rect(board_surf, pygame.Color('darkgrey' if dark else 'beige'), rect)
|
||||
dark = not dark
|
||||
dark = not dark
|
||||
return board_surf
|
||||
|
||||
def get_square_under_mouse(board):
|
||||
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
|
||||
x, y = [int(v // TILESIZE) for v in mouse_pos]
|
||||
try:
|
||||
if x >= 0 and y >= 0: return (board[y][x], x, y)
|
||||
except IndexError: pass
|
||||
return None, None, None
|
||||
|
||||
def create_board():
|
||||
board = []
|
||||
for y in range(8):
|
||||
board.append([])
|
||||
for x in range(8):
|
||||
board[y].append(None)
|
||||
|
||||
for x in range(0, 8):
|
||||
board[1][x] = ('black', 'pawn')
|
||||
for x in range(0, 8):
|
||||
board[6][x] = ('white', 'pawn')
|
||||
|
||||
return board
|
||||
|
||||
def draw_pieces(screen, board, font, selected_piece):
|
||||
sx, sy = None, None
|
||||
if selected_piece:
|
||||
piece, sx, sy = selected_piece
|
||||
|
||||
for y in range(8):
|
||||
for x in range(8):
|
||||
piece = board[y][x]
|
||||
if piece:
|
||||
selected = x == sx and y == sy
|
||||
color, type = piece
|
||||
s1 = font.render(type[0], True, pygame.Color('red' if selected else color))
|
||||
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
|
||||
pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE+1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
|
||||
screen.blit(s2, s2.get_rect(center=pos.center).move(1, 1))
|
||||
screen.blit(s1, s1.get_rect(center=pos.center))
|
||||
|
||||
def draw_selector(screen, piece, x, y):
|
||||
if piece != None:
|
||||
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
|
||||
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
|
||||
|
||||
def draw_drag(screen, board, selected_piece, font):
|
||||
if selected_piece:
|
||||
piece, x, y = get_square_under_mouse(board)
|
||||
if x != None:
|
||||
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
|
||||
pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)
|
||||
|
||||
color, type = selected_piece[0]
|
||||
s1 = font.render(type[0], True, pygame.Color(color))
|
||||
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
|
||||
pos = pygame.Vector2(pygame.mouse.get_pos())
|
||||
screen.blit(s2, s2.get_rect(center=pos + (1, 1)))
|
||||
screen.blit(s1, s1.get_rect(center=pos))
|
||||
selected_rect = pygame.Rect(BOARD_POS[0] + selected_piece[1] * TILESIZE, BOARD_POS[1] + selected_piece[2] * TILESIZE, TILESIZE, TILESIZE)
|
||||
pygame.draw.line(screen, pygame.Color('red'), selected_rect.center, pos)
|
||||
return (x, y)
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
font = pygame.font.SysFont('', 32)
|
||||
screen = pygame.display.set_mode((640, 480))
|
||||
board = create_board()
|
||||
board_surf = create_board_surf()
|
||||
clock = pygame.time.Clock()
|
||||
selected_piece = None
|
||||
drop_pos = None
|
||||
while True:
|
||||
piece, x, y = get_square_under_mouse(board)
|
||||
events = pygame.event.get()
|
||||
for e in events:
|
||||
if e.type == pygame.QUIT:
|
||||
return
|
||||
if e.type == pygame.MOUSEBUTTONDOWN:
|
||||
if piece != None:
|
||||
selected_piece = piece, x, y
|
||||
if e.type == pygame.MOUSEBUTTONUP:
|
||||
if drop_pos:
|
||||
piece, old_x, old_y = selected_piece
|
||||
board[old_y][old_x] = 0
|
||||
new_x, new_y = drop_pos
|
||||
board[new_y][new_x] = piece
|
||||
selected_piece = None
|
||||
drop_pos = None
|
||||
|
||||
screen.fill(pygame.Color('grey'))
|
||||
screen.blit(board_surf, BOARD_POS)
|
||||
draw_pieces(screen, board, font, selected_piece)
|
||||
draw_selector(screen, piece, x, y)
|
||||
drop_pos = draw_drag(screen, board, selected_piece, font)
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user