Skip to content

Lesson 05 - Moving the Cube! 🤓🤓

课前复习 Recap - Creat pygame window and add a cube

这段代码是对我们上节课内容的回顾,找找看:你还记得哪句代码可以画出一个矩形吗? 里面的参数分别控制什么内容呢?试试看吧,参数 0 修改为 -1 会有惊喜喔!

你仅需复制这段代码到编辑器,即可修改代码:

import pygame

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
SAND = (255, 255, 255)

# Game setup
# window size
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# window caption
pygame.display.set_caption("Chrome Dino Game")
# Create a Frame Rate object
clock = pygame.time.Clock()

# Game loop
running = True

while running:
    screen.fill(SAND)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # add a cube
    pygame.draw.rect(screen, (255, 0, 0), (400, 300, 100, 100), 0)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

Heroshot: Moving the cube - Horizontally!🌎🤫

复制这段代码,了解如何控制物品水平方向移动; 等等,你还能控制方块垂直方向移动吗? 斜方向的移动呢?

import pygame

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
SAND = (255, 255, 255)

# Game setup
# window size
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# window caption
pygame.display.set_caption("Chrome Dino Game")
# Create a Frame Rate object
clock = pygame.time.Clock()

# Game loop
running = True

#Moving the cube - Horizontally 水平移动
x = 400
while running:
    screen.fill(SAND)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.draw.rect(screen, (255, 0, 0), (x, 400, 100, 100), 0)
    x = x + 4

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

Chanllenge:Rising Smoke!飘烟挑战

如果你能走到这里,说明你的学习能力还真的不错!试试挑战一下,让烟雾飘起来吧!!

import pygame

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
SAND = (255, 255, 255)

# Game setup
# window size
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# window caption
pygame.display.set_caption("Chrome Dino Game")
# Create a Frame Rate object
clock = pygame.time.Clock()

# Game loop
running = True
while running:
    screen.fill(SAND)

    #背景图

    #画矩形-地面
    pygame.draw.rect(screen,(0,255,0),(0,500,800,300),0)
    #画屋子墙体
    pygame.draw.rect(screen,(255,150,100),(500,300,200,200),0)
    pygame.draw.rect(screen, (255, 255, 255), (600, 350, 50, 50), 0)
    #画月亮
    pygame.draw.circle(screen,(200,200,250),(0,0),100)
    #画屋顶
    pygame.draw.polygon(screen,(255,180,100),[(450,300),(600,150),(750,300)])
    pygame.draw.polygon(screen,(0,0,0),[(450,300),(600,150),(750,300)],3)
    #画烟圈
    pygame.draw.ellipse(screen,(180,180,255),[650,160,60,15],1)
    pygame.draw.ellipse(screen,(180,180,255),[650,120,80,30],1)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

Heroshot

飘烟挑战

alt text

Running Man

alt text

Advanced Course - Rhythm Blocks

Initial Code:

This is the initial program for you to develop and design. You can modify the code and add your new ideas to achieve your task goal, have a try! 这是原始代码,只画了两个方块,一起来挑战一下,看你是否能够顺利通关-完成今天的代码任务吧!!

Just copy this program to your editor and get started. Good Luck! 仅需复制以下代码到你的编辑器,即可进一步开发,试试看!

import pygame

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
SAND = (255, 255, 255)

# Game setup
# window size
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# window caption
pygame.display.set_caption("Chrome Dino Game")
# Create a Frame Rate object
clock = pygame.time.Clock()

# Game loop
running = True
# Blocks gap
gap = random.randint(100,100)


while running:
    screen.fill(SAND)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Ground
    pygame.draw.rect(screen,(0,0,0),(0,500,800,5))

    # Moving colored blocks
    # Block1 - Red
    pygame.draw.rect(screen, (255, 0, 0), (300, 400, 50, 100), 0)
    # Block2 - Green
    pygame.draw.rect(screen, (0, 255, 0), (300+gap, 400, 50, 100), 0)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

With the program provided above, you can achieve these goals if you can try eidt the code:

3-colored cubes 🦄

alt text

rainbow cubes 🎹🎵🌈🌈🌞

alt text

Rhythm Blocks 🪗🎹🎶😎

alt text

Try an advanced project ? see this Rhythm Blocks:

alt text

RGB Tools:

You can pick color and use it’s RGB values
你可以在这个网站找到颜色的 RGB 值:RGB Color Wheel