Adroid记录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_27922913/article/details/49181593

这几天杭州天气不好,总是在下雨,温度比较低,整个人的心情也不是那么好,再加上这几天事情比较多,前天写的也忘了发布了,今天对过去的三天做一个小结吧,

前天写了两个小游戏,一个是在窗口随机掉落字母,键盘上输入,如果输入正确则加分,输入错误或字母掉下去就减分的游戏,是用Java写的,源代码如下:

import java.awt.*;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.*;

public class MyCharGame {

public static void main(String args[]){

JFrame w = new JFrame();

w.setSize(1000, 768);

w.setBackground(Color.BLACK);

MyPanel18 mp = new MyPanel18();

w.add(mp);

Thread t = new Thread(mp);

t.start();

w.addKeyListener(mp);

mp.addKeyListener(mp);

System.out.print(true);

w.setVisible(true);

}

}

class MyPanel18 extends JPanel implements Runnable,KeyListener{

int[] x = new int[10];

int[] y = new int[10];

char[] c = new char[10];

int grade = 2000;

public MyPanel18(){

for(int i = 0;i < 10 ; i ++){

x[i] = (int)(Math.random()*950);

y[i] = (int)(Math.random()*400);

c[i] = (char)(97+Math.random()*26);

}

}

public void paint(Graphics g){

super.paint(g);

super.setBackground(Color.BLACK);

Font f = new Font(“”,1,36);

g.setFont(f);

g.setColor(Color.RED);

g.drawString(“成绩是” + grade, 50, 50);

if(grade <= 0){

Font h = new Font(“”,1,64);

g.setFont(h);

g.drawString(“GAME OVER”, 300, 300);

grade = 0;

//for(int i=0; i < 10; i++){

//x[i]=-100;

//y[i]=0;

//}

}

for(int i = 0;i<10;i++){

g.setColor(Color.WHITE);

g.drawString(“”+c[i], x[i], y[i]);

}

}

public void run() {

while(true){

for(int i = 0;i <10;i++){

y[i]++;

if(y[i] > 780){

grade -= 100;

y[i] = 0;

x[i] = (int)(Math.random()*950);

c[i] = (char)(Math.random()*26+97);

}

}

try{

Thread.sleep(40000/grade);

}catch(Exception e){}

repaint();

}

}

@Override

public void keyTyped(KeyEvent e) {

// TODO Auto-generated method stub

}

public void keyPressed(KeyEvent e) {

char cc = e.getKeyChar();

int yy = -1;

int index = -1;

for(int i = 0; i < 10; i++){

if(c[i] == cc){

if(yy < y[i]){

yy = y[i];

index = i;

} }}

if(index == -1){

grade -= 100;

}else{

grade += 10;

y[index] = 0;

x[index] = (int)(Math.random()*950);

c[index] = (char)(Math.random()*26+97);

}

}

public void keyReleased(KeyEvent e) {

}

}

中间注释掉的是我实现的方法,上面是老师实现的方法。

下面是我根据老师讲的自己写的一个安卓小游戏,但是由于没有学习布局,游戏只限于特定分辨率的手机屏幕,是一个用木板接住小球的游戏,每接住一次加10分,掉落则游戏结束。

下面我只提供MyView方法,main方法可以自己根据其创建:

package com.example.ball;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.graphics.Typeface;

import android.os.Handler;

import android.renderscript.Font;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

public class MyView extends View implements Runnable,OnTouchListener{

float x = 30;

float y = 80;

int f = 0;

float x1 = 300;

float y1 = 800;

float grade = 100;

private Handler handler;

public void setHandler(Handler handler){

this.handler = handler;

}

public MyView(Context context) {

super(context);

}

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint p = new Paint();

canvas.drawRGB(80, 251, 15);

canvas.drawCircle(x, y, 30, p);

canvas.drawRect(x1, y1, x1+300, y1+20, p);

p.setTextSize(30);

p.setColor(Color.BLUE);

canvas.drawText(“Grade:” + grade, 600, 100, p);

if(y > 1200){

p.setTextSize(80);

p.setColor(Color.RED);

p.setTypeface(Typeface.DEFAULT_BOLD);

canvas.drawText(“GAME OVER”, 180, 400, p);

p.setTextSize(40);

p.setColor(Color.BLACK);

p.setTypeface(Typeface.DEFAULT_BOLD);

canvas.drawText(“你的最终成绩是”+grade + “分”, 180, 600, p);

}

}

public void run() {

while(true){

if(f == 0){

x ++;

y ++;

}

if(f == 1){

x –;

y ++;

}

if(f == 2){

x –;

y –;

}

if(f == 3){

x ++;

y–;

}

if(f == 4){

y ++;

}

if(x > 770){

if(f == 0){

f = 1;

}else

f = 2;

}

if(y > 1050){

if(f == 1){

f = 4;

}else

f = 4;

}

if(x < 30){

if(f == 2){

f = 3;

}else

f = 0;

}

if(y < 30){

if(f == 3){

f = 0;

}else

f = 1;

}

if(y+20 >= y1 && x1 < x && x1+300 > x+20){

grade += 10 ;

if(f == 1){

f = 2;

}else

f = 3;

}

try{

Thread.sleep(4);

}catch(Exception e){}

//repaint();

handler.sendEmptyMessage(123);

}

}

public boolean onTouch(View arg0, MotionEvent arg1) {

x1 = arg1.getX() - 150;

y1 = arg1.getY() - 50;

this.invalidate();

return true;

}

}

然后就是最近两天了(昨天和今天),主要做了两件事:

一是做了一个植物大战僵尸,虽然功能还不是十分的完善,但是基本功能还是不错的,源代码有点长,大概有300多行,我就不复制过来了,不过对于核心代码还是想分享一下:

class ZombiePanel extends JPanel implements Runnable,MouseListener,MouseMotionListener,KeyListener{

//僵尸的位置坐标

int[] x = new int[10];

int[] y = new int[10];

Zomb[] zombies = new Zomb[10];

int m = 0;

int n = 0;

int j = 1;

int h1 = -10;

int h2 = -10;

int z1 = 0;

int z2 = 0;

int ball1 = 170;

int ball2 = 360;

int plant1 = 100;

int plant2 = 350;

int[] plx = new int[100];

int[] ply = new int[100];

int q = 0;

int grade = 0;



int num = 1;



//创建一个容器装不定数量子弹

Vector vector =  new Vector();



public ZombiePanel(){                                  //初始化

for(int i=0 ; i < 10 ; i ++){

x[i] = 800 + (int)(Math.random()*300);

int k = (int)(Math.random()*5);

switch(k){

case 0:y[i] = 30;break;

case 1:y[i] = 120;break;

case 2:y[i] = 210;break;

case 3:y[i] = 300;break;

case 4:y[i] = 390;break;

}

int t = (int)(Math.random()*3);

switch(t){

case 0:zombies[i] = new ZombieNormal();break;

case 1:zombies[i] = new ZombieBucket();break;

case 2:zombies[i] = new ZombieConehead();break;

}

}

} 

public void paint(Graphics g) {

super.paint(g);

Image img = new ImageIcon(“pic/background1.jpg”).getImage();

g.drawImage(img, -220, 0,getWidth() + 570,getHeight(), null);

//画僵尸

for(int i = 0 ; i < 10 ; i ++){

g.drawImage(zombies[i].imgs[j], x[i], y[i], null);

}

Image plant = new ImageIcon(“pic/plant_peashoot_single/p_01_0”+j+”.png”).getImage();

g.drawImage(plant, plant1, plant2, null);

ball2 = plant2;

Image ball = new ImageIcon(“pic/bullet_01.png”).getImage();

g.drawImage(ball, ball1, ball2, null);

Image img3 = new ImageIcon(“pic/hammer.png”).getImage();

g.drawImage(img3, m, n,50,80, null);

Image img4 = new ImageIcon(“pic/hammer_down.png”).getImage();

g.drawImage(img4, h1, h2,50,80, null);

}

public void run() {

while(true){

for(int i = 0; i < 7 ; i ++){

x[i] -= 5;

}

x[7] -= 1;

x[8] -= 3;

x[9] -= 4;

ball1 += 20;

try{

Thread.sleep(80);

}catch(Exception e){}

j ++;

if(j >= 8){

j = 1;

}

num ++;

if(num >= 8){

num = 1;

}

repaint();

for(int i=0 ; i < 10 ; i ++){

if((z1 > x[i] && z1 < x[i]+60 && z2 > y[i] && z2 < y[i]+100)||x[i]<=-100 ){

x[i] = 800;

y[i] = 90*(int)(Math.random()*4) + 30;

}

if(ball1 > x[i] && ball1 < x[i]+60 && ball2 > y[i] && ball2 < y[i]+100 ){

x[i] = 800;

y[i] = 90*(int)(Math.random()*4) + 30;

ball1 = 170;

}

}

if(ball1 >= 800){

ball1 = 170;

}

}

}

​然后第二件事就是在安卓上怎么控制按键和设置简单的布局,这个比较复杂,下面这个是一个小例子,只提供MainActivity,其他的可以自行添加:

public class MainActivity extends Activity implements OnClickListener{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final EditText txtUser = (EditText)(this.findViewById(R.id.txtUser));

final EditText txtPass = (EditText)(this.findViewById(R.id.txtPass));

Button btnLogin = (Button)this.findViewById(R.id.btnLogin)

btnLogin.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

String user = txtUser.getText().toString();

String pass = txtPass.getText().toString();

if(user.equals(“aaa”)&&pass.equals(“111”)){

Toast.makeText(MainActivity.this, “欢迎”,1000).show();

}else{

Toast.makeText(MainActivity.this, “滚”,1000).show();

}

}

});

}

​明天就开始接触怎么获取短信信息,希望这个项目的学习能圆满结束。

展开阅读全文

没有更多推荐了,返回首页