Related
I've been working on a piece of code that compares two images and tells me if they are similar, it also tells me which pixels are different in the image, after this it plots them into a pygame screen so that I can see which parts of the image are moving more clearly. The only problem is that it seems as if pygame cannot handle it or something and it crashes, no errors appear.
code:
import cv2
import pygame
from pygame.locals import *
lib = 'Map1.png'
lib2 = 'Map2.png'
lib3 = []
coordenatesx = []
coordenatesy = []
Read = list(cv2.imread(lib).astype("int"))
Read2 = list(cv2.imread(lib2).astype("int"))
counter = 0
for i in range(len(Read)):#y coords
for j in range(len(Read[i])):#x coords
blue = list(Read[i][j])[0]
green = list(Read[i][j])[1]
red = list(Read[i][j])[2]
blue2 = list(Read2[i][j])[0]
green2 = list(Read2[i][j])[1]
red2 = list(Read2[i][j])[2]
difference = (blue+green+red)-(blue2+green2+red2)
lib3.append(difference)
if difference <= 10 and difference >= -10:
counter+=1
coordenatesx.append(j)
coordenatesy.append(i)
if counter >= (i*j)*0.75:
print('They are similar images')
print('They are different by:', str((counter / (i * j)) * 100), '%')
else:
print('They are different')
print('They are different by:', str((counter / (i * j)) * 100), '%')
pygame.init()
screen = pygame.display.set_mode((500,500))
while 1:
screen.fill((20))
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
for l in range(len(coordenatesx)):
for v in range(len(coordenatesy)):
pygame.draw.rect(screen, (blue, red, green), pygame.Rect(coordenatesx[l], coordenatesy[v], 1, 1))
pygame.display.update()
image1:
image2:
Pygame didn't crash. You know how defining a Pygame window without calling the pygame.event.get() method would cause problems, right? Well, when you put
for l in range(len(coordenatesx)):
for v in range(len(coordenatesy)):
pygame.draw.rect(screen, (blue, red, green), pygame.Rect(coordenatesx[l], coordenatesy[v], 1, 1))
into the while loop that's supposed to constantly call the pygame.event.get() method, you are dramatically slowing down the looping process.
To see this with your eyes, add a print() statement into the loop, and see how slow it prints:
while 1:
screen.fill((20))
print("Looping...")
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
for l in range(len(coordenatesx)):
for v in range(len(coordenatesy)):
pygame.draw.rect(screen, (blue, red, green), pygame.Rect(coordenatesx[l], coordenatesy[v], 1, 1))
pygame.display.update()
One fix is to move the pygame.event.get() call into the nested for loop (as well as the pygame.display.update() call if you want to see the updating):
while 1:
screen.fill((20))
for l in range(len(coordenatesx)):
for v in range(len(coordenatesy)):
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
pygame.draw.rect(screen, (blue, red, green), pygame.Rect(coordenatesx[l], coordenatesy[v], 1, 1))
pygame.display.update()
Use cv2/OpenCV and NumPy. Compute the absolute difference of the images with numpy.absolute. Sum the color channels and count the non-zero pixels with numpy.count_nonzero:
import cv2
import numpy
Read = cv2.imread('Map1.png').astype("int")
Read2 = cv2.imread('Map2.png').astype("int")
diff = numpy.absolute(Read - Read2).astype("uint8")
gray = numpy.sum(diff, axis=2)
count = numpy.count_nonzero(gray)
print(f"{count} / {gray.size}")
If you don't want to import NumPy:
diff = abs(Read - Read2)
gray = (diff[:,:,0] + diff[:,:,1] + diff[:,:,2])
diff = diff.astype("uint8")
count = sum([c > 0 for r in gray for c in r])
print(f"{count} / {gray.size}")
Minimal example:
import cv2
import numpy
import pygame
from pygame.locals import *
lib = 'Map1.png'
lib2 = 'Map2.png'
Read = cv2.imread(lib).astype("int")
Read2 = cv2.imread(lib2).astype("int")
diff = numpy.absolute(Read - Read2).astype("uint8")
gray = numpy.sum(diff, axis=2)
count = numpy.count_nonzero(gray)
print(f"{count} / {gray.size}")
pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
def cv2ImageToSurface(cv2Image):
if cv2Image.dtype.name == 'uint16':
cv2Image = (cv2Image / 256).astype('uint8')
size = cv2Image.shape[1::-1]
if len(cv2Image.shape) == 2:
cv2Image = np.repeat(cv2Image.reshape(size[1], size[0], 1), 3, axis = 2)
format = 'RGB'
else:
format = 'RGBA' if cv2Image.shape[2] == 4 else 'RGB'
cv2Image[:, :, [0, 2]] = cv2Image[:, :, [2, 0]]
surface = pygame.image.frombuffer(cv2Image.flatten(), size, format)
return surface.convert_alpha() if format == 'RGBA' else surface.convert()
diff_surf = cv2ImageToSurface(diff)
run = True
while run:
clock.tick(100)
screen.fill((20))
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
run = False
screen.fill(0)
screen.blit(diff_surf, diff_surf.get_rect(center = screen.get_rect().center))
pygame.display.update()
pygame.quit()
I am trying to draw a checkerboard. However when I call the 'drawBoard' function it returns a black screen. I think the problem may be this line here:
colour = con.BOARD_COLOURS[((r+c) % 2)]
I was attempting to index the different colour values using either 0 or 1 in order to alternate the colour of the squares as they are drawn. Could any one explain where I am going wrong. Below is the full code:
#Defining constants
# RGB colours
BOARD_COLOURS = [(245, 222, 179),(34, 139, 34)]
# Dimensions
ROWS = 8
COLS = 8
DIMS = 600
SQUARE = 600 // 8
class Board:
def __init__(self):
pass
def drawBoard(self, screen):
for r in range(con.ROWS):
for c in range(con.ROWS):
colour = con.BOARD_COLOURS[((r+c) % 2)]
pg.draw.rect(screen, colour, pg.Rect(c*con.SQUARE, r*con.SQUARE, con.SQUARE, con.SQUARE))
def main():
t = 60
clock = pg.time.Clock()
interface = pg.display.set_mode((con.DIMS, con.DIMS))
pg.init()
run = True
while run:
clock.tick(t)
for ev in pg.event.get():
if ev.type == pg.QUIT:
run = False
b = Board()
b.drawBoard(interface)
pg.quit()
main()
You need to update the display. Update the display by calling either pygame.display.update() or pygame.display.flip() after drawing the scene:
def main():
t = 60
clock = pg.time.Clock()
interface = pg.display.set_mode((con.DIMS, con.DIMS))
pg.init()
b = Board()
run = True
while run:
clock.tick(t)
for ev in pg.event.get():
if ev.type == pg.QUIT:
run = False
interface.fill(0)
b.drawBoard(interface)
pygame.display.flip()
pg.quit()
main()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
To drag the objects you have to implement the MOUSEBUTTONDOWN, MOUSEBUTTONUP and MOUSEMOTION event:
def main():
t = 60
clock = pg.time.Clock()
interface = pg.display.set_mode((DIMS, DIMS))
pg.init()
pg.display.set_caption("Checkers Game")
dragitem = None
run = True
b = Board()
while run:
clock.tick(t)
for ev in pg.event.get():
if ev.type == pg.QUIT:
run = False
if ev.type == pg.MOUSEBUTTONDOWN:
col = ev.pos[0] * COLS // DIMS
row = ev.pos[1] * ROWS // DIMS
if b.board[row][col] > 1:
dragitem = (row, col, b.board[row][col])
b.board[row][col] = (row + col) % 2 == 0
dragpos = ev.pos
if ev.type == pg.MOUSEBUTTONUP:
new_col = ev.pos[0] * COLS // DIMS
new_row = ev.pos[1] * ROWS // DIMS
if b.board[new_row][new_col] < 2:
b.board[new_row][new_col] = dragitem[2]
else:
b.board[dragitem[0]][dragitem[1]] = dragitem[2]
dragpos = None
dragitem = None
if ev.type == pg.MOUSEMOTION and dragitem:
dragpos = ev.pos
interface.fill(0)
b.drawBoard(interface)
b.drawPiece(interface)
if dragitem:
pg.draw.circle(interface, PIECE_COLOUR[dragitem[2]-2], dragpos, RADIUS)
pg.display.flip()
pg.quit()
I'm trying to create a walking spider like this:
I considered using a SimpleMotor at the pink and red joints and control them using the rate function. But when I tried, I get an error that the function is not callable.
self.motorJoint1.rate(0.0) TypeError: 'float' object is not callable
I don't see any other functions in the pymunk API that allow controlling the joints at will. Is there really no function or am I missing something?
Basically in the run loop I want to specify rotations to the joints at certain points of time, to not just make the spider walk, but to eventually be able to use Neural Networks to allow it to experiment with various configurations of leg positions and figure out which ones can make it walk:
angle1 = 30
angle2 = 10
redJoint1.rotate(angle1)
pinkJoint2.rotate(angle2)
if angle1 < 50:
angle1 = angle1 + 1
Is it possible at all to achieve such a level of control over joints using Pymunk? To be able to stop moving the legs (without needing to put the body to sleep), or to rotate the leg joints to whatever angle the spider 'wishes to' at any point in time?
Sample code would be a great help.
From the servo example I took a hint and implemented this basic leg:
import sys
import pygame
from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
from pygame.color import THECOLORS
import pymunk
from pymunk import Vec2d
import pymunk.pygame_util
class Simulator(object):
def __init__(self):
self.display_flags = 0
self.display_size = (600, 600)
self.space = pymunk.Space()
self.space.gravity = (0.0, -1900.0)
#self.space.damping = 0.999 # to prevent it from blowing up.
# Pymunk physics coordinates start from the lower right-hand corner of the screen.
self.ground_y = 100
ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
ground.friction = 1.0
self.space.add(ground)
self.screen = None
self.draw_options = None
def reset_bodies(self):
for body in self.space.bodies:
if not hasattr(body, 'start_position'):
continue
body.position = Vec2d(body.start_position)
body.force = 0, 0
body.torque = 0
body.velocity = 0, 0
body.angular_velocity = 0
body.angle = body.start_angle
def draw(self):
self.screen.fill(THECOLORS["white"])### Clear the screen
self.space.debug_draw(self.draw_options)### Draw space
pygame.display.flip()### All done, lets flip the display
def main(self):
pygame.init()
self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
width, height = self.screen.get_size()
self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
def to_pygame(p):
return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
def from_pygame(p):
return to_pygame(p)
clock = pygame.time.Clock()
running = True
font = pygame.font.Font(None, 16)
# Create the spider
chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
chWd = 70; chHt = 50
chassisMass = 10
legWd_a = 50; legHt_a = 5
legWd_b = 100; legHt_b = 5
legMass = 1
relativeAnguVel = 0
#---chassis
chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
chassis_b.position = chassisXY
chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
chassis_shape.color = 200, 200, 200, 100
print("chassis position");print(chassis_b.position)
#---first left leg a
leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
leftLeg_1a_shape.color = 255, 0, 0, 100
#---first left leg b
leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
leftLeg_1b_shape.color = 0, 255, 0, 100
#---first right leg a
rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
rightLeg_1a_shape.color = 255, 0, 0, 100
#---first right leg b
rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
rightLeg_1b_shape.color = 0, 255, 0, 100
#---link left leg b with left leg a
pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
#---link left leg a with chassis
pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
#---link right leg b with right leg a
pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
#---link right leg a with chassis
pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)
self.space.add(chassis_b, chassis_shape)
self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)
#---prevent collisions with ShapeFilter
shape_filter = pymunk.ShapeFilter(group=1)
chassis_shape.filter = shape_filter
leftLeg_1a_shape.filter = shape_filter
rightLeg_1a_shape.filter = shape_filter
leftLeg_1b_shape.filter = shape_filter
rightLeg_1b_shape.filter = shape_filter
simulate = False
rotationRate = 2
while running:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
#running = False
sys.exit(0)
elif event.type == KEYDOWN and event.key == K_s:
# Start/stop simulation.
simulate = not simulate
elif event.type == KEYDOWN and event.key == K_r:
# Reset.
# simulate = False
self.reset_bodies()
elif event.type == KEYDOWN and event.key == K_UP:
motor_ba1Left.rate = rotationRate
elif event.type == KEYDOWN and event.key == K_DOWN:
motor_ba1Left.rate = -rotationRate
elif event.type == KEYDOWN and event.key == K_LEFT:
motor_ac1Left.rate = rotationRate
elif event.type == KEYDOWN and event.key == K_RIGHT:
motor_ac1Left.rate = -rotationRate
elif event.type == KEYUP:
motor_ba1Left.rate = 0
motor_ac1Left.rate = 0
self.draw()
### Update physics
fps = 50
iterations = 25
dt = 1.0/float(fps)/float(iterations)
if simulate:
for x in range(iterations): # 10 iterations to get a more stable simulation
self.space.step(dt)
pygame.display.flip()
clock.tick(fps)
if __name__ == '__main__':
sim = Simulator()
sim.main()
It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.
The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.
If there's a better way, do let me know by posting an answer or editing this one.
I am trying to find the distance from the start node named seeker to the goal node named target. I have already done most of the implementation of the algorithm and each of the nodes have a coordinate or box on a grid. My python code is lacking and I cannot find the coordinates and distance.
I have tried using the vector in the math class which gave me logical errors.
I also tried sending the actual objects hoping I could use those to reference the coordinates as I'd done in the code but got a TypeError: expected sequence object with len >= 0 or a single integer.
import numpy as np
import pygame as pg
import sys
from Settings import *
from Sprites import *
vec = pg.math.Vector2
class Game:
# initialise game window, etc
def __init__(self):
self.playing = True
pg.init()
self.mover = 'target'
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(Title)
self.clock = pg.time.Clock()
pg.key.set_repeat(500, 100)
self.load_data()
# pg.mouse.set_pos(0, 0)
self.walls = []
def load_data(self):
pass
# start a new game
def new(self):
self.all_sprites = pg.sprite.Group()
self.player = Player(self, 10, 10)
self.target = Target(self, 5, 5)
def load_data(self):
pass
# game loop
def run(self):
while self.playing:
self.dt = self.clock.tick(FPS) / 10000
self.events()
self.update()
self.draw()
def draw_grid(self):
for x in range(0, WIDTH, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
def quit(self):
pg.quit()
sys.exit()
def update(self):
# game loop update
self.all_sprites.update()
def events(self):
# game loop events
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.MOUSEBUTTONDOWN:
mpos = vec(pg.mouse.get_pos()) // TILESIZE
if event.button == 1:
if mpos in g.walls:
g.walls.remove(mpos)
else:
g.walls.append(mpos)
if event.type == pg.KEYDOWN:
if event.key == pg.K_b:
# start breadth first
breadthfirst(self.target, self.player, self.walls, self.screen)
if event.key == pg.K_d:
# start depth first
depthfirst(self.target, self.player, self.walls, self.screen)
if event.key == pg.K_a:
# start A* search
astarsearch(self.target, self.player, self.walls, self.screen)
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_t:
self.mover = 'target'
if event.key == pg.K_s:
self.mover = 'seeker'
if self.mover == 'target':
if event.key == pg.K_LEFT:
self.target.move(dx=-1)
if event.key == pg.K_RIGHT:
self.target.move(dx=1)
if event.key == pg.K_UP:
self.target.move(dy=-1)
if event.key == pg.K_DOWN:
self.target.move(dy=1)
else:
if event.key == pg.K_LEFT:
self.player.move(dx=-1)
if event.key == pg.K_RIGHT:
self.player.move(dx=1)
if event.key == pg.K_UP:
self.player.move(dy=-1)
if event.key == pg.K_DOWN:
self.player.move(dy=1)
def draw(self):
# game loop - draw
self.screen.fill(BGCOLOR)
self.draw_grid()
self.all_sprites.draw(self.screen)
for wall in self.walls:
rect = pg.Rect(wall * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(self.screen, GREEN, rect)
# always do the flip after drawing everything
pg.display.flip()
def show_start_screen(self):
# game splash/start screen
pass
def show_go_screen(self):
# game over/continue screen
pass
def breadthfirst(target, seeker, walls, maingrid):
pass
def depthfirst(target, seeker, wall, maingrid):
pass
def astarsearch(target, seeker, wall, maingrid):
# array to store path locations
direct_graph = {}
# target location, returns a tuple
target = np.where(target.pos) # problem area
# seeker location, returns locations
start = np.where(seeker.pos) # problem area
# array of cost to travel so far
g_cost_array = np.zeros(seeker) # problem area
g_cost_array[start] = 0
total_g_cost = 0
# array for heuristic cost
h_cost_array = np.zeros(seeker) # problem area
# need to use a loop unfortunately...
t = 0
# possible steps
steps = ((-1, 0), (+1, 0), (0, -1), (0, +1))
for rows in h_cost_array:
s = 0
for cols in rows:
# check if it's a wall! if not - get the distance to target
loc = (t, s)
if (maingrid[loc]):
pass
else:
dist = abs(target[0] - s) + abs(target[1] - t)
h_cost_array[t, s] = dist
s += 1
t += 1
# total cost = h + g
f_cost_array = g_cost_array + h_cost_array
# closed and open sets
open_set = []
open_set.append(start)
closed_set = []
# actual path
path = []
path.append([tuple(target[0]), tuple(target[1])])
solution_found = False
while (open_set):
open_f_cost = []
# get the heuristic cost for the candidates in the open set
for vals in open_set:
open_f_cost.append(f_cost_array[vals])
# the shortest heuristic now
# the index of the candidate with the lowest distance/heuristic
best_dist_now = open_f_cost.index(min(open_f_cost))
# the current best position
best_pos_now = open_set[best_dist_now]
# if the destination is reached, finish!
if (tuple(best_pos_now) == target):
solution_found = True
break
else:
# remove the best guy from the open_set and add it to the closed set
closed_set.append(open_set.pop(best_dist_now))
# analyze the steps from the current best
for step in steps:
cand = (best_pos_now[0] + step[0], best_pos_now[1] + step[1])
# check if there's a wall or beyond the screen
if cand[0] < 0 or cand[1] < 0 or cand[0] > 39 or cand[1] > 23:
pass
# skip this candidate because it's a wall!
elif maingrid[cand]:
pass
# need an else clause here to weed out the off-screen locations
else:
# check if the candidate is in the closed set
already_seen = False
for dead in closed_set:
if np.all(dead == cand):
already_seen = True
break
# if the cell is in the closed set, skip it
if already_seen:
pass
else:
approx_g_score = g_cost_array[best_pos_now] + 1
# check if it's in the open list:
new = True
for others in open_set:
if np.all(others == cand):
new = False
break
# if a new cell or improved
if (new or approx_g_score < g_cost_array[cand]):
direct_graph[tuple(cand[0]), tuple(cand[1])] = (
tuple(best_pos_now[0]), tuple(best_pos_now[1]))
g_cost_array[cand] = approx_g_score
f_cost_array[cand] = g_cost_array[cand] + h_cost_array[cand]
if new:
open_set.append(cand)
if not solution_found:
return None
else:
recurrentPath(path, direct_graph, target, start)
return path
# takes a dictionary as input
def recurrentPath(final_path, raw_path, dest, origin):
a = raw_path[tuple(dest[0]), tuple(dest[1])]
final_path.append(a)
if (a != origin):
recurrentPath(final_path, raw_path, a, origin)
else:
return final_path
g = Game()
g.show_start_screen()
while True:
g.new()
g.run()
g.show_go_screen()
pg.QUIT
the expected results is to return the correct path to the target object from the seeker object after you press the keyboard character a. Note that with a left click, a wall can be constructed to add a hindrance to the seeking of the goal.
I'm building a multiple choice quiz game in pygame however there is an issue with my while loop.
I successfully built the game in python without the GUI and all the logic works fine, however, when I try and add the GUI to the logic it doesn't work in the same way.
In my working logic game the continuous while loop is paused at every question due to me using the 'raw_input()' function and waits for the user to answer. When I do the GUI version my questions are read in successfully but it reads them all in at once and there is no time to answer.
I've tried adding time.sleep() after reading each question but this means that the program doesn't respond to event handlers. If the event handlers did work then this would be the perfect solution, giving users a certain amount of time to answer.
This example code below won't compile as I have left out many classes and methods but wanted to show this is where my issue lies. I read dictionary keys and values in and then try and match the user input with the index of the correct answer which is always answerMain[0] before getting shuffled.
Has anyone had a similar issue or know of a possible solution?
attemptMain = {'Q': 'Question Here', 'A': 'Answer1','B': 'Answer2','c': 'Answer3','D': 'Answer4', }
def normalQuestions(self, list):
for i in list:
questionMain = self.attemptMain.keys()[i - 1]
answersMain = (self.attemptMain.values()[i - 1])
correctAnswerMain = answersMain[0]
shuffle(answersMain)
answersMainIndex = (1 + answersMain.index(correctAnswerMain) )
self.layout.questionandAnswers(questionMain, answersMain[0], answersMain[1], answersMain[2], answersMain[3])
time.sleep(10)
x = self.layout.controls()
if (x == answersMainIndex):
print("this is the correct answer")
def controls(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print("here" + str(1))
return '1'
elif event.key == pygame.K_2:
print("here" + str(2))
return '2'
elif event.key == pygame.K_3:
print("here" + str(3))
return '3'
elif event.key == pygame.K_4:
print("here" + str(4))
return '4'
Here's a quiz game example. I use the dt = clock.tick(fps) solution from the linked answer. You can just decrement a time variable by the dt and if it's below 0, switch to the next question. The user can input 1-4 which is then compared with the last element of the question tuple to check if the answer was correct.
import random
import pygame as pg
pg.init()
FONT = pg.font.Font(None, 34)
FONT_SMALL = pg.font.Font(None, 24)
BLUE = pg.Color('steelblue1')
def get_question(questions, question_index):
"""Create a surface with the question and the 4 answer choices."""
question, *answers, _ = questions[question_index]
# Blit the question and answers onto this transparent surface.
question_surface = pg.Surface((500, 150), pg.SRCALPHA)
question_surface.blit(FONT.render(str(question), True, BLUE), (0, 0))
for y, answer in enumerate(answers, 1):
txt = FONT_SMALL.render('{}: {}'.format(y, answer), True, BLUE)
question_surface.blit(txt, (0, 20*y+20))
return question_surface
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
dt = 0
time_to_answer = 5 # User has 5 seconds to answer.
time = time_to_answer
# A list of tuples that contain the question, 4 multiple choice
# answers and the last element is the correct answer.
questions = [
('Is it A, B, C or D?', 'A', 'B', 'C', 'D', '3'),
("What's 2+3?", '23', '5', '3', '2', '2'),
]
random.shuffle(questions)
question_index = 0
question_surface = get_question(questions, question_index)
correct = 0
incorrect = 0
game_over = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if not game_over:
user_input = event.unicode
correct_answer = questions[question_index][-1]
if user_input == correct_answer:
print('Correct')
correct += 1
time = time_to_answer
else:
incorrect += 1
time = time_to_answer
question_index += 1
if question_index >= len(questions):
game_over = True
else:
question_surface = get_question(questions, question_index)
else: # Restart.
game_over = False
correct = 0
incorrect = 0
random.shuffle(questions)
question_index = 0
question_surface = get_question(questions, question_index)
if not game_over:
time -= dt
# If the timer is below 0, switch to the next question.
if time <= 0:
question_index += 1
incorrect += 1
if question_index >= len(questions):
game_over = True
else:
time = time_to_answer
print('next')
question_surface = get_question(questions, question_index)
screen.fill((30, 30, 30))
if not game_over:
screen.blit(question_surface, (20, 50))
time_surface = FONT.render(str(round(time, 1)), True, BLUE)
screen.blit(time_surface, (20, 20))
correct_answer_surface = FONT_SMALL.render(
'Correct {}, incorrect {}'.format(correct, incorrect),
True, BLUE)
screen.blit(correct_answer_surface, (20, 250))
pg.display.flip()
dt = clock.tick(30) / 1000
if __name__ == '__main__':
main()
pg.quit()