想用opencv写贪吃蛇,发现事件无法绑定到图片窗口上,转用pygame
代码如下:
import pygame
import sys
import random
from pygame.locals import *
import time
import cv2
import numpy as np
#用opencv做贪吃蛇失败
# def show_image(image,show_time):
# cv2.imshow('test',image)
# cv2.waitKey(show_time)
# #cv2.destroyAllWindows()
# class Shake(object):
# body = [[100,100,255],[100,80,255],[100,60,255]]
# stride = 20
# direction = 'right'
# shake_len = 3
# def __init__(self):
# pass
# def shake_move(self):
# if self.direction == 'right':
# x = self.body[0][1]
# x += self.stride
# y = self.body[0][0]
# color = self.body[0][2]
# self.body.insert(0,[y,x,color])
# self.body.pop()
# elif self.direction == 'left':
# self.body[0][1] -= self.stride
# x = self.body[0][1]
# x += self.stride
# y = self.body[0][0]
# color = self.body[0][2]
# self.body.insert(0,[y,x,color])
# self.body.pop()
# elif self.direction == 'down':
# self.body[0][0] += self.stride
# x = self.body[0][1]
# x += self.stride
# y = self.body[0][0]
# color = self.body[0][2]
# self.body.insert(0,[y,x,color])
# self.body.pop()
# elif self.direction == 'up':
# self.body[0][0] -= self.stride
# x = self.body[0][1]
# x += self.stride
# y = self.body[0][0]
# color = self.body[0][2]
# self.body.insert(0,[y,x,color])
# self.body.pop()
# def shake_eat(self):
# append_body = (self.body[self.len-1][0]+20,self.body[self.len-1][1],color)
# self.body.append(append_body)
# self.len = len(self.body)
# class Food(object):
# body = []
# stride = 20
# x = 300
# y = 300
# color = 255
# def __init__(self):
# x = int(600*random.random())
# y = int(800*random.random())
# self.body = [x,y,self.color]
# def eat_food(self):
# x = int(600*random.random())
# y = int(800*random.random())
# self.body = [x,y,self.color]
# def get_direction(shake):
# for event in pygame.event.get():
# if event.type == QUIT:
# pygame.quit()
# sys.exit()
# elif event.type == KEYDOWN: # 键盘输入
# if event.key == K_RIGHT or event.key == ord('d'): # 方向键和AWSD
# shake.direction = 'right'
# if event.key == K_LEFT or event.key == ord('a'):
# shake.direction = 'left'
# if event.key == K_UP or event.key == ord('w'):
# shake.direction = 'up'
# if event.key == K_DOWN or event.key == ord('s'):
# shake.direction = 'down'
# #elif event.key == ord('q'):
# def main():
# cv2.namedWindow('test')
# shake = Shake()
# food = Food()
# i = 0
# while i<15:
# image = np.zeros((800,600))
# for body in shake.body:
# image[body[0]:body[0]+20,body[1]:body[1]+20] = 255
# shake.shake_move()
# print(shake.body)
# get_direction(shake)
# print(shake.direction)
# image[food.body[0]:food.body[0]+20,food.body[1]:food.body[1]+20] = 255
# if shake.body[0][0] == food.body[0] and shake.body[0][1] == food.body[1]:
# food.eat_food()
# shake.shake_eat()
# show_image(image,300)
# i += 1
# pygame.init()
# main()
#启用pygame做贪吃蛇
def main():
pygame.init()
screen = pygame.display.set_mode((800,800),0,24) //创建窗口
background_image_path = 'test.jpg'
shake_image_path = 'test1.jpg'
background_image = pygame.image.load(background_image_path) //提取图片
shake_image = pygame.image.load(shake_image_path)
shake = Shake()
food = Food()
fclock = pygame.time.Clock() //创建时钟
while True:
if shake.body[0][0] == 800 or shake.body[0][1] == 800 or shake.body[0][0]<=0 or shake.body[0][1]<=0:
break
print(shake.body[1:])
print(shake.body)
if shake.body[0] in shake.body[1:]:
break
screen.blit((background_image),(0,0)) //将图片投影到窗口
shake.move()
for body in shake.body:
screen.blit((shake_image),(body[0],body[1]))
if shake.body[0][0] == food.body[0] and shake.body[0][1] == food.body[1]:
shake.eat_food()
food.eat_change()
screen.blit((shake_image),(food.body[0],food.body[1]))
fclock.tick(5) //每秒刷新几次窗口
get_direction(shake)
pygame.display.update()
class Shake(object):
body = [[100,100],[100,80],[100,60]]
shake_len = len(body)
direction = 'right'
stride = 20
def move(self):
if self.direction == 'down':
x = self.body[0][1] + self.stride
self.body.insert(0,[self.body[0][0],x])
self.body.pop()
elif self.direction == 'up':
x = self.body[0][1] - self.stride
self.body.insert(0,[self.body[0][0],x])
self.body.pop()
elif self.direction == 'left':
y = self.body[0][0] - self.stride
self.body.insert(0,[y,self.body[0][1]])
self.body.pop()
elif self.direction == 'right':
y = self.body[0][0] + self.stride
self.body.insert(0,[y,self.body[0][1]])
self.body.pop()
def eat_food(self):
x = self.body[self.shake_len-1][1]
y = self.body[self.shake_len-1][0]
self.body.append([y-self.stride,x-self.stride])
class Food(object):
body = [200,200]
def eat_change(self):
x = 20*int(40*random.random())
y = 20*int(40*random.random())
self.body = [y,x]
def get_direction(shake):
//键盘事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN: # 键盘输入
if event.key == K_RIGHT or event.key == ord('d'): # 方向键和AWSD
shake.direction = 'right'
if event.key == K_LEFT or event.key == ord('a'):
shake.direction = 'left'
if event.key == K_UP or event.key == ord('w'):
shake.direction = 'up'
if event.key == K_DOWN or event.key == ord('s'):
shake.direction = 'down'
main()
print("game-over")