我做了一个问答游戏,向玩家提出一些问题,如果点击了错误的答案或者问题是否完成,游戏结束时会在警告对话框中显示分数。 我想要进一步做的是,获得所有比赛的游戏并将其添加到记分板并显示从高到低的分数,并在每次玩新游戏时附加社交。我试图使用sqlite在数据库中创建一个表。但我不知道如何将分数链接到该数据库类并将它们添加到记分板中。
这是主要的游戏活动。关于游戏的所有代码都在这里
Bitmap DevImjBmpFrmTxtFnc(String TxtSrgPsgVal)
{
int TxtSyzVal = 100;
TextPaint PenPytVaj = new TextPaint(ANTI_ALIAS_FLAG);
PenPytVaj.setTextSize(TxtSyzVal);
PenPytVaj.setColor(Color.BLACK);
PenPytVaj.setTextAlign(Paint.Align.LEFT);
Rect TxtRctVar = new Rect();
PenPytVaj.getTextBounds(TxtSrgPsgVal, 0, TxtSrgPsgVal.length(), TxtRctVar);
Bitmap TxtImjBmpVar = Bitmap.createBitmap(TxtSyzVal * 2, TxtSyzVal * 2, Bitmap.Config.ARGB_8888);
Canvas ImjCanvasVaj = new Canvas(TxtImjBmpVar);
float XcoVal = ImjCanvasVaj.getWidth() / 2 - TxtRctVar.width() / 2 - TxtRctVar.left;
float YcoVal = ImjCanvasVaj.getHeight() / 2 + TxtRctVar.height() / 2 - TxtRctVar.bottom;
ImjCanvasVaj.drawText(TxtSrgPsgVal, XcoVal, YcoVal, PenPytVaj);
return TxtImjBmpVar;
}
这是分数类
package com.example.sudhe.quizgame3;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class GameActivity extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
ImageView QuestionImage;
TextView score;
List<CountryItem> list;
private int mScore = 0;
Random r;
int turn = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
r = new Random();
QuestionImage = (ImageView)findViewById(R.id.QuestionImage);
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
answer3 = (Button)findViewById(R.id.answer3);
answer4 = (Button)findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
score.setText("Score: " + mScore);
list = new ArrayList<>();
for (int i=0; i < new DataBase().answers.length; i++){
list.add(new CountryItem(new DataBase().answers[i], new DataBase().flags[i]));
}
Collections.shuffle(list);
newQuestion(turn);
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer1.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())){
mScore++;
score.setText("Score: " + mScore);
if (turn < list.size()){
turn++;
newQuestion(turn);
}else{
gameOver();
}
}else {
gameOver();
}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer2.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())){
mScore++;
score.setText("Score: " + mScore);
if (turn < list.size()){
turn++;
newQuestion(turn);
}else{
gameOver();
}
}else {
gameOver();
}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer3.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())){
mScore++;
score.setText("Score: " + mScore);
if (turn < list.size()){
turn++;
newQuestion(turn);
}else{
gameOver();
}
}else {
gameOver();
}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer4.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())){
mScore++;
score.setText("Score: " + mScore);
if (turn < list.size()){
turn++;
newQuestion(turn);
}else{
gameOver();
}
}else {
gameOver();
}
}
});
}
private void newQuestion(int number){
QuestionImage.setImageResource(list.get(number - 1).getImage());
int correct_answer = r.nextInt(4) + 1;
int firstButton = number - 1;
int secondButton;
int thirdButton;
int fourthButton;
switch (correct_answer){
case 1:
answer1.setText(list.get(firstButton).getName());
do{
secondButton = r.nextInt(list.size());
}while (secondButton == firstButton);
do{
thirdButton = r.nextInt(list.size());
}while (thirdButton == secondButton || thirdButton == firstButton);
do{
fourthButton = r.nextInt(list.size());
}while (fourthButton == thirdButton || fourthButton == secondButton || fourthButton == firstButton);
answer2.setText(list.get(secondButton).getName());
answer3.setText(list.get(thirdButton).getName());
answer4.setText(list.get(fourthButton).getName());
break;
case 2:
answer2.setText(list.get(firstButton).getName());
do{
secondButton = r.nextInt(list.size());
}while (secondButton == firstButton);
do{
thirdButton = r.nextInt(list.size());
}while (thirdButton == secondButton || thirdButton == firstButton);
do{
fourthButton = r.nextInt(list.size());
}while (fourthButton == thirdButton || fourthButton == secondButton || fourthButton == firstButton);
answer1.setText(list.get(secondButton).getName());
answer3.setText(list.get(thirdButton).getName());
answer4.setText(list.get(fourthButton).getName());
break;
case 3:
answer3.setText(list.get(firstButton).getName());
do{
secondButton = r.nextInt(list.size());
}while (secondButton == firstButton);
do{
thirdButton = r.nextInt(list.size());
}while (thirdButton == secondButton || thirdButton == firstButton);
do{
fourthButton = r.nextInt(list.size());
}while (fourthButton == thirdButton || fourthButton == secondButton || fourthButton == firstButton);
answer2.setText(list.get(secondButton).getName());
answer1.setText(list.get(thirdButton).getName());
answer4.setText(list.get(fourthButton).getName());
break;
case 4:
answer4.setText(list.get(firstButton).getName());
do{
secondButton = r.nextInt(list.size());
}while (secondButton == firstButton);
do{
thirdButton = r.nextInt(list.size());
}while (thirdButton == secondButton || thirdButton == firstButton);
do{
fourthButton = r.nextInt(list.size());
}while (fourthButton == thirdButton || fourthButton == secondButton || fourthButton == firstButton);
answer2.setText(list.get(secondButton).getName());
answer3.setText(list.get(thirdButton).getName());
answer1.setText(list.get(fourthButton).getName());
break;
}
}
private void gameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(GameActivity.this);
alertDialogBuilder
.setMessage("Game Over! Your Score is " + mScore + "points.")
.setCancelable(false)
.setPositiveButton("NEW GAME",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
这是我使用sqlite
创建的数据库
package com.example.sudhe.quizgame3;
/**
* Created by sudhe on 19-05-2017.
*/
public class Score {
int id;
String game_score;
public Score(int i, String string, String cursorString){
}
public Score( String game_score){
this.game_score = game_score;
}
public Score(int id, String game_score){
this.id = id;
this.game_score = game_score;
}
public Score() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGame_score() {
return game_score;
}
public void setGame_score(String game_score) {
this.game_score = game_score;
}
}
这是我为显示分数而创建的记分板活动。这就是我陷入困境的地方。我不知道如何将游戏活动的分数添加到此。
package com.example.sudhe.quizgame3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sudhe on 19-05-2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "scoreManager";
private static final String TABLE_SCORE = "score_table";
private static final String KEY_ID = "id";
private static final String KEY_SCORE = "score";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_SCORE_TABLE ="CREATE TABLE " + TABLE_SCORE + " (" +
KEY_ID + " INTEGER PRIMARY KEY," + KEY_SCORE + " TEXT"
+ ")";
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);
onCreate(db);
}
void addScore(Score score){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SCORE, score.getGame_score());
db.insert(TABLE_SCORE, null, values);
db.close();
}
Score getScore(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SCORE, new String[] {KEY_ID, KEY_SCORE}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null){
cursor.moveToFirst();
}
Score score = new Score(Integer.parseInt(cursor.getString(0)), cursor.getString(1));
return score;
}
public List<Score> getAllScores(){
List<Score> scoreList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_SCORE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do{
Score score = new Score();
score.setId(Integer.parseInt(cursor.getString(0)));
score.setGame_score(cursor.getString(1));
scoreList.add(score);
}while (cursor.moveToNext());
}
return scoreList;
}
public int updatescore(Score score){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SCORE, score.getGame_score());
return db.update(TABLE_SCORE, values, KEY_ID + "=?",
new String[]{String.valueOf(score.getId())});
}
public void deletescore(Score score){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SCORE, KEY_ID + "=?",
new String[]{String.valueOf(score.getId())});
db.close();
}
public int getScoreCount(){
String countQuery = "SELECT * FORM " + TABLE_SCORE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
}