PyGame:掌上游戏机之赛车

阅读: 评论:0

PyGame:掌上游戏机之赛车

PyGame:掌上游戏机之赛车

#   _*_ 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 = 0speed_para = 4
clock = pygame.time.Clock()screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)class Block():def __init__(self):#   随机生成通道位置的参照坐标fer_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):#   通道随机出的参照坐标为左侧边缘fer_x == 0:self.block_right_pos = (self.passway_width, self.block_y)(screen, self.block_color,pygame.Rect(self.block_right_pos,(SCREEN_SIZE[0] - self.passway_width, self.block_height)))# 通道随机出的参照坐标为右侧边缘fer_x == SCREEN_SIZE[0]:self.block_left_pos = (0, self.block_y)(screen, self.block_color,pygame.Rect(self.block_left_pos, (SCREEN_SIZE[0] - self.passway_width, self.block_height)))# 通道随机出的参照坐标距离右侧边缘间不足以满足你设定的self.passway_width通道宽度fer_x + self.passway_width > 320:self.block_left_pos = (0, self.block_y)self.block_right_pos = (fer_x, self.block_y)(screen, self.block_color,pygame.Rect(self.block_left_pos, (fer_x - self.passway_width, self.block_height)))(screen, self.block_color,pygame.Rect(self.block_right_pos, (SCREEN_SIZE[0] - fer_x, self.block_height)))else:self.block_left_pos = (0, self.block_y)self.block_right_pos = (fer_x + self.passway_width, self.block_y)(screen, self.block_color,pygame.Rect(self.block_left_pos, (fer_x, self.block_height)))(screen, self.block_color,pygame.Rect(self.block_right_pos,(SCREEN_SIZE[0] - self.block_right_pos[0], self.block_height)))# 更新障碍物的位置以及生成新的障碍物对象def update(self):global score, statusself.block_move_down()if self.block_y == self.block_spacing:blocks.append(Block())if self.block_y > SCREEN_SIZE[1]:del blocks[0]score += 1print(score)# 判定碰撞时需要使用block的底部纵坐标block_bottom_y = self.block_y + self.block_height#   获取car的几个属性car_pos_y = car.car_pos_ycar_pos_x = car.car_pos_xcar_width = car.car_width#   如果block的底部在car的高度内,需要判断碰撞if car_pos_y <= block_bottom_y <= car_pos_y + 3 * cell_height:#   通道随机出的参照坐标为左侧边缘fer_x == 0:#   如果car的躯壳在通道内,判定为不碰撞fer_x <= car_pos_x <= fer_x + self.passway_width - car_width:pass#   car的躯壳不在通道内else:status = 3# 通道随机出的参照坐标为右侧边缘fer_x == SCREEN_SIZE[0]:#   如果car的躯壳在通道内,判定为不碰撞if SCREEN_SIZE[0] - self.passway_width <= car_pos_x <= SCREEN_SIZE[0] - car_width:pass#   car的躯壳不在通道内else:status = 3# 通道随机出的参照坐标距离右侧边缘间不足以满足你设定的self.passway_width通道宽度fer_x + self.passway_width > 320:#   如果car的躯壳在通道内,判定为不碰撞fer_x - self.passway_width <= car_pos_x <= fer_x - car_width:pass#   car的躯壳不在通道内else:status = 3else:#   如果car的躯壳在通道内,判定为不碰撞fer_x <= car_pos_x <= fer_x + self.passway_width - car_width:pass#   car的躯壳不在通道内else:status = 3blocks.append(Block())class Car():def __init__(self):#   定义car的宽度及高度self.car_width = cell_width * 3self.car_height = cell_height * 4#   获取car的左上角定点坐标(这里的car区域实际上是一个矩形)self.car_pos_x = SCREEN_SIZE[0] / 2 - (cell_width + cell_width / 2)self.car_pos_y = SCREEN_SIZE[1] - cell_height * 4#   设置car的移动速度self.car_speed = 5self.car_color = GREEN#   car的左移处理def car_move_left(self):self.car_pos_x -= 2 * self.car_speedself.car_pos_x = max(0, self.car_pos_x)#   car的右移处理def car_move_right(self):self.car_pos_x += 2 * self.car_speedself.car_pos_x = min(SCREEN_SIZE[0] - cell_width * 3, self.car_pos_x)#   更新car中每个需要染色部分的坐标,以方便在car矩形图中染色出car的形状def update_car_pos(self):car_point_1 = (self.car_pos_x + cell_width, self.car_pos_y)car_point_2 = (self.car_pos_x, self.car_pos_y + cell_height)car_point_3 = (self.car_pos_x + cell_width, self.car_pos_y + cell_height)car_point_4 = (self.car_pos_x + cell_width * 2, self.car_pos_y + cell_height)car_point_5 = (self.car_pos_x + cell_width, self.car_pos_y + cell_height * 2)car_point_6 = (self.car_pos_x, self.car_pos_y + cell_height * 3)car_point_7 = (self.car_pos_x + cell_width, self.car_pos_y + cell_height * 3)car_point_8 = (self.car_pos_x + cell_width * 2, self.car_pos_y + cell_height * 3)car_points = [car_point_1, car_point_2, car_point_3, car_point_4, car_point_5, car_point_6, car_point_7,car_point_8]return car_points#   画出cardef draw(self, car_points):for car_point in car_points:(screen, self.car_color, pygame.Rect(car_point, (cell_width, cell_height)))car = Car()#   斑马线
class Zebra():def __init__(self):#   定义一个斑马线对象的高度及宽度bra_rect_height = 5 * bra_rect_width = 3 * cell_width#   初始化斑马线区域左上角坐标bra_pos_x, bra_pos_y = (0, 0)#   获取斑马线中间的矩形块左上角坐标bra_rect_x = SCREEN_SIZE[0] / 2 - cell_width / bra_rect_y = bra_pos_y + (cell_height + cell_height / 2)def draw(self):#   斑马线左侧虚线的横坐标left_line_x = SCREEN_SIZE[0] / 2 - (cell_width + cell_width / 2)#   斑马线右侧虚线的横坐标right_line_x = SCREEN_SIZE[0] / 2 + (cell_width + cell_width / 2)#   一个斑马线对象的两侧虚线高度line_height = bra_pos_y + 5 * cell_height#   画出一个斑马现对象的两侧虚线for i in bra_pos_y, line_height + 1, cell_height * 2):pygame.draw.line(screen, WHITE, (left_line_x, i), (left_line_x, i + cell_height))pygame.draw.line(screen, WHITE, (right_line_x, i), (right_line_x, i + cell_height))#   斑马线中间矩形块的宽度及高度zebra_rect_width = cell_widthzebra_rect_height = cell_height * 2#   画出斑马线对象的中间矩形块(screen, WHITE, pygame.Rect((bra_rect_x, bra_rect_y), (zebra_rect_width, zebra_rect_height)))def update(self):bra_pos_y += 1 * bra_rect_y = bra_pos_y + (cell_height + cell_height / 2)bra_pos_y == bra_rect_height:zebras.append(Zebra())bra_pos_y > SCREEN_SIZE[1]:del zebras[0]zebras.append(Zebra())class GmaeBody():# 退出检测函数def checkForOut():global status, blocks, score, car, blockfor event in ():#   点击×pe == 12:exit()pe == 2:#   按键为Escif event.key == 27:exit()elif event.key == 32:if status == 1:status = 2elif status == 3:score = 0blocks = []car = Car()blocks.append(Block())status = 2#   调试用网格def debug_grid(bool):grid_color = WHITEif bool:#   调试用网格for i in range(cell_width, 320, cell_width):pygame.draw.line(screen, grid_color, (i, 0), (i, 400))for j in range(cell_height, 400, cell_height):pygame.draw.line(screen, grid_color, (0, j), (320, j))#   游戏开始菜单的设计def start_menu():font = pygame.font.SysFont("arial", 30)text_surWarn = der(u"START", True, BLUE)warn_width = _width()warn_height = _height()pos_warn = (SCREEN_SIZE[0] / 2 - warn_width / 2, SCREEN_SIZE[1] / 2 - warn_height / 2)screen.blit(text_surWarn, pos_warn)#   游戏结束菜单的设计def end_menu():font_score = pygame.font.SysFont("arial", 30)font_warn = pygame.font.SysFont("arial", 30, 5)text_surScore = der("Score:%d" % score, True, RED)text_surWarn = der(u"RESTART", True, RED)winner_width = _width()winner_height = _height()warn_width = _width()warn_height = _height()rect_width = max(winner_width, warn_width)rect_height = winner_height + warn_heightrect_y = SCREEN_SIZE[1] / 2 - rect_height / 2rect_x = SCREEN_SIZE[0] / 2 - rect_width / (screen, GREEN, pygame.Rect((rect_x, rect_y), (rect_width, rect_height)))#   显示居中处理winner_x = SCREEN_SIZE[0] / 2 - winner_width / 2winner_y = rect_yscreen.blit(text_surScore, (winner_x, winner_y))#   显示居中处理pos_warn_x = SCREEN_SIZE[0] / 2 - warn_width / 2pos_warn_y = rect_y + winner_heightscreen.blit(text_surWarn, (pos_warn_x, pos_warn_y))while True:checkForOut()if status == 1:start_menu()if status == 2:#   初始化carcar_points = car.update_car_pos()#   捕获按键key_pressed = _pressed()#   如果按下LEFT键,执行挡板左移if key_pressed[276]:car.car_move_left()car_points = car.update_car_pos()# 如果按下RIGHT键,执行挡板右移elif key_pressed[275]:car.car_move_right()car_points = car.update_car_pos()#   填充颜色为黑色,刷新界面screen.fill(BLACK)for zebra in zebras:zebra.update()zebra.draw()#   分别显示blocks列表中的每一个blocfor block in blocks:block.update()block.draw()#   画出carcar.draw(car_points)#   开启网格调试debug_grid(False)if status == 3:end_menu()#   延迟界面刷新clock.tick(60)pygame.display.update()GmaeBody()

 

本文发布于:2024-02-02 11:52:12,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170684593043614.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:游戏机   掌上   PyGame
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23