• 大小: 57KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: Python
  • 标签: python  

资源简介

Python小游戏,打砖块的源代码。启蒙初学者

资源截图

代码片段和文件信息

“““
 bricka (a breakout clone)
 Developed by Leonel Machava 

 http://codeNtronix.com
“““
import sys
import pygame

SCREEN_SIZE   = 640480

# object dimensions
BRICK_WIDTH   = 60
BRICK_HEIGHT  = 15
PADDLE_WIDTH  = 60
PADDLE_HEIGHT = 12
BALL_DIAMETER = 16
BALL_RADIUS   = BALL_DIAMETER / 2

MAX_PADDLE_X = SCREEN_SIZE[0] - PADDLE_WIDTH
MAX_BALL_X   = SCREEN_SIZE[0] - BALL_DIAMETER
MAX_BALL_Y   = SCREEN_SIZE[1] - BALL_DIAMETER

# Paddle Y coordinate
PADDLE_Y = SCREEN_SIZE[1] - PADDLE_HEIGHT - 10

# Color constants
BLACK = (000)
WHITE = (255255255)
BLUE  = (00255)
BRICK_COLOR = (2002000)

# State constants
STATE_BALL_IN_PADDLE = 0
STATE_PLAYING = 1
STATE_WON = 2
STATE_GAME_OVER = 3

class Bricka:

    def __init__(self):
        pygame.init()
        
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption(“bricka (a breakout clone by codeNtronix.com)“)
        
        self.clock = pygame.time.Clock()

        if pygame.font:
            self.font = pygame.font.Font(None30)
        else:
            self.font = None

        self.init_game()

        
    def init_game(self):
        self.lives = 3
        self.score = 0
        self.state = STATE_BALL_IN_PADDLE

        self.paddle   = pygame.Rect(300PADDLE_YPADDLE_WIDTHPADDLE_HEIGHT)
        self.ball     = pygame.Rect(300PADDLE_Y - BALL_DIAMETERBALL_DIAMETERBALL_DIAMETER)

        self.ball_vel = [5-5]

        self.create_bricks()
        

    def create_bricks(self):
        y_ofs = 35
        self.bricks = []
        for i in range(7):
            x_ofs = 35
            for j in range(8):
                self.bricks.append(pygame.Rect(x_ofsy_ofsBRICK_WIDTHBRICK_HEIGHT))
                x_ofs += BRICK_WIDTH + 10
            y_ofs += BRICK_HEIGHT + 5

    def draw_bricks(self):
        for brick in self.bricks:
            pygame.draw.rect(self.screen BRICK_COLOR brick)
        
    def check_input(self):
        keys = pygame.key.get_pressed()
        
        if keys[pygame.K_LEFT]:
            self.paddle.left -= 5
            if self.paddle.left < 0:
                self.paddle.left = 0

        if keys[pygame.K_RIGHT]:
            self.paddle.left += 5
            if self.paddle.left > MAX_PADDLE_X:
                self.paddle.left = MAX_PADDLE_X

        if keys[pygame.K_SPACE] and self.state == STATE_BALL_IN_PADDLE:
            self.ball_vel = [5-5]
            self.state = STATE_PLAYING
        elif keys[pygame.K_RETURN] and (self.state == STATE_GAME_OVER or self.state == STATE_WON):
            self.init_game()

    def move_ball(self):
        self.ball.left += self.ball_vel[0]
        self.ball.top  += self.ball_vel[1]

        if self.ball.left <= 0:
            self.ball.left = 0
            self.ball_vel[0] = -self.ball_vel[0]
        elif self.ball.left >= MAX_BALL_X:
            self.bal

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        5960  2011-04-14 02:34  bricka.py
     文件          74  2011-04-14 02:36  README.txt
     文件       58068  2011-04-14 03:10  screenshot.png

评论

共有 条评论