python做一个小游戏

#coding=utf-8
import pygame
from pygame.locals import *
import sys
import random
 
 
BLACK =(0,0,0)
WHITE = (255,255,255)
bg_color = (0, 0, 70)# 背景颜色
 
SCREEN_SIZE = [320,400]#屏幕大小
BAR_SIZE = [20,5]#挡板大小
BALL_SIZE = [15,15]#球的尺寸
 
 
class Game(object):
    def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()#定时器
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption('My Game')#设置标题
 
        #ball 初始位置
        self.ball_pos_x = SCREEN_SIZE[0]//2 - BALL_SIZE[0]/2
        self.ball_pos_y = 0
        #ball 移动方向
        #self.ball_dir_x = -1 #-1:left 1:right
        self.ball_dir_y = 1# 1:down
 
        self.ball_pos = pygame.Rect(self.ball_pos_x,self.ball_pos_y,BALL_SIZE[0],BALL_SIZE[1])
 
        self.score =0
        self.bar_pos_x = SCREEN_SIZE[0]//2 - BAR_SIZE[0]//2
        self.bar_pos = pygame.Rect(self.bar_pos_x,SCREEN_SIZE[1]-BAR_SIZE[1],BAR_SIZE[0],BALL_SIZE[1])
 
    def bar_move_left(self):#左移
        self.bar_pos_x = self.bar_pos_x - 2
 
    def bar_move_right(self):#右移
        self.bar_pos_x = self.bar_pos_x + 2
 
    def run(self):
        pygame.mouse.set_visible(0) #移动鼠标不可见
        bar_move_left =False
        bar_move_right = False
        while True:
            for event in pygame.event.get():
                if event.type == QUIT: #当按下关闭按键
                    pygame.quit()
                    sys.exit()#接收到退出事件后退出程序
 
                elif event.type == pygame.MOUSEBUTTONDOWN and event.button ==1:#鼠标左键按下
                    bar_move_left = True
                elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: #左键弹起
                    bar_move_left = False
                elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:#右键
                    bar_move_right = True
                elif event.type == pygame.MOUSEBUTTONUP and event.button == 3:  #左键弹起
                    bar_move_right = False
 
            if bar_move_left == True and bar_move_right ==False:
                self.bar_move_left()
            if bar_move_left == False and bar_move_right == True:
                self.bar_move_right()
 
            self.screen.fill(bg_color)
            self.bar_pos.left = self.bar_pos_x
            pygame.draw.rect(self.screen, WHITE, self.bar_pos)
 
            ## 球移动
            self.ball_pos.bottom += self.ball_dir_y * 3
            pygame.draw.rect(self.screen, WHITE, self.ball_pos)
 
            ## 判断球是否落到板上
            if self.bar_pos.top <= self.ball_pos.bottom and (
                    self.bar_pos.left <= self.ball_pos.right and self.bar_pos.right >= self.ball_pos.left):
                self.score += 1
                print("Score: ", self.score, end='\r')
            elif self.bar_pos.top <= self.ball_pos.bottom and (
                    self.bar_pos.left > self.ball_pos.right or self.bar_pos.right < self.ball_pos.left):
                print("Game Over: ", self.score)
                return self.score
 
            ## 更新球下落的初始位置
            if self.bar_pos.top <= self.ball_pos.bottom:
                self.ball_pos_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
                self.ball_pos_y = 0
                self.ball_pos = pygame.Rect(self.ball_pos_x, self.ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
 
            pygame.display.update()#更新软件界面显示
            self.clock.tick(60)
 
 
game = Game()
game.run()#启动

资源🐖页下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑I客I零

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值