-
Notifications
You must be signed in to change notification settings - Fork 1
/
Field.py
102 lines (85 loc) · 3.27 KB
/
Field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# coding: utf-8
import pygame
import sys
from pygame.locals import *
from Rules import *
from functions import *
class Field():
"""Create a 3x3 field"""
def __init__(self, width, height, (cardWidth, cardHeight), boss):
self.boss = boss
self.x = width / 7
self.y = height / 7
self.width = width * 5 / 7
self.height = height * 4 / 5
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
self.surface = pygame.Surface((self.width, self.height), SRCALPHA)
x = 92
y = 60
self.positions = [
# First line
(2.53 * x, 1.6 * y), (3.75 * x, 1.6 * y), (4.98 * x, 1.6 * y),
# Second line
(2.53 * x, 4.0 * y), (3.75 * x, 4.0 * y), (4.98 * x, 4.0 * y),
# Third line
(2.53 * x, 6.4 * y), (3.75 * x, 6.4 * y), (4.98 * x, 6.4 * y)
]
self.elementName = []
self.elements = []
self.elementSound = []
elementary(self)
self.drawElements()
self.fieldRects = []
self.fieldSurf = []
lineField = []
for i in self.positions:
lineField.append(pygame.Rect(i, (cardWidth, cardHeight)))
if len(lineField) == 3:
self.fieldRects.append(lineField)
lineField = []
def squareClicked(self):
coords = pygame.mouse.get_pos()
if self.rect.collidepoint(coords):
for line in range(len(self.fieldRects)):
for square in range(len(self.fieldRects[line])):
if self.fieldRects[line][square].collidepoint(coords):
return line * 3 + square
return -1
def saveState(self):
"""Allow to save the state of the game, to play later or for IA to
to know the situation"""
self.state = []
self.line = []
index = 0
filled = 0
for line in self.fieldRects:
for rect in line:
filled = 0
for card in self.boss.player1Hand.cards:
if card.rect.topleft == rect.topleft:
self.line.append(card)
filled = 1
for card in self.boss.player2Hand.cards:
if card.rect.topleft == rect.topleft:
self.line.append(card)
filled = 1
if not filled:
self.line.append(None)
self.state.append(self.line)
self.line = []
def drawElements(self):
"""Will draw the elements on the Field"""
for i in range(9):
if self.elementName[i] != None:
elementSurf, elementRect = loadElement(self.elementName[i])
elementRect.topright= \
self.positions[i]
elementRect.move_ip(-40,-30)
self.elements.append((elementSurf, elementRect))
else:
self.elements.append(None)
self.update()
def update(self):
for elem in self.elements:
if elem != None:
self.surface.blit(elem[0] ,elem[1])