Ir para conteúdo
Fórum Script Brasil
  • 0

Setas


Denis_Rave

Pergunta

9 respostass a esta questão

Posts Recomendados

  • 0

Sim, elas têm (5 é para cima, 24 é para baixo, 19 é esquerda e 4 é direita), mas não adianta tentar detectá-las pelo evento KeyPress. Você deve detectá-las pelos eventos KeyDown ou KeyUp e basta usar as constantes vbkeyXXXX (onde "XXXX" é o nome da tecla). Exemplo

select case keycode
  case vbkeyup
    msgbox "Cima"
  case vbkeydown
    msgbox "Baixo"
end select

Abraços,

Graymalkin

Link para o comentário
Compartilhar em outros sites

  • 0

Acho que você ainda não entendeu o conceito de "eventos". O evento KeyDown é chamado (ou seja, o código que está dentro dele é executado) a cada vez que uma tecla é pressionada no controle. Se o evento tem o seguinte código:

msgbox "Alô"
Será exibida essa mensagem a cada vez que uma tecla for pressionada no controle. Todavia, o evento KeyDown tem um parâmetro, chamado KeyCode, que nos informa qual a tecla digitada. Esse parâmetro pode ser alterado, por exemplo:
KeyCode=vbKeyB
Se você colocar isso no evento KeyDown de uma textbox, por exemplo, fará com que a cada vez que uma tecla seja pressionada no controle apareça na caixa de texto a letra "B". Portanto, se colocamos o seguinte código no evento KeyDown:
If KeyCode = vbKeyDown Then
  KeyCode = 0
End If
Estamos simplesmente fazendo com que a cada vez que uma tecla seja pressionada no controle ele teste se a tecla digitada foi a seta para baixo, e caso seja realmente esta tecla ele muda o valor para 0 (que é uma tecla nula). Ou seja, ela não *bloqueia* permanentemente uma tecla e sim faz o teste a cada que qualquer tecla é pressionada e age de acordo. Portanto, a sua pergunta sobre "como habilitá-la novamente" não faz muito sentido. O que você poderia fazer é ter uma variável booleana que diria se a tecla deve ser aceita ou não, exemplo:
if not pode
    if KeyCode = vbKeyDown Then
      KeyCode = 0
    endif
endif

Nesse caso, "pode" é uma variável booleana (ou seja, só pode ser True ou False). Se ela for True, nenhuma tecla será bloqueada. Caso ela seja False, então verificar-se-á se a tecla digitada corresponde a tecla da seta para baixo e caso positivo, então ela será bloqueada.

Abraços,

Graymalkin

Link para o comentário
Compartilhar em outros sites

  • 0

Olá amigos!

Estou aprendendo Visual Basic, utilizando o Visual Studio 2010 e hoje me deparei com a mesma necessidade e dificuldade. Depois de muito pesquisar e fazer testes, consegui o que precisava.

No meu caso eu usei esta função para criar um jogo da galinha (picturebox) atravessando a rua na vertical enquanto os carros (picturebox) passam na horizontal. Na ultima aula o professor ensinou a fazer os carros andarem automaticamente ao abrir o programa, mas não deu tempo de ele ensinar a controlar a galinha somente usando as setas do teclado.

Não sei se a sua situação é a mesma, mas talvez funcione.

1 - Você deve clicar no formulário onde irá acontecer a ação, localizar a propriedade "KeyPreview" e alterar para True. (desta forma o seu formulário passa a reconhecer o que é digitado);

2 - Ainda com o formulário selecionado, vá no Eventos deste formulário (ícone de um raio) e procure o evento KeyDown ou KeyUp (este método vai funcionar em qualquer um desses). Dê um duplo clique sobre o nome para abrir a linha de código.

3 - No código fonte, dentro do evento que você clicou, você vai digitar um If (ou Select Case, o que você achar melhor. No meu caso usei um If), e vai digitar o seguinte código:

If e.KeyValue = Keys.Up Then
moverGalinhaParaCima(pctGalinha, 10)
End If

Este If irá validar se a tecla "seta para cima" foi pressionada. Caso sim, o código irá chamar a função que moverá a picturebox da Galinha em 10 pixels. Lembrando que não há a necessidade de criar uma função para isto, você pode digitar toda a ação dentro do If.

Depois para as outras setas segue o mesmo raciocínio, basta mudar para Keys.Down, Keys.Left ou Keys.Right

Agora só me falta bolar o raciocínio em que o jogo detecta que a picturebox da galinha encostou na picturabox do carro para fazer com que dê Game Over. rsrsrs

Link para o comentário
Compartilhar em outros sites

  • 0

esse exemplo está em delphi, mas voce pode observar a lógica do jogo... que será a mesma para o VB

program ChickenCrossing;
{$APPTYPE CONSOLE}
{Time to code and test 1 hour, 6 minutes}
{bugs fixed:
1. Error 104 - forgot reset(f);
2. Hang - forgot f in readln(f,width,length, etc. statement
3. Incorrect output - toca and tocd multiplied by chickenspeed instead of divide
4. Incorrect output - tod field incorrect, same error, multiply by speed
instead of divide
5. Incorrect output - forgot to add start time (i+ ...) in toca, tocd calc
6. Incorrect output - read variables in wrong order readln(f, length, width...);
instead of readln(f,width length...);

}

uses
SysUtils;

{Problem Description

Why did the chicken cross the road? Well, that is not our concern. We only seek
to make sure that it gets across safely.

The chicken will cross several roads (anything from Farm-to-Market roads to interstate
highways including access roads). For each road to cross, the chicken looks
to see how many lanes there are to cross and what traffic there is on each lane.
The chicken then starts waiting for traffic to clear so it can cross safely. In order to
maintain the timing between steps and his head-bobbing, the chicken will only start
walking on 1 second intervals. However, this chicken is particularly impatient and
will start walking whether it is safe or not after waiting only 60 seconds and it
may be time to break out the barbeque sauce.

When the chicken does cross the road, it will start walking on a path perpendicular
to the lanes (all lanes are parallel) at a constant rate of 10 feet/second and will not
stop until reaching the other side. All lanes are 15 feet wide and there is no room
between the lanes. Each vehicle description includes a width in feet of the vehicle
(0<W<=15 feet), the length of the vehicle (0<L<=40 feet), a constant speed of the
vehicle in feet/second (0<S<=100 feet/second), and the time (in seconds) which
the vehicle will cross the line which the chicken will attempt to walk along.
Second zero is the first opportunity for the chicken to start across the road. Vehicles
always drive down the exact center of the lane they occupy (no drunk drivers here!).

Information for each crossing will be formatted in the input file as follows.
The first line for each crossing will contain the number of lanes (1<=L<=20) to be crossed
(direction does not matter here). The second line contains the number of vehicles
(0<=N<=100) involved in the crossing. Each of the subsequent N lines contains the
description of the vehicles in no particular order. The first value of the line
is an integer which identifies the lane number (1 to L) the vehicle occupies
(lane 1 is closest to the chicken). The second through fifth values are floating
point numbers which identify the width, length and speed of the vehicle and the
time (in seconds) that the vehicle will cross the line the chicken will walk
along. You must read to the end-of-file.

You may assume that there are no lane changes; collisions between vehicles are
irrelevant; and sufficient safety margin has been included in the dimensions of the
vehicle to treat the chicken as a single point. It is time to get the barbeque
sauce if at any time any part of the vehicle and the single point of the chicken intersect.

For each road to cross, you must output the crossing number (start counting at
one) and an integer representing the earliest second (0 to 60) the chicken should start
to cross such that it can cross safely. If the chicken cannot cross safely, you
must print the message "Bar-B-Q time!" instead of the crossing start time. Your output
should be formatted similar to that in the examples below.

Sample Input:
2
8
2 5.0 6.3 12.0 47.7
1 14.5 39.6 66.0 29.3
2 14.8 40.0 9.0 4.8
1 11.1 40.0 100.0 24.3
1 9.0 14.2 83.6 2.1
1 9.0 15.0 88.0 1.1
2 12.6 29.9 80.67 19.25
1 3.0 6.0 40.0 10.6
6
1
4 15.0 40.0 0.1 4.4
6
1
2 15.0 40.0 0.1 4.4

Sample Output:

Crossing 1 should start at 8.
Crossing 2 Bar-B-Q time!
Crossing 3 should start at 0.
}

type
TTruck=class
length,width,speed,toa,tod:single;
lane:integer;
end;

var
chickspeed:integer=10;
lanewidth:integer=15;
f:text;
i:integer;
nbrlanes:integer;
nbrtrucks:integer;
trucks:array[1..100] of TTruck;
casenbr:integer=0;

procedure computeanswer;
var
i,j:integer;
toca,tocd:single; {time of chicked arrival & departure from a truck wide space}
ok1,ok2,solved:boolean;
begin
solved:=false;
for i:= 0 to 60 do
begin
ok2:=true;
for j:=1 to nbrtrucks do
with trucks[j] do
begin
ok1:=false;
toca:=i+((lane-1)*lanewidth+lanewidth/2-width/2)/chickspeed;
tocd:=i+((lane-1)*lanewidth+lanewidth/2+width/2)/chickspeed;
if tocd<toa then OK1 :=true;
If toca>tod then OK1 :=true;
if not OK1 then
begin
ok2:=false;
break;
end;
end;
if ok2 then {solution found}
begin
writeln('Crossing '+inttostr(casenbr)+' should start at '+inttostr(i));
solved:=true;
break;
end;
end;
if not solved then writeln('Crossing '+inttostr(casenbr)+' Bar-B-Q time!');
end;



begin
assignfile(f,'Data.txt');
reset(f);
while not eof(f) do
begin
readln(f,nbrlanes);
readln(f,nbrtrucks);
for i:= 1 to nbrtrucks do
begin
trucks:=TTRuck.create;
with trucks do
begin
readln(f,lane,width,length,speed,toa);
tod:=toa+length/speed;
end;
end;
inc(casenbr);
computeanswer;
end;
readln;
end.

abraço

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...