java - 我应该如何将此游戏打包为apk?
问题描述
我在 python 中创建了一个游戏,并尝试将其打包为 apk。经过多次不成功的尝试,我尝试安装 android studio 并在 java 中重新创建它。作为第一次尝试在 java 中编写代码,我遇到了我不知道如何克服的错误
我尝试使用 kivy 和公文包将我的游戏打包为 apk,然后我只会更改输入。但是我放弃了这个解决方案,而是尝试在 java 中重新创建它,但没有取得多大成功。
import pygame
import random
from os import path
pygame.init()
win = pygame.display.set_mode((500, 500))
#image = pygame.image.load("grass.png").convert_alpha()
#image = pygame.transform.scale(image,(500,500))
#dim = image.get_rect()
#win.blit(image,(250-dim.center[0],250-dim.center[1]))
pygame.display.update()
pygame.display.set_caption("F1")
if not path.exists("highscore.txt"):
f = open("highscore.txt","w")
run = False
out = False
while out == False:
myFont = pygame.font.SysFont("Times New Roman", 18)
hsc = myFont.render("Highscore:", 1, [255,255,255])
f = open("highscore.txt", "r")
highsc = myFont.render(f.read(), 1, [255,255,255])
x = 238
y = 500
width = 30
height = 50
speed = 0
hs = 0
obstacles = []
while not run :
start = pygame.draw.rect(win,(255,125,125),[200,125,100,30])
getout = pygame.draw.rect(win,(255,125,125),[200,325,100,30])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 :
if start.collidepoint(event.pos):
run = True
elif getout.collidepoint(event.pos):
pygame.quit()
class incoming(object):
def __init__(self,a,b,width,height):
self.a = a
self.b = b + speed
self.width = width
self.height = height
def collision(self):
if x == self.a and self.b + height > 380:
if self.b < 380 + height:
return True
def draw(self,win):
pygame.draw.rect(win,(random.randrange(0,255),random.randrange(0,255),random.randrange(0,255)),(self.a,self.b,self.width,self.height))
#inc = pygame.image.load("inc.png").convert_alpha()
#inc = pygame.transform.scale(inc,(30,50))
#if self.a < 235:
# inc = pygame.transform.flip(inc,180,1)
#win.blit(inc,(self.a,self.b))
pygame.time.set_timer(pygame.USEREVENT+2, random.randrange(600,800))
while run:
win.fill((0,255,0))
road = pygame.draw.rect(win,[0,0,0],[165,y,140,-1000000])
cont = pygame.draw.rect(win,[255,255,255],[234,1000000,2,-1000000])
pl = pygame.draw.rect(win,[255,0,0],[x,380,width,height])
#player1 = pygame.image.load("enzo.png").convert_alpha()
#player1 = pygame.transform.scale(player1,(30,50))
#win.blit(player1,(x,380))
a = random.randrange(168,276,35)
while speed < 30:
speed = speed + 3
break
y=y+speed
b = 0
Score = int((y - 500)/100)
myFont = pygame.font.SysFont("Times New Roman", 18)
scris = myFont.render("Score:", 1, [255,255,255])
scr = myFont.render(str(int(Score)), 1, [255,255,255])
win.blit(scris, (30,40))
win.blit(scr, (30, 60))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.USEREVENT+2 and speed > 1.3 :
obstacles.append(incoming(a,b,width,height))
for obstacle in obstacles:
if obstacle.collision():
pygame.time.delay(1000)
win.set_alpha(0)
run = False
with open("highscore.txt", "r+") as hisc:
hi = hisc.read()
if not hi:
hi = '0'
if int(Score) > int(hi):
hisc.seek(0)
hisc.write(str(int(Score)))
if obstacle.a < 210:
obstacle.b = obstacle.b + speed + 1
else:
obstacle.b = obstacle.b + speed - 1
obstacle.draw(win)
win.blit(hsc, (380,40))
win.blit(highsc, (380, 60))
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 200 :
x=x-35
if keys[pygame.K_RIGHT] and x < 260:
x=x+35
pygame.quit()
这(上图)是游戏在 Python 中的运行方式。
这(下)是我的 JAVA 代码
package com.example.neonrace;
import android.graphics.Rect;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean run = false;
boolean out = false;
while (out == false){
final int x = 238;
final int[] y = {500};
final int width = 30;
final int height = 50;
final int[] speed = {0};
final List<Object> obstacles = new ArrayList<Object>();
class incoming{
int a;
int b;
int width;
int height;
public incoming(int a, int b, int width, int height){
this.a = a;
this.b = b;
this.width = width;
this.height = height;
}
public void collision;{
if (x == this.a) && (this.b + height) > 380; {
if (this.b < (380 + height));{
return true;
}
}
}
public static void draw() {
Canvas.drawRect(this.a, this.b, this.width, this.height, black);
}
};
new java.util.Timer().schedule(
new java.util.TimerTask(){
@Override
public void run(){
obstacles.add(incoming(int a, int b, int width, int height));
obstacles[].draw();
}
},700
);
Button Start = (Button) findViewById(R.id.Start);
Start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
boolean run = true;
while (run) {
Canvas.drawRect(165, 380, 240, 250, blue);
Canvas.drawRect(x,382, 30,50, red);
Random r = new Random();
int a = (int) r.nextInt(168, 276, 35);
while (speed[0] <30){
speed[0] = speed[0] + 3;
}
y[0] = y[0] + speed[0];
int size = ArrayList.size();
int i = 0;
for ( int i = 0, i < size, i++){
if obstacles[i].collision == true{
run = false
}
if obstacles[i].a < 210{
obstacles[i].b += speed+1
}
else {
obstacles[i].b += speed - 1
}
obstacles[i].draw()
}
public boolean onTouchEvent(MotionEvent event) {
int u = (int) event.getX();
if u < 200{
x = x - 35
}
else {
x = x + 35
}
}
}
}
}
});
}
}
}
这是我第一次用java编写代码,结果与预期的相差甚远。PS我知道我没有对退出按钮做任何事情。这应该是最后一件需要担心的事情。
解决方案
您不会将其放在任何接近 Android 应用程序所需格式的地方。你不能在 onCreate 中永远运行一个大循环——onCreate 需要相当快地返回。在此之前,您不会看到任何 UI。您正在调用 Canvas.drawRect- 这不起作用,drawRect 是在 Canvas 的实例上调用的,而不是在类上调用的。而且 Activity 没有绘制功能——它有视图,而这些视图可以绘制。活动没有。
老实说,这是你需要退后一步,学习 Android 和 Java 的水平,当你学得更多时再试一次。重写比打捞要快。
推荐阅读
- spring - pom 文件中的错误,没有消息或指示出了什么问题
- amazon-web-services - 为什么 Lambda 还在 S3 存储桶中使用旧版本的 zip 文件?
- visual-studio - 将我的项目发布到 Azure App-Aplication(Web 托管)后出现错误
- typescript - 如何使用打字稿扩展基本类型
- docker - 使用 docker-compose 将 Elasticsearch 请求/查询记录到 docker 实例的标准输出
- php - 通过 phpseclib 上传的文件减少到 14 个字节?
- cratedb - Cratedb 自定义排序
- workflow - 使用堆栈跟踪和消息终止工作流
- javascript - 无法重置只读输入文本
- reactjs - 使占位符加载响应