Python项目:赛车

#coding:utf-8
import pygame
from pygame.locals import *
import time
import random
import sys
import os
#初始化pygame环境
pygame.init ()

#创建一个长宽分别为1300/700窗口
os.environ[ 'SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 25)
canvas = pygame.display.set_mode((1000,600))
canvas.fill((255,255,255))

#设置窗口标题
pygame.display.set_caption("赛车")

#加载图片
#背景图片
bg = pygame.image.load("images/赛道.png")
#敌人车辆
car1 = pygame.image.load("images/赛车02.png")
#玩家车辆
car2 = pygame.image.load("images/赛车01.png")
#遮光罩
bg1=pygame.image.load("images/屏幕遮罩.png")
#计时板
clock=pygame.image.load("images/倒计时.png")
#游戏胜利
win=pygame.image.load("images/胜利.png")
#游戏失败
lose=pygame.image.load("images/再接再厉.png")

def handleEvent():  
    for event in pygame.event.get():
        if event.type == QUIT :
            pygame.quit() 
            sys.exit()
        #键盘控制
        if Game.states=='RUNNING':
            if event.type == KEYDOWN and event.key == K_RIGHT :
                Game.player.right()
            if event.type == KEYDOWN and event.key == K_LEFT :
                Game.player.left()
#玩家类
class Player():
    def __init__(self,x,y,img):
        self.width=76
        self.height=175
        self.x=x
        self.y=y
        self.img=img
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    
#敌人类
class Car():
    def __init__(self,x,y,img):
        self.width=87
        self.height=164
        self.x=random.randint(255,(1000-self.width-225))
        self.y=-self.height
        self.img=img
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    def step(self):
        self.y=self.y+10

#赛道背景类
class Runway():
    def __init__(self):
        self.width = 1000
        self.height = 1884
        self.img = bg
        self.x1 = 0
        self.y1 = 0
        self.x2 = 0
        self.y2 = -self.height
    def paint(self):
        canvas.blit(self.img,(self.x1,self.y1))
        canvas.blit(self.img,(self.x2,self.y2))
    def step(self):
        self.y1 = self.y1 + 12
        self.y2 = self.y2 + 12
        if self.y1 >= self.height:
            self.y1 = -self.height
        if self.y2 >=self.height:
            self.y2 = -self.height


#产生敌人车辆
def conEnter():
    if not isActionTime(Game.lastTime,Game.interval):
        return
    Game.lastTime=time.time()
    Game.cars.append(Car(0,-164,car1))


#绘制组件方法
def conPaint():
    Game.runway.paint()
    canvas.blit(bg1,(0,0))
    Game.player.paint()
    for car in Game.cars:
        car.paint()

#组件移动方法       
def conStep():
    Game.runway.step()
    for car in Game.cars:
        car.step()    

#存储变量的类    
class Game():
    lastTime=0
    interval=3
    states='RUNNING'
    runway=Runway() 
    player=Player(420,400,car2)
    cars=[] 
def isActionTime(lastTime,interval):
    if lastTime==0:
        return  True
    currentTime=time.time()
    return currentTime-lastTime>=interval



#状态控制方法           
def control():
    conEnter()
    conPaint()
    conStep()

# 工具方法-写文字方法
def fillText(text, position, view=canvas):
    # 设置字体样式和大小
    my_font = pygame.font.Font("my_font/font1.ttf", 38)
    # 渲染文字
    text = my_font.render(text, True, (255, 255, 255))
    view.blit(text, position)
               
while True:
    #调用状态控制方法
    control()
    
    # 监听有没有按下退出按钮
    handleEvent()
    # 更新屏幕内容
    pygame.display.update()
    #延时10毫秒 
    pygame.time.delay(10)

想要可以拿走,不谢


版权声明:本文为m0_67991177原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。