资源简介
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
- 上一篇:Python-每天定时发消息
- 下一篇:使用训练好的模型进行预测
相关资源
- 使用训练好的模型进行预测
- Python-每天定时发消息
- 实现火车票查询python.zip
- python SVM算法
- 基于python的人脸检测
-
li
nk Prediction相似性计算方法.rar - 利用Python创建ArcGis批量输出地图影工
- 利用Python创建ArcGis批量裁剪栅格工具
- 利用Python创建ArcGis裁剪工具
- PSO算法Python实现自定义函数
- Python的Nmap模块
- 教务管理系统
- python实现Apriori算法apriori.py和数据
- opc python 读取代码
- Python+Flask+sqlite实现的选课系统
- wxpython使用matplotlib的简单教程
- geoserver-rest-python-1.1
- leetcode python题解
- 基于SVM的手写字体识别Python版本
- Python RWR 可重启随机游走代码
- Python→Transorflow猫狗识别完整代码,附
- RPi.GPIO-0.6.3.tar
- python+rabird.winio实现驱动级模拟按键
- 基于wxPython和PySerial实现的串口助手
- Python Keras库 安装包
- python实现网络爬虫 爬取北上广深的天
- 正确可用的基于python实现的贝塞尔曲
- python3抓取头条新闻源码
- python.txt
- python版本selenium webdriver api
评论
共有 条评论