2.09.2018

Android 2D Game Tutorial for Beginners (轉貼)

Android 2D Game Tutorial for Beginners


This document is based on:
  • Android Studio 1.5

Target of the document is help you to become acquainted with a few simple techniques in  programming Android Game 2D. Include:
  • Use SuffaceView
  • Drawing on a Canvas
  • The motion of the game character.
  • Interactions with the player's gestures
In this document, I will guide you step by step, therefore, you need to read and practice up to down. We will write each version of the game from 1 to the final version.

2- Create a Game Project

Note that you're creating a 2D game on Android, so the interface of the game must be drawn by you, so you do not need aactivity_main.xml file.
OK, your Project was created.

3- Preparing Images and sounds

You need a few images
  • chibi1.png


  • chibi2.png



  • explosion.png





The Audio file of the explosion.
Background sound:
Copy these images to the drawable folder of project. Create raw folder, and copy explosion.wav & background.mp3 to this folder.

4- Setting fullscreen (Version:1)

With games, you need to set the background image and an important thing is that you need to set FullScreen mode.
Your MainActivity class must extends from the Activity class .
MainActivity.java (Version: 1)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.o7planning.androidgame2d;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set fullscreen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Set No Title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}
Running apps:

1.11.2018

Pure virtual function in C++ (轉貼)


純虛擬函式、抽象類別(Abstract class)


C++預設函式成員都不是虛擬函式,如果要將某個函式成員宣告為虛擬函式,則要加上"virtual"關鍵字,然而C++提供一種語法定義「純虛擬函式」 (Pure virtual function),指明某個函式只是提供一個介面,要求繼承的子類別必須重新定義該函式,定義純虛擬函式除了使用關鍵字"virtual"之外,要在函 式定義之後緊跟著'='並加上一個0,例如:
class Some {
public:
    // 純虛擬函式
    virtual void someFunction() = 0;

    ....
};

一個類別中如果含有純虛擬函式,則該類別為一「抽象類別」(Abstract class),該類別只能被繼承,而不能用來直接生成實例,如果試圖使用一個抽象類別來生成實例,則會發生編譯錯誤。

Why do we need virtual functions in C++? (轉貼)

Why do we need virtual functions in C++?



1989

down voteaccepted
I'm a C++ newbie myself, but here is how I understood not just what virtual functions are, but why they're required:
Let's say you have these two classes:
class Animal
{
    public:
        void eat() { std::cout << "I'm eating generic food."; }
};

class Cat : public Animal
{
    public:
        void eat() { std::cout << "I'm eating a rat."; }
};
In your main function:
Animal *animal = new Animal;
Cat *cat = new Cat;

animal->eat(); // Outputs: "I'm eating generic food."
cat->eat();    // Outputs: "I'm eating a rat."
So far so good, right? Animals eat generic food, cats eat rats, all without virtual.
Let's change it a little now so that eat() is called via an intermediate function (a trivial function just for this example):
// This can go at the top of the main.cpp file
void func(Animal *xyz) { xyz->eat(); }
Now our main function is:
Animal *animal = new Animal;
Cat *cat = new Cat;

func(animal); // Outputs: "I'm eating generic food."
func(cat);    // Outputs: "I'm eating generic food."
Uh oh... we passed a Cat into func(), but it won't eat rats. Should you overload func() so it takes a Cat*? If you have to derive more animals from Animal they would all need their own func().
The solution is to make eat() from the Animal class a virtual function:
class Animal
{
    public:
        virtual void eat() { std::cout << "I'm eating generic food."; }
};

class Cat : public Animal
{
    public:
        void eat() { std::cout << "I'm eating a rat."; }
};
Main:
func(animal); // Outputs: "I'm eating generic food."
func(cat);    // Outputs: "I'm eating a rat."
Done.

7.08.2017

大量PNG圖去背轉GIF圖檔

1. 安裝 ImageMagick
(2.a 單轉檔, MJt1.png轉MJt1.gif)
>convert "MJt1.png" -transparent #ffffff "output\MJt1.gif"

2.b *.png去背(白色去背#ffffff)轉gif

>FOR %G IN ("*.png") DO convert "%G" -transparent #ffffffff "output\%G.gif"

==>會生成許多*.png.gif

3.用Windows PowerShell,將*.png.gif轉成*.gif
>Dir | Rename-Item -NewName { $_.name –eplace "png.gif", "gif" }

Note:在cmd下用ren *.png.gif *.gif無効…

11.05.2015

Python 3超新手常犯的錯誤

##注意parameter的值是variable時,不會跟著variable的值來改變

t = 0

def p(a=t):
    print("a=%d"%a)

def main():
    global t
    t = 100
    p()
    print("t=%d"%t)

if __name__ == "__main__":
    main()

##############Output#############
a=0
t=100

10.20.2015

Python3 "from m import *" 超新手常犯的錯誤

#在python 3, 請注意s的值並不會共用
# a.py
from b import *

def main():
    global s
    print(s)
    add()
    print(s)
    s += 6
    print(s)
    add()
    print(s)

if __name__ == "__main__":
    main()

# b.py
s =10

def add():
    global s
    s += 1
    print("s=%d"%s)

###
# Execute a.py
###
10
s=11
10
16
s=12
16

#如果要s的值共用,可以如下修改:
# a.py
import b

def main():
    print(b.s)
    b.add()
    print(b.s)
    b.s += 6
    print(b.s)
    b.add()
    print(b.s)

if __name__ == "__main__":
    main()

# b.py (不變)
s =10

def add():
    global s
    s += 1
    print("s=%d"%s)

###
# Execute a.py
###
10
s=11
11
17
s=18
18