import pygame,sys
import tkinter,time
from pygame.locals import *
import pygame
from random import randint
import pygame
from pygame.locals import *
from sys import exit
import webbrowser
from tkinter.ttk import *
from tkinter import *
import tkinter as tk
import random
import string
import math
from math import *
from time import *
import os,sys,keyword
from tkinter import messagebox,PhotoImage
from tkinter import*
from datetime import datetime
import webbrowser
from cefpython3 import cefpython as cef
import platform
from tkinter.ttk import OptionMenu
import urllib
import requests,time,random,json,struct
def p():
__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,
pygame.Rect(self.block_left_pos, (self.refer_x - self.passway_width, self.block_height)))
pygame.draw.rect(screen, self.block_color,
pygame.Rect(self.block_right_pos, (SCREEN_SIZE[0] - self.refer_x, self.block_height)))
else:
self.block_left_pos = (0, self.block_y)
self.block_right_pos = (self.refer_x + self.passway_width, self.block_y)
pygame.draw.rect(screen, self.block_color,
pygame.Rect(self.block_left_pos, (self.refer_x, self.block_height)))
pygame.draw.rect(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, status
self.block_move_down()
if self.block_y == self.block_spacing:
blocks.append(Block())
if self.block_y > SCREEN_SIZE[1]:
del blocks[0]
score += 1
print(score)
# 判定碰撞时需要使用block的底部纵坐标
block_bottom_y = self.block_y + self.block_height
# 获取car的几个属性
car_pos_y = car.car_pos_y
car_pos_x = car.car_pos_x
car_width = car.car_width
# 如果block的底部在car的高度内,需要判断碰撞
if car_pos_y <= block_bottom_y <= car_pos_y + 3 * cell_height:
# 通道随机出的参照坐标为左侧边缘
if self.refer_x == 0:
# 如果car的躯壳在通道内,判定为不碰撞
if self.refer_x <= car_pos_x <= self.refer_x + self.passway_width - car_width:
pass
# car的躯壳不在通道内
else:
status = 3
# 通道随机出的参照坐标为右侧边缘
elif self.refer_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通道宽度
elif self.refer_x + self.passway_width > 320:
# 如果car的躯壳在通道内,判定为不碰撞
if self.refer_x - self.passway_width <= car_pos_x <= self.refer_x - car_width:
pass
# car的躯壳不在通道内
else:
status = 3
else:
# 如果car的躯壳在通道内,判定为不碰撞
if self.refer_x <= car_pos_x <= self.refer_x + self.passway_width - car_width:
pass
# car的躯壳不在通道内
else:
status = 3
blocks.append(Block())
class Car():
def __init__(self):
# 定义car的宽度及高度
self.car_width = cell_width * 3
self.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 = 5
self.car_color = GREEN
# car的左移处理
def car_move_left(self):
self.car_pos_x -= 2 * self.car_speed
self.car_pos_x = max(0, self.car_pos_x)
# car的右移处理
def car_move_right(self):
self.car_pos_x += 2 * self.car_speed
self.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
# 画出car
def draw(self, car_points):
for car_point in car_points:
pygame.draw.rect(screen, self.car_color, pygame.Rect(car_point, (cell_width, ce
爬虫与pyqt的应用之游戏项目(喜欢点赞o)
最新推荐文章于 2024-08-07 22:30:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)