【发布时间】:2014-06-25 22:17:06
【问题描述】:
我的代码应该在计算机随机选择的 11、15 或 19 个精灵中生成,其中一个在移动,然后停止工作。我不在乎看到精灵移动,只要它们实际上是在改变 x 坐标。他们应该只是跳到新的位置。我正在使用 python 2.7 和 pygame。如果您发现我的代码有问题,我很想听听!总的来说,我是编码新手,我希望能变得更好。谢谢! 我的代码如下:
import pygame, sys
from pygame.locals import *
import pygame
import time
import random
pygame.init()
screen = pygame.display.set_mode((720, 480))
gray = pygame.Color(220, 220, 220)
black = pygame.Color(0, 0, 0)
blue = pygame.Color(0, 0, 255)
red = pygame.Color(255, 0 ,0)
white = pygame.Color(255, 255, 255)
t_delta = 0
t_target = 60*15
t_start = time.time()
g=0
#f = open("results", "a")
#f.write("trial number, answer, reaction time, correct answer, number of balls in left circle at response, number of balls in middle circle at response, number of balls in right circle at response\n")
#f.close()
class tokens(pygame.sprite.Sprite):
def __init__(self, color, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
token_list = pygame.sprite.Group()
'''t_end = time.time() + 60*15
while time.time() < t_end: (other method for determining 15 minute timer)'''
while t_delta < t_target:
screen.fill(gray)
pygame.draw.rect(screen, black, Rect((30,140), (200,200)))
pygame.draw.rect(screen, black, Rect((260,140), (200,200)))
pygame.draw.rect(screen, black, Rect((490,140), (200,200)))
pygame.display.update()
#draws three squares on screen
if len(token_list) == 0:
n = random.randint(1,3)
if n == 1:
n=11
elif n == 2:
n = 15
elif n == 3:
n = 19
#chooses amount of tokens
x_coords = [265, 305, 345, 385, 425, 265, 305, 345, 385, 425, 265, 305, 345, 385, 425, 265, 305, 345, 385]
y_coords = [170, 170, 170, 170, 170, 210, 210, 210, 210, 210, 250, 250, 250, 250, 250, 290, 290, 290, 290]
# initial x y coords for token
left_score = 0
right_score = 0
if len(token_list) < n:
for i in range(0,n):
token = tokens((255,120,0), 20, 20)
token.rect.x = x_coords[i-1]
token.rect.y = y_coords[i-1]
token_list.add(token)
if len(token_list) == n:
token_list.draw(screen)
pygame.display.update()
for i in token_list:
token_direction = random.randint(1,2)
if token_direction == 1:
token.rect.x -= 230
pygame.display.update()
left_score += 1
time.sleep(1)
if token_direction == 2:
token.rect.x += 230
pygame.display.update()
right_score += 1
time.sleep(1)
for event in pygame.event.get():
if event.type == KEYDOWN:
g+=1
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
answer = "left"
#f = open("results", "a")
#f.write(str(g) + ", left\n")
#f.close()
pygame.draw.rect(screen, blue, Rect((30,140), (200,200)))
print("You picked left!")
pygame.display.update()
time.sleep(1)
screen.fill(black)
token_list.empty()
pygame.display.update()
time.sleep(1)
elif keys[pygame.K_RIGHT]:
answer = "right"
#f = open("results", "a")
#f.write(str(g) + ", right\n")
#f.close()
pygame.draw.rect(screen, blue, Rect((490,140), (200,200)))
print("You picked right!")
pygame.display.update()
time.sleep(1)
screen.fill(black)
token_list.empty()
pygame.display.update()
time.sleep(1)
t_delta = time.time() - t_start
【问题讨论】:
-
你没有修复我上次发布此内容时指出的相同问题。
-
我试图修改你的代码,但我不懂游戏规则。我得到了移动令牌,但它们四处移动:)
-
这不是游戏,实际上是为了科学实验。现在,代码应该在中心方块产生指定数量的标记(精灵),然后每 1 秒移动到右侧或左侧方块,在“网格”的同一插槽中,但是在另一个广场。我计划让玩家在任何点猜测他认为哪个方格会有更多标记,然后标记每 0.2 秒移动一次,而不是 1 秒。如果玩家正确,他的分数将增加 1。15 分钟后,猜对的总数就是他的分数。这有帮助吗?
-
如果您更改
token.rect.x并调用pygame.display.update()您不会移动令牌。您必须重新绘制令牌。但可能你也必须重新绘制其他元素(灰色背景、黑色矩形、所有标记) -
使用当前代码和循环中的
time.sleep,您必须等待 19 秒才能获得按键。
标签: python python-2.7 pygame sprite