- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
<分区>
我刚刚使用 pygame 在 python 中实现了一个非常基本的俄罗斯方 block 游戏,希望任何人都可以建设性地批评我。
这是我的第一个相当长的程序,所以肯定会有错误。我也无法完全理解 pygame 事件处理,因此键盘控制可能不可靠,但请尝试一下。
任何建设性的批评或建议都将受到高度重视。
###############################################################################
## an implementation of a ***very*** basic tetris game in python using pygame
###############################################################################
'''rotate --- r
pause ---- p
direction buttons for movement'''
import sys
import pygame
import random
size = width, height = 200, 400
color = {'black': (0, 0, 0), 'white':(255, 255, 255)}
sqrsize = 20
occupied_squares = []
top_of_screen = (0, 0)
top_x, top_y = top_of_screen[0], top_of_screen[1]
num_block = 4
pen_size = 1
mov_delay, r_delay = 300, 50
board_centre = 80
no_move = 0
events = {276: 'left', 275: 'right', 112: 'pause'}
pygame.init()
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((color['white']))
screen.blit(background, top_of_screen)
pygame.display.flip()
def tetris():
''' this is the controller of the whole game for now, maybe changed
later on as game architecture changes.'''
global mov_delay
while True:
curr_shape = create_newshape(board_centre)
l_of_blcks_ind = blck_x_axis = 0
shape_name_ind = blck_y_axis = 1
move_dir = 'down' #default move direction
game = 'playing' #default game state play:- is game paused or playing
shape_blcks = [pygame.Rect(block[blck_x_axis], block[blck_y_axis],
sqrsize, sqrsize) for block in curr_shape[l_of_blcks_ind]]
if legal(shape_blcks):
draw_shape(shape_blcks)
else:
break #game over
while True:
if game == 'paused':
for event in pygame.event.get(pygame.KEYDOWN):
if event.key == pygame.K_p:
game = 'playing'
else:
for event in pygame.event.get((pygame.KEYDOWN, pygame.KEYUP,
pygame.QUIT)):
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
mov_delay = 50
continue
elif event.key == pygame.K_r:
shape_blcks = rotate(shape_blcks, curr_shape[shape_name_ind])
draw_shape(shape_blcks)
pygame.time.delay(r_delay)
continue
elif event.key == pygame.K_p:
game = 'paused'
move_dir = events[event.key]
break
else:
if event.key in events.keys():
mov_delay = 50
move_dir = events[event.key]
move (shape_blcks, move_dir)
draw_shape(shape_blcks)
pygame.time.delay(mov_delay)
continue
elif event.type == pygame.KEYUP:
if mov_delay != 300:
mov_delay = 300
move_dir = 'down'
moved = move(shape_blcks, move_dir)
draw_shape(shape_blcks)
pygame.time.delay(mov_delay)
'''if block did not move and the direction for movement is down
then shape has come to rest so we can exit loop and then a new
shape is generated. if direction for movement is sideways and
block did not move it should be moved down rather'''
if not moved and move_dir == 'down':
for block in shape_blcks:
occupied_squares.append((block[blck_x_axis],
block[blck_y_axis]))
break
else:
draw_shape(shape_blcks)
pygame.time.delay(mov_delay)
for row_no in range (height - sqrsize, 0, -sqrsize):
if row_filled(row_no):
delete_row(row_no)
def draw_shape(shp_blcks):
'''this draws list of blocks or a block to the background and blits
background to screen'''
if isinstance(shp_blcks, list):
for blck in shp_blcks:
pygame.draw.rect(background, color['black'], blck, pen_size)
else:
pygame.draw.rect(background, color['black'], shp_blcks, pen_size)
screen.blit(background, top_of_screen)
pygame.display.update()
def row_filled(row_no):
'''check if a row is fully occupied by a shape block'''
for x_coord in range(0, width, sqrsize):
if (x_coord, row_no) in occupied_squares:
continue
else:
return False
return True
def delete_row(row_no):
'''removes all squares on a row from the occupied_squares list and then
moves all square positions which have y-axis coord less than row_no down
board'''
global occupied_squares
new_buffer = []
x_coord, y_coord = 0, 1
background.fill(color['white'])
for sqr in occupied_squares:
if sqr[y_coord] != row_no:
new_buffer.append(sqr)
occupied_squares = new_buffer
for index in range(len(occupied_squares)):
if occupied_squares[index][y_coord] < row_no:
occupied_squares[index] = (occupied_squares[index][x_coord],
occupied_squares[index][y_coord] + sqrsize)
for sqr in occupied_squares:
rect = pygame.Rect(sqr[x_coord], sqr[y_coord], sqrsize, sqrsize)
draw_shape(rect)
def move(shape_blcks, direction):
'''input:- list of blocks making up a tetris shape
output:- list of blocks making up a tetris shape
function moves the input list of blocks that make up shape and then checks
that the list of blocks are all in positions that are valide. position is
valid if it has not been occupied previously and is within the tetris board.
If move is successful, function returns the moved shape and if move is not
possible, function returns a false'''
directs = {'down':(no_move, sqrsize), 'left':(-sqrsize, no_move),
'right':(sqrsize, no_move), 'pause': (no_move, no_move)}
delta_x, delta_y = directs[direction]
for index in range(num_block):
shape_blcks[index] = shape_blcks[index].move(delta_x, delta_y)
if legal(shape_blcks):
for index in range(num_block):
background.fill((color['white']), shape_blcks[index].move(-delta_x,
-delta_y))
return True
else:
for index in range(num_block):
shape_blcks[index] = shape_blcks[index].move(-delta_x, -delta_y)
return False
def legal(shape_blcks):
'''input: list of shape blocks
checks whether a shape is in a legal portion of the board as defined in the
doc of 'move' function'''
blck_x_axis, blck_y_axis = 0, 1
for index in range(num_block):
new_x, new_y = (shape_blcks[index][blck_x_axis],
shape_blcks[index][blck_y_axis])
if (((new_x, new_y) in occupied_squares or new_y >= height) or
(new_x >= width or new_x < top_x)): #probly introduced a bug by removing the check for shape being less that y-axis origin
return False
return True
def create_newshape(start_x=0, start_y=0):
'''A shape is a list of four rectangular blocks.
Input:- coordinates of board at which shape is to be created
Output:- a list of the list of the coordinates of constituent blocks of each
shape relative to a reference block and shape name. Reference block has
starting coordinates of start_x and start_y. '''
shape_names = ['S', 'O', 'I', 'L', 'T']
shapes = {'S':[(start_x + 1*sqrsize, start_y + 2*sqrsize),
(start_x, start_y), (start_x, start_y + 1*sqrsize),(start_x + 1*sqrsize,
start_y + 1*sqrsize)],
'O':[(start_x + 1*sqrsize, start_y + 1*sqrsize), (start_x, start_y),
(start_x, start_y + 1*sqrsize), (start_x + 1*sqrsize, start_y)],
'I':[(start_x, start_y + 3*sqrsize), (start_x, start_y),
(start_x, start_y + 2*sqrsize), (start_x, start_y + 1*sqrsize)],
'L':[(start_x + 1*sqrsize, start_y + 2*sqrsize), (start_x, start_y),
(start_x, start_y + 2*sqrsize), (start_x, start_y + 1*sqrsize)],
'T':[(start_x + 1*sqrsize, start_y + 1*sqrsize),(start_x, start_y),
(start_x - 1*sqrsize, start_y + 1*sqrsize),(start_x,
start_y + 1*sqrsize)]
}
a_shape = random.randint(0, 4)
return shapes[shape_names[a_shape]], shape_names[a_shape]
#return shapes['O'], 'O' #for testing
def rotate(shape_blcks, shape):
'''input:- list of shape blocks
ouput:- list of shape blocks
function tries to rotate ie change orientation of blocks in the shape
and this applied depending on the shape for example if a 'O' shape is passed
to this function, the same shape is returned because the orientation of such
shape cannot be changed according to tetris rules'''
if shape == 'O':
return shape_blcks
else:
#global no_move, occupied_squares, background
blck_x_axis, blck_y_axis = 0, 1
shape_coords = [(block[blck_x_axis], block[blck_y_axis]) for
block in shape_blcks]
ref_shape_ind = 3 # index of block along which shape is rotated
start_x, start_y = (shape_coords[ref_shape_ind][blck_x_axis],
shape_coords[ref_shape_ind][blck_y_axis])
new_shape_blcks = [(start_x + start_y-shape_coords[0][1],
start_y - (start_x - shape_coords[0][0])),
(start_x + start_y-shape_coords[1][1],
start_y - (start_x - shape_coords[1][0])),
(start_x + start_y-shape_coords[2][1],
start_y - (start_x - shape_coords[2][0])),
(shape_coords[3][0], shape_coords[3][1])]
if legal(new_shape_blcks):
for index in range(num_block): # paint over the previous shape
background.fill(color['white'], shape_blcks[index])
return [pygame.Rect(block[blck_x_axis], block[blck_y_axis],
sqrsize, sqrsize) for
block in new_shape_blcks]
else:
return shape_blcks
if __name__ == '__main__':
tetris()
我正在尝试创建一个数据库来处理我在 Play 中的任务!框架。 这是我所拥有的: 在build.sbt中: libraryDependencies ++= Seq( jdbc, cache,
在我的游戏中定义一个表单!当编译器吐出这个奇怪的错误时 Controller :重载方法值映射与替代:...[一堆废话]...Error occurred in an application invo
我的应用程序有问题,@Max约束注释。 我的 Controller 方法定义如下: public static void save(@Required @Max(255) String content
我想创建一个像这样的标签: #{some_tag entity:user, field:'name'} 并期望它通过使用如下表达式生成带有用户名的输出: ${_entity._field} 我知道这行
我创建了一些 Model 对象来代表一家拥有多个客户的公司,以及一个由公司和客户组合以及多个发票行组成的发票对象。我创建了以下模型对象: @Entity public class Company ex
Playframework 现在是 typesafe-stack 的一部分。 那么,如果我要使用像主要语言一样的 Scala ,我现在应该下载什么? TypsafeStack 还是 PlayFrame
在玩!如果你这样称呼: void method() { User u = User(); u.name = "bob"; u.save(); while(true){/* endless loop *
我正在 Play 中构建一个应用程序!包含大量我想跟踪更改的数据的框架。在企业解决方案中,我可能会使用数据库触发器将更改复制到历史表中以跟踪这些更改。我不熟悉 Play!/JPA 中的类似范例,但也许
我一直在学习JavaScript技能,但是遇到一个问题,当单击此处是我的代码时,音频没有被播放。 Your browser does no
我想实现在某些模型保存后在表中插入一行的行为。我当前的解决方案简而言之是:(这只是我的代码示例,因此请不要评论数据库或描述符模型的正确性)。 我有一个监听器,用于在更新/插入实体上插入行为行 Desc
如何使我的模型类字段独一无二?例如。如果已经登录,我想为用户显示正确的消息。我必须自己编写验证检查并使用它,或者可以使用 JPA @UniqueConstraint? 最佳答案 我是这样做的: @En
我使用的是 Play 1.2.1。我想对我的用户密码进行哈希处理。我认为 Crypto.passwordHash 会很好,但事实并非如此。 passwordHash 文档说它返回 MD5 密码哈希值。
我一直在研究戏剧!框架模块并希望扩展它,添加一些功能。我发现了一个从Enhancer(play.classloading.enhancers.Enhancer)扩展的类,但不明白为什么Play!采用了
我使用的是 Play Framework 1.2.5。我有几个与 NAme 和 Age 字段相关的验证。年龄验证无法正常工作。即使年龄大于 18 岁,我也会收到错误消息。 下面是action方法中的验
我使用的是 Play Framework 1.2.5。两者有什么区别: @{Application.render()} 和 @Application.render() 第一个最好用在表单 Action
我是新来的!我被一些总是有错误的表格所困扰。即使所有字段都已填写,我也无法弄清楚问题是什么。 路线 GET /products/ controllers.Pr
我显示可编辑的数据库表行的列表。我想允许用户编辑显示表中的数据并同时保存所有更新。我应该如何取回 Controller 的更新列表? 最佳答案 由于 Play 可以绑定(bind)到 POJO,也可以
那么,假设我从 Controller 异步启动一个作业,然后渲染一些模板。 MyJob job = new MyJob(); job.doJob(); render(); 我的工作看起来像: 导入 p
当前使用的 Play Framework 为 2.0.4。当我尝试使用此命令升级到 2.6.21 时: addSbtPlugin("com.typesafe.play"% "sbt-plugin"%
我目前正在与 Play 合作!框架和看来日志记录只适用于游戏!仅但对于具有 LOGGER 初始化的类不起作用。 这是 logback.xml ${application.hom
我是一名优秀的程序员,十分优秀!