Pygam Lesson 4 - Color Monitoring (maze game)

Preface: We learned in the first three lessons, window creation, image loading, common mouse events, etc. Today we learn a color monitoring and make a maze game. Let's take a look

The previous three lessons are here, please remember to pay attention to them:

insert image description here

video demo

insert image description here

1. Interface construction

  • Background image (800x600):
    insert image description here

In the last lesson we learned about mouse events, and in this lesson we will learn about button events

Code usage for key event:

while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
		        exit()
		if event.type == pygame.KEYDOWN:
		    if event.key == pygame.K_LEFT:
		        按下左键执行的代码块
		    elif event.key == pygame.K_RIGHT:
		        按下右键执行的代码块
		    elif event.key == pygame.K_UP:
		        按下上键执行的代码块
		    elif event.key == pygame.K_DOWN:
		        按下下键执行的代码块
		   ...
		   ...
		   ...
  • Common Key Events
pygame.K_LEFT left button
pygame.K_RIGHT right click
pygame.K_UP up key
pygame.K_DOWN down key

Color detection:

sc.get_at((x,y))
sc: window object
get_at((x,y)), the function is a tuple, which indicates the color of the current coordinates, and the color return value is (r,g,b)

sc.get_at((x-9,y))

logical analysis:

  • 1. Use the maze back image as the window background (800x600)
  • 2. Use the circle drawing code in the previous lesson to draw a small circle
  • 3. Obtain the coordinates of the four vertices of the small circle
  • 4. Determine the color of the coordinates of the four vertices of the small circle, if it touches the black line. If it returns (0,0,0), it means the game is lost; if it touches the green box at the end point, it means the game wins.

Continuous monitoring and interval time of keys

If you do not write this line, the ball will not be able to continue to move

pygame.key.set_repeat(1,10)

Full version code:

# 完整代码
# 请完成迷宫游戏
import pygame,sys
import time
pygame.init()
#2_2 :设置持续按键时间间隔
pygame.key.set_repeat(1,10)
#设定好颜色变量,方便下面使用
red = (255,0,0)
black = (0,0,0)
green = (0,255,0)
# 3_2:设置变量,判断是否胜利
win = False
#知识进阶,计算时间
mytime1 = time.time()
# 1:创建窗口、贴上小球
sc = pygame.display.set_mode((860,660))
bg = pygame.image.load("./image/maze.png")
title = pygame.display.set_caption("迷宫")
sc.blit(bg,(0,0))
#由于小球的位置是会变化的,所以要线设置变量
# x = 760 作弊坐标
# y = 600 作弊坐标

x = 25
y = 25

# replay = input("xxxxxxxxxxxxxxxxxxxxx")
def gong(x,y):
    while True:
        sc.blit(bg,(0,0))
        pygame.draw.circle(sc,red,(x,y),8)
        pygame.display.update( )
        # 2_1:获取键盘事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x-=1
                elif event.key == pygame.K_RIGHT:
                    x+=1
                elif event.key == pygame.K_UP:
                    y-=1
                elif event.key == pygame.K_DOWN:
                    y+=1
        # 3_1 : 侦测颜色,判断输赢
        if sc.get_at((x-9,y))==black or sc.get_at((x+9,y))==black or sc.get_at((x,y+9))==black or sc.get_at((x,y-9))==black:
            # 3_2:设置变量,判断是否胜利
            win = False
            break
        if sc.get_at((x-9,y))==green or sc.get_at((x+9,y))==green or sc.get_at((x,y+9))==green or sc.get_at((x,y-9))==green:
            win = True

            break
gong(x,y)
#3_3 判断胜利或失败
while True:
    if win == True:
        mytime2 = time.time()
        mytime = int(mytime2-mytime1)
        print("恭喜,你逃出了迷宫,用时:%s秒"%mytime)
        replay = input("你想再玩一遍吗?(y or n )")
        if replay == "y":
            gong(x,y)
        else:
            print("退出游戏")
            sys.exit()
    else:
        print("真遗憾,你输了")
        replay = input("你想再玩一遍吗?(y or n )")
        if replay == "y":
            gong(x,y)
        else:
            print("退出游戏")
            sys.exit()

I hope everyone has to help

A little programmer dedicated to office automation#

I've seen this, follow + like + bookmark = don't get lost! !

If you want to know more about Python office automation, please pay attention!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/130804267