# _*_ coding:utf-8 _*_
from random import randint
import pygame
from pygame.locals import *
from sys import exit
__author__ = 'admin'
'''
童年的回忆:掌上游戏机之赛车
'''
pygame.init()
# 设置屏幕宽度
SCREEN_SIZE = (320, 400)
# 颜色
BLACK = (0, 0, 0)
WHITE = (250, 250, 250)
BLUE = (0, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
ORANGE = (255, 128, 64)
DARK_GREEN = (0, 64, 0)
# 调试网格中单元格的长宽
cell_width = 20
cell_height = 20
# 初始化游戏状态:1/游戏开始界面 2/游戏进行中 3/游戏结束
status = 1
# 阻挡对象的列表
blocks = []
# 斑马线对象的列表
zebras = []
# 初始化得分
score = 0
speed_para = 4
clock = pygame.time.Clock()
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
class Block():
def __init__(self):
# 随机生成通道位置的参照坐标
self.refer_x = randint(0, SCREEN_SIZE[0] + 1)
# 障碍物起始纵坐标
self.block_y = 0
# 随机生成通道的大小
self.passway_width = randint(4, 10) * cell_width
# 随机生成障碍物的间距,可以依据这个控制同屏出现的最大障碍物数
self.block_spacing = randint(10, 10) * cell_height
# block的高度,可以设计成随机高度
self.block_height = randint(5, 20) / 10 * cell_height
# 初始化
self.block_right_pos = (0, 0)
self.block_left_pos = (0, 0)
self.block_color = ORANGE
# 判定碰撞时需要使用block的底部纵坐标
self.block_bottom_y = self.block_y + self.block_height
# 障碍物移动情况
def block_move_down(self):
self.block_y += 1 * speed_para
# 画出本次Block()对象
def draw(self):
# 通道随机出的参照坐标为左侧边缘
if self.refer_x == 0:
self.block_right_pos = (self.passway_width, self.block_y)
pygame.draw.rect(screen, self.block_color,
pygame.Rect(self.block_right_pos,
(SCREEN_SIZE[0] - self.passway_width, self.block_height)))
# 通道随机出的参照坐标为右侧边缘
elif self.refer_x == SCREEN_SIZE[0]:
self.block_left_pos = (0, self.block_y)
pygame.draw.rect(screen, self.block_color,
pygame.Rect(self.block_left_pos, (SCREEN_SIZE[0] - self.passway_width, self.block_height)))
# 通道随机出的参照坐标距离右侧边缘间不足以满足你设定的self.passway_width通道宽度
elif self.refer_x + self.passway_width > 320:
self.block_left_pos = (0, self.block_y)
self.block_right_pos = (self.refer_x, self.block_y)
pygame.draw.rect(screen, self.block_color,
pyga
PyGame:掌上游戏机之赛车
最新推荐文章于 2024-07-04 05:48:04 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)