-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
157 lines (125 loc) · 3.9 KB
/
game.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# import the pygame module, so you can use it
import pygame
from pygame.locals import *
from circle import Circle
from ocr import ocr_core
import math
SPEED = 5
WHITE = (254, 254, 254)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
(width, height) = (500, 500)
collision = False
# define a main function
def main():
global screen
global trace_sprites
global hands_sprites
global word
word = []
radius = 20
# initialize the pygame module
pygame.init()
pygame.display.set_caption("ImplementAI")
# create a surface on screen
screen = pygame.display.set_mode((width, height))
screen.fill(WHITE)
# set up first frame
lefthand = Circle(((1/3)*width, height/2), BLUE)
righthand = Circle(((2/3)*width, height/2), RED)
#This will be a list that will contain all the sprites we intend to use in our game.
hands_sprites = pygame.sprite.Group()
trace_sprites = pygame.sprite.Group()
# Add the car to the list of objects
hands_sprites.add(lefthand)
hands_sprites.add(righthand)
# main loop
running = True
while running:
# event handling, gets all event from the event queue
for event in pygame.event.get():
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
screenshot()
guess()
# change the value to False, to exit the main loop
running = False
updateTrace(righthand)
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
righthand.moveUp()
if keys[pygame.K_DOWN]:
righthand.moveDown()
if keys[pygame.K_RIGHT]:
righthand.moveRight()
if keys[pygame.K_LEFT]:
righthand.moveLeft()
if keys[pygame.K_w]:
lefthand.moveUp()
if keys[pygame.K_s]:
lefthand.moveDown()
if keys[pygame.K_d]:
lefthand.moveRight()
if keys[pygame.K_a]:
lefthand.moveLeft()
if keys[pygame.K_SPACE]:
trace_sprites = pygame.sprite.Group()
if keys[pygame.K_RETURN]:
word.append(guess())
print('word: ' + "".join(word))
trace_sprites = pygame.sprite.Group()
collisionDetection(lefthand, righthand)
#Game Logic
hands_sprites.update()
trace_sprites.update()
#Drawing on Screen
screen.fill(WHITE)
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
trace_sprites.draw(screen)
hands_sprites.draw(screen)
#Refresh Screen
pygame.display.flip()
def collisionDetection(hand1, hand2):
global collision
x1 = hand1.rect.x
x2 = hand2.rect.x
y1 = hand1.rect.y
y2 = hand2.rect.y
max = hand1.radius + hand2.radius
distance = math.sqrt((x1-x2)**2 + (y1-y2)**2)
if distance <= max:
if not collision:
collision = True
hand2.toggleTrace()
if not hand2.traceOn:
screenshot()
guess()
else:
if collision:
collision = False
def screenshot():
#Game Logic
hands_sprites.update()
trace_sprites.update()
#Drawing on Screen
screen.fill(WHITE)
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
trace_sprites.draw(screen)
#Refresh Screen
pygame.display.flip()
pygame.image.save(screen, 'screenshot.jpeg')
def guess():
guess = ocr_core('screenshot.jpeg')
print('guess: ' + guess)
return guess
def updateTrace(hand):
if hand.traceOn:
newCircle = Circle(hand.getPos(), BLACK)
trace_sprites.add(newCircle)
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()