Thursday, December 8, 2022

My First Blog my first love Story part 1

Mera naam Pooja hai mai abhi 22 saal ki ho chuki hu . Mai abhi Noida me rahti hu .
               To chaliye suru karte hai
Kahani suru hoti hai jab mai 17 saal ki hui thi tab .
Ek din mai Apni mammy ke sath badarpur,Delhi gaye huye the buaa ji ke pas wahi samne ek ladka rahta tha us di barish bhi ho rahi thi wo ladka chhat par barish me naha raha tha . Mai use dekh rahi thi wo dekhne me bahut achha lag raha tha .
Tabhi usne mujhe dekh liya use pata chal gaya ki Mai use dekh rahi hu.
Or wo bhi mujhe dekhne laga mujhe usse baat karni thi par kaise karti or mammy or mai barish rukne ka intezar kar rahe the taki apne ghar ja sake . Mera Mann to nahi tha janeka Mann to tha kisi tarike se bas usse baat ho pr kya karti. barish ruki or hum ghar chale gaye.
Us din to mai bas issi ke baare me sochti rahi
Din bit gaya or mai apne kaam me lag gai or bhul gai lekin 4 din baad buaa ki ladki ka phone aaya....... Aage ki kahani kal bataungi 
Bye love you all



Labels:

Friday, December 6, 2019

Simple car racing android game in Sketchware

To create a simple car racing game in sketchware follow the steps given below.

1. Create a new project in Sketchware.

2. In VIEW area of main.xml add a TextView textview3 for displaying score of previous game, a TextView textview4 for displaying high score, and a Button button1 for starting game.

3. In MainActivity, add a Shared Preferences component sp:sp.

4. Add onStart event and put blocks to set the text of textview3 and textview4.

5. Create a new page page2.xml.

6. On button1 click use intent to move to page2.

7. On page2.xml add a LinearV linear1 with padding 0 and width and height MATCH_PARENT.

8. In Page2Activity, add a Shared Preferences component sp:sp.

9. Create a number variable highscore.

10. In onCreate event of Page2Activity, use blocks to set the value of number variable highscore, and use codes to create a new GameView and add it to linear1, as shown in image below.

The code used in the add source directly block is:
GameView bcv = new GameView(this);
linear1.addView(bcv);

11. Create a more block extra.

12. In the more block extra, use an add source directly block and put codes to create a new View class GameView.
}

public class GameView extends View{
private Paint myPaint;
private int speed=1;
private int time = 0;
private int score = 0;
private int myCarPosition = 0;
private ArrayList<HashMap<String, Object>> otherCars = new ArrayList<>();

int viewWidth = 0;
int viewHeight = 0;

public GameView(Context context){
super(context);
myPaint = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
viewWidth = this.getMeasuredWidth();
viewHeight = this.getMeasuredHeight();

if (time%700 < 6 + 2*speed){
HashMap<String, Object> map = new HashMap<>();
map.put("lane", getRandom(0,2));
map.put("startTime", time);
otherCars.add(map);
}

time = time + 6 + 2*speed;

int carWidth = viewWidth/5;
int carHeight = carWidth+10;

myPaint.setStyle(android.graphics.Paint.Style.FILL);

myPaint.setColor(Color.RED);
canvas.drawRect((myCarPosition*viewWidth/3) + viewWidth/15, viewHeight - 2 -carHeight, (myCarPosition*viewWidth/3) + (viewWidth/15) + carWidth, viewHeight-2, myPaint);

myPaint.setColor(Color.GREEN);

for (int i = 0; i<otherCars.size(); i++){
int carX = ((int)otherCars.get(i).get("lane")*viewWidth/3) + viewWidth/15;
int carY = time - (int)otherCars.get(i).get("startTime");

canvas.drawRect(carX, carY-carHeight, carX + carWidth, carY, myPaint);

if ((int)otherCars.get(i).get("lane") == myCarPosition){
if (carY > viewHeight - 2 - carHeight && carY < viewHeight - 2){
sp.edit().putString("hs", String.valueOf(highscore)).commit();
sp.edit().putString("score", String.valueOf(score)).commit();
finish();
}}

if (carY > viewHeight + carHeight){
otherCars.remove(i);
score++;
speed = 1 + Math.abs(score/10);
if (score > highscore){
highscore = score;
}
}
}

myPaint.setColor(Color.BLACK);
myPaint.setTextSize(40);
canvas.drawText("Score: " + String.valueOf(score), 80f, 80f, myPaint);
canvas.drawText("Speed: " + String.valueOf(speed), 380f, 80f, myPaint);

invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x1 = event.getX();
if ( x1<viewWidth/2){
if (myCarPosition>0){
myCarPosition--;
}
}
if ( x1>viewWidth/2){
if (myCarPosition<2){
myCarPosition++;
}
}
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;

}

13. Save and Run the project.

 


To replace the Rectangles by images of cars:
* Add images of cars using image manager. Example car_blue, and car_yellow.
* Replace the code in More Block with following code:

}

public class GameView extends View{
private Paint myPaint;
private int speed=1;
private int time = 0;
private int score = 0;
private int myCarPosition = 0;
private ArrayList<HashMap<String, Object>> otherCars = new ArrayList<>();

int viewWidth = 0;
int viewHeight = 0;

public GameView(Context context){
super(context);
myPaint = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
viewWidth = this.getMeasuredWidth();
viewHeight = this.getMeasuredHeight();

if (time%700 < 10 + speed){
HashMap<String, Object> map = new HashMap<>();
map.put("lane", getRandom(0,2));
map.put("startTime", time);
otherCars.add(map);
}

time = time + 10 + speed;

int carWidth = viewWidth/5;
int carHeight = carWidth+10;

myPaint.setStyle(android.graphics.Paint.Style.FILL);

android.graphics.drawable.Drawable d = getResources().getDrawable(R.drawable.car_blue, null);
d.setBounds((myCarPosition*viewWidth/3) + viewWidth/15 + 25, viewHeight - 2 -carHeight, (myCarPosition*viewWidth/3) + (viewWidth/15) + carWidth-25, viewHeight-2);
d.draw(canvas);

myPaint.setColor(Color.GREEN);

for (int i = 0; i<otherCars.size(); i++){
int carX = ((int)otherCars.get(i).get("lane")*viewWidth/3) + viewWidth/15;
int carY = time - (int)otherCars.get(i).get("startTime");

android.graphics.drawable.Drawable d2 = getResources().getDrawable(R.drawable.car_yellow, null);
d2.setBounds(carX+25, carY-carHeight, carX + carWidth-25, carY);
d2.draw(canvas);

if ((int)otherCars.get(i).get("lane") == myCarPosition){
if (carY > viewHeight - 2 - carHeight && carY < viewHeight - 2){
sp.edit().putString("hs", String.valueOf((int)highscore)).commit();
sp.edit().putString("score", String.valueOf(score)).commit();
finish();
}}

if (carY > viewHeight + carHeight){
otherCars.remove(i);
score++;
speed = 1 + Math.abs(score/8);
if (score > highscore){
highscore = score;
}
}
}

myPaint.setColor(Color.WHITE);
myPaint.setTextSize(40);
canvas.drawText("Score: " + String.valueOf(score), 80f, 80f, myPaint);
canvas.drawText("Speed: " + String.valueOf(speed), 380f, 80f, myPaint);

invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x1 = event.getX();
if ( x1<viewWidth/2){
if (myCarPosition>0){
myCarPosition--;
}
}
if ( x1>viewWidth/2){
if (myCarPosition<2){
myCarPosition++;
}
}
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;


}

Firebase auth in Sketchware for Login Activity

To create a login activity using Firebase Auth in Sketchware, follow the steps given below. This method uses Firebase authentication service for creating login.

1. In your Firebase account, go to Firebase authentication.
2. In Firebase authentication web set-up, go to SIGN-IN method, and enable Email/password and Anonymous.

3. Go to Project settings in your Firebase project and copy the Web API Key, Project ID, and App ID.
4. Paste the Project ID, App ID, and Web API Key in your project in Sketchware, in the Firebase settings.

5. On the MainActivity page add a File Shared preferences component user:user and an Intent i. Also add a Timer and a Firebase Auth testlogin.



6. Create a new page login.xml with Activity called LoginActivity.

7. In onCreate event of MainActivity use blocks as shown in image below.

It identifies main page with File user key page. It also checks if user is logged in to FirebaseAuth.
If user is logged in then other blocks are executed.
If user is not logged in, he is taken to LoginActivity using Intent i.

8. On LoginActivity VIEW area,
* add two Linear(H) loading_linear and linear3.

* In linear3 add two Buttons button2 and button3 for Sign in and Sign up respectively.

* Below linear3 add two Linear(V) login_linear and register_linear.

* In login_linear add edittext3 and edittext4 for email and password respectively, and add a Button login_button for Sign in.

* In register_linear add edittext7, edittext6 and edittext5 for nickname, email and password respectively. Also add register_button for Sign up.

See the image below.

9. In LOGIC area of LoginActivity, add a new File Shared preferences component user:user (same as in MainActivity),
a FirebaseDB component user_id:userid, and a Firebase Auth component testlogin (same as in MainActivity).


10. Add a List Map map1 and a String list uid.

11. Also add a Map variable map_var, and a number variable n.

12. In onCreate event of LoginActivity add blocks as shown in image below.
These blocks identify page by setting value login to file key page. They also make loading_linear VISIBLE and other linears GONE.

13. On button2 click add blocks shown in image below. These blocks make login_linear visible, and button2 red.

14. On button3 click add blocks shown in image below. These blocks make register_linear visible, and button3 red.

15. On register_button Click, add blocks as shown in image below.

These blocks check if the EditText fields are empty. If they are empty it toasts a message. If not empty, it registers the email and password of user to Firebase Auth and makes loading_linear visible.

16. Add event Firebase Auth: onCreateUserComplete and use blocks as shown in image below.
If the user is registered successfully, these blocks push his FirebaseAuth Uid, and his nickname to Firebase DB user_id. If registration fails, these blocks make register_linear VISIBLE.

17. Add a new event FirebaseDB on child added. In this event use blocks as in image below.


These blocks get data from Firebase DB to List Map map1, and then create a String list uid from all the uid in List Map map1, and make login_linear VISIBLE.

18. On  login_button click use blocks as shown in image below.

These blocks Sign in user using Firebase Auth and make loading_linear visible.

19. Add event Firebase Auth: onSignInUserComplete and use blocks as shown in image below.

If user is signed in using Firebase Auth, these blocks save his nickname, and move him to MainActivity by Finishing LoginActivity.

20. Add onBackPressed event and use following codes in it.
21. Again in MainActivity, add onStart event, and use following blocks.

22. In VIEW area of MainActivity add a button (button2) for Sign out, and on the event of button2 click, use following blocks.

23. Save and run the project.

Here the nickname entered by user and his Uid created by Firebase Auth, are saved in Firebase realtime database.

SearchView for String list in Sketchware

To implement a SearchView for a simple ListView showing a String list in Sketchware, follow the steps given below.

1. Create a new project in Sketchware.

2. In VIEW area add a ListView listview1.

3. Switch On AppCompat and Design.

4. Create a String List list.

5. Create a more block extra.

6. In the more block extra, use an add source directly block and put codes to declare a SearchView searchview and an ArrayAdapter adapter. After that add the SearchView as an OptionsMenu item.
}
android.support.v7.widget.SearchView searchview;
ArrayAdapter<String> adapter;
@Override
public boolean onCreateOptionsMenu( Menu menu) {
menu.add(0,0,0,"search").setActionView(searchview).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
{

7. In onCreate event, add items to the String List list.
8. After adding items to the list use add source directly blocks and put following codes:
i. Define the ArrayAdapter and set it as adapter of ListView.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listview1.setAdapter(adapter);

ii. Define SearchView and add it to Toolbar.
searchview = new android.support.v7.widget.SearchView(this);

iii. Set OnQueryTextListener for the SearchView.
searchview.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String text = newText;
adapter.getFilter().filter(text);
return false;
}
});

9. Save and run the project. You will see the search icon on the Toolbar. You can click on it and search the ListView.