2009年12月26日 星期六

Qt使用OpenGL














paintGL()畫圖

initializeGL()初始化

resizeGL( int width, int height )



glBegin(GL_TRIANGLES);//开始绘制三角形

glColor3f( 1.0, 0.0, 0.0 );//上色,red

glVertex3f( 0.0f, 1.0f, 0.0f);//3角形第一點

//glTranslatef(x, y, z),x, y, z是相对与您当前所在点的位移

glColor3f( 0.0, 1.0, 0.0 );//上色,green

glVertex3f(-1.0f,-1.0f, 0.0f);//3角形第2點

glColor3f( 0.0, 0.0, 1.0 );//上色,blue

glVertex3f( 1.0f,-1.0f, 0.0f);//3角形第3點

glEnd();//END三角形绘制结束。



glBegin( GL_QUADS ); //开始绘制4邊行

glColor3f( 0.0, 1.0, 0.0 );

glVertex3f( 1.0, 1.0, -1.0 );

glVertex3f( -1.0, 1.0, -1.0 );

glVertex3f( -1.0, 1.0, 1.0 );

glVertex3f( 1.0, 1.0, 1.0 );

glEnd();


read more...

2009年12月25日 星期五

QT撰寫的linplayer v0.050多媒體播放器釋出


















增加播放VCD CD 圖片,全螢幕...............
OS是64位元ubuntu910,其他相近LINUX OS 64位元版本亦適用.
OS是32位元ubuntu904,其他相近LINUX OS 32位元版本亦適用.

位置:
http://sites.google.com/site/yplinlinux/


read more...

2009年12月23日 星期三

ubuntuone
















ubuntu one申請就有2G的網路硬碟,這應該還不錯

read more...

2009年12月18日 星期五

先裝上ubuntu10.04來玩

















想說先裝來測試看看,因為想測自己的QT程式,這次就先裝gnome,感覺還滿順暢,我只先裝到記憶卡來玩.


read more...

2009年12月6日 星期日

QT撰寫的linplayer v0.016 多媒體播放器發佈.

































可播放網路電視,網路廣播,本地音樂,本地影片
版本v0.016 ,支援多種格式, 目前大部分基本功能都具備了,下一版希望能增加更多功能.
下載位置:
http://sites.google.com/site/yplinlinux/

read more...

2009年12月3日 星期四

多媒體播放器計畫


用Qt撰寫的多媒體播放器計畫發布.
http://sites.google.com/site/yplinlinux/home



read more...

2009年11月22日 星期日

用Qt做個播放器

使用Phonon來播放影片,首先要安裝Phonon套件

media = new Phonon::MediaObject;
media->setCurrentSource(Phonon::MediaSource("./test.mpeg"));

參考:
http://support.oss.org.tw/wiki/index.php/Qt_Phonon


read more...

2009年11月8日 星期日

kubuntu9.10安裝多媒體中心xbmc
















1.添加源
sudo vi /etc/apt/sources.list
#other
deb http://ppa.launchpad.net/team-xbmc/karmic-ppa/ubuntu/ karmic main
deb-src http://ppa.launchpad.net/team-xbmc/karmic-ppa/ubuntu/ karmic main
2.更新套件庫
sudo apt-get update
出現找不
到公鑰,將找不到,添入XXXXXXX
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com XXXXXXXX

3.安裝多媒體中心xbmc
sudo apt-get install xbmc

4.中文設置
複製字型
sudo cp /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc /usr/share/xbmc/media/Fonts/arial.ttc
sudo cp /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc /usr/share/xbmc/media/Fonts/arial.ttf
取代一些字(把~/.xbmc/userdata/guisettings.xml檔案,修改font)
sed -e "s/language>english<\/language/language>Chinese (Simple)<\/language/" -i ~/.xbmc/userdata/guisettings.xml sed -e "s/font>Default<\/font/font>Arial<\/font/" -i ~/.xbmc/userdata/guisettings.xml 5.打開xbmc,language選項改成中文(請再確定4,~/.xbmc/userdata/guisettings.xml) 參考 http://forum.ubuntu.org.cn/viewtopic.php?f=74&t=184521

read more...

kubuntu9.10桌面秀









read more...

2009年11月2日 星期一

ubuntu遠端登入

1.把遠端電腦(server),系統-管理-登入畫面-本地端改成遠端,這樣就讓client端能登入
2.sudo apt-get install tsclient
sudo apt-get install xnest
這樣客戶端,就能用
tsclient來登入server端,安裝xnest是註冊XDMCP而已
3.啟動
tsclient,終端機下
tsclient
或KDE選單-應用程式-網際網路-Termial Server Client


tsclient輸入

computer:請輸入遠端機器名稱(hostname查看),或ip adress(ifconfig查看)
protocl:請選XDMCP(記得要安裝xnest套件才會出現這個選項)
user name:遠端使用者
password:遠端使用者密碼





















read more...

2009年10月31日 星期六

error while loading shared libraries: libstdc++.so.5 ubuntu 910

安裝了kubuntu 910 amd64跑一些之前軟一錯誤
error while loading shared libraries: libstdc++.so.5
先下載
libstdc++5_3.3.6-17ubuntu1_i386.deb強制安裝,copy /usr/lib/的libstdc++.so.5.0.7到/usr/lib32,在/usr/lib32的libstdc++.so.5.0.7建立一個軟連結到libstdc++.so.5

sudo dpkg -i --force-all libstdc++5_3.3.6-17ubuntu1_i386.deb
sudo cp /usr/lib/libstdc++.so.5.0.7 /usr/lib32
cd /usr/lib32
ln -s libstdc++.so.5.0.7 libstdc++.so.5

原文出處:
http://blog.dalejefferson.com/2009/09/gwt-on-64bit-ubuntu-karmic-910.html

read more...

2009年10月28日 星期三

kubuntu910


ubuntu910再幾天就會正式推出,就先安裝rc版玩玩,kubuntu910的畫面真是太漂亮了,說真的玩這麼久Linux,是我看過完成度最高的一次,進步太大了.

read more...

2009年9月6日 星期日

ubuntu9.04再一個輕量級桌面WindowMaker
















WindowMaker套件庫就有,這個滿漂亮的

1.字型依樣畫葫蘆(我寫了bash shell來一次替換 /usr/share/WindowMaker/Styles/下,所有style的字型)下載

wm.sh(sudo來執行)

-------------------------------------------------------------------------------------

#!/bin/sh
for file in `ls /usr/share/WindowMaker/Styles/*.style`
do
if [ -f "$file" ]
then
sed "/Font/s/\".*\"/\"Trebuchet MS,AR PL UKai TW MBE-1:bold:pixelsize=15\"/g" $file > $file.test
rm $file
cp $file.test $file
rm $file.test
#echo "$file"
#cat add.txt>> "$file"
fi
done

2.背景
把圖片放到
~/GNUstep/Library/WindowMaker/Backgrounds

滑鼠右鍵,工作區,外觀,backgrounds,




3.
sudo vi /etc/X11/WindowMaker/menu.hook

"Nautilus" SHEXEC /usr/bin/nautilus改成

"Nautilus" SHEXEC /usr/bin/nautilus --no-desktop

避免開啟檔案瀏覽器,跳到gnome桌面

4.登入執行指令(shell)
vi ~/GNUstep/Library/WindowMake/autostart 加入

#
# Place applications to be executed when WindowMaker is started here.
# This should only be used for non-X applications or applications that
# do not support session management. Other applications should be restarted
# by the WindowMaker session restoring mechanism. For that, you should
# either set SaveSessionOnExit=YES or select "Save Session" in the Workspace
# submenu of the root menu when all applications you want started are
# running.
#
# WindowMaker will wait until this script finishes, so if you run any
# commands that take long to execute (like a xterm), put a ``&'' in the
# end of the command line.
#
# This file must be executable.
#
wmcalclock &
wmmemload &
wmmixer -w &
wmsysmon &
wmcpuload &
wmxmms2 &
nm-applet &
xmms2d &





上面小程式套件庫都有





read more...

2009年9月5日 星期六

ubuntu9.04再一個輕量級桌面icewm



















icewm套件庫中就有,linux還有好幾個桌面有空再來完看看,比較低階電腦用這些桌面很適合(大約使用100M的RAM,上上網,聽音樂,看一般電影,超好用的)

1.中文字型,依樣畫葫蘆(根據前面找到字型),這次我寫了一個小bash shell來替換,一次把主題全改成中文

icewm.sh(用sudo來執行)下載
=============================================================================
#!/bin/sh
for directory in `ls /usr/share/icewm/themes/ `
do
if [ -d "/usr/share/icewm/themes/$directory" ]
then
for file in `ls /usr/share/icewm/themes/$directory/*.theme`
do
if [ -f "$file" ]
then
#echo "$file"
cat add.txt>> "$file"
fi
done
fi
done

add.txt檔放字型下載
==============================================================
#FONTS#########################################################

TitleFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
TitleFontNameXft="AR PL UKai TW MBE:size=15:medium"

MenuFontName="-*-sans-bold-r-normal-*-*-110-*-*-p-*--*-*"
MenuFontNameXft="AR PL UKai TW MBE:size=15:medium"

NormalTaskBarFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-100-*-*-p-*--*-*"
NormalTaskBarFontNameXft="AR PL UKai TW MBE:size=10:medium"

ActiveTaskBarFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-100-*-*-p-*--*-*"
ActiveTaskBarFontNameXft="AR PL UKai TW MBE:size=10:bold"

NormalButtonFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-100-*-*-p-*--*-*"
NormalButtonFontNameXft="AR PL UKai TW MBE:size=10:medium"

ActiveButtonFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-100-*-*-p-*--*-*"
ActiveButtonFontNameXft="AR PL UKai TW MBE:size=10:bold"

LabelFontName="-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
LabelFontNameXft="AR PL UKai TW MBE:size=12:bold"

ToolTipFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-100-*-*-p-*--*-*"
ToolTipFontNameXft="sans:size=10:medium"

ClockFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-100-*-*-p-*--*-*"
ClockFontNameXft="AR PL UKai TW MBE:size=10:medium"

ApmFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-120-*-*-p-*--*-*"
ApmFontNameXft="AR PL UKai TW MBE:size=12:medium"

InputFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-120-*-*-p-*--*-*"
InputFontNameXft="AR PL UKai TW MBE:size=12:medium"

moveSizeFontName="-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
moveSizeFontNameXft="AR PL UKai TW MBE:size=12:bold"

QuickSwitchFontName="-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
QuickSwitchFontNameXft="AR PL UKai TW MBE:size=12:bold"

ListBoxFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-120-*-*-p-*--*-*"
ListBoxFontNameXft="AR PL UKai TW MBE:size=11:medium"

NormalWorkspaceFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
NormalWorkspaceFontNameXft="AR PL UKai TW MBE:size=12:bold"

ActiveWorkspaceFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
ActiveWorkspaceFontNameXft="AR PL UKai TW MBE:size=12:bold"

StatusFontName="-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
StatusFontNameXft="AR PL UKai TW MBE:size=12:bold"

ToolButtonFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
ToolButtonFontNameXft="AR PL UKai TW MBE:size=12:bold"

FxFontName="-*-AR PL UKai TW MBE-bold-r-normal-*-*-120-*-*-p-*--*-*"
FxFontNameXft="AR PL UKai TW MBE:size=12:bold"

MinimizedWindowFontName="-*-AR PL UKai TW MBE-medium-r-normal-*-*-120-*-*-p-*--*-*"
MinimizedWindowFontNameXft="AR PL UKai TW MBE:size=12:medium"

2.再工具列放幾個執行圖

vi ~/.icewm/toolbar加入


# This is an example for IceWM's toolbar definition file.
#
# Place your variants in /etc/X11/icewm or in $HOME/.icewm
# since modifications to this file will be discarded when you
# (re)install icewm.
#
prog XTerm xterm x-terminal-emulator
#prog FTE fte fte
#prog Netscape netscape netscape
prog "Mozilla Firefox" /usr/share/pixmaps/firefox-3.0.png firefox
prog "nm-applet" /usr/share/pixmaps/gnome-nettool.xpm nm-applet
prog "gnome-screenshot" /usr/share/pixmaps/gnome-qeye.png gnome-screenshot
prog "Home Folder" /usr/share/icons/gnome/16x16/places/user-home.png /usr/bin/nautilus --no-desktop


3.登入執行shell(or指令)

vi ~/.icewm/startup加入

gkrellm -w &

存檔後
chmod u+x ~/.icewm/startup

4.讓背景放大整個桌面
sudo vi /usr/share/icewm/preferences

DesktopBackgroundScaled =1
read more...

2009年9月3日 星期四

ubuntu9.04輕量級桌面e16(支援3D特效)















linux有很多桌面管理程式,再介紹一個e16,套件中就有e16(用synaptic安裝)




1.中文設定,根據上篇字型設定

vi /usr/share/e16/config/fonts.cfg.xft修改
/* Xft font example */
#include
__E_CFG_VERSION 1
__FONTS __BGN

font-default "xft:AR PL UKai TW MBE-15:bold"

font-border "xft:AR PL UKai TW MBE-15:bold"
font-border-italic "xft:AR PL UKai TW MBE-15:italic"
font-border-small "xft:AR PL UKai TW MBE-15"

font-coords "xft:AR PL UKai TW MBE-15:bold"

font-dialog "xft:AR PL UKai TW MBE-15"
font-dialog-hilite "xft:AR PL UKai TW MBE-15:bold"
font-dialog-italic "xft:AR PL UKai TW MBE-15:italic"

font-epplet "xft:AR PL UKai TW MBE-15"
font-epplet-small "xft:AR PL UKai TW MBE-15"
font-epplet-medium "xft:AR PL UKai TW MBE-15"
font-epplet-large "xft:AR PL UKai TW MBE-15"




font-focus "xft:AR PL UKai TW MBE-15:bold"

font-iconbox "xft:AR PL UKai TW MBE-15:bold"

font-init "xft:AR PL UKai TW MBE-15:bold"

font-menu "xft:AR PL UKai TW MBE-15"

font-pager "xft:AR PL UKai TW MBE-15:bold"

font-tooltip "xft:AR PL UKai TW MBE-15:bold"

__END

2.啟動e16執行shell
vi /usr/share/e16/scripts/starte16加入
nm-applet & #啟動nm-applet
gkrellm -w & #啟動gkrellm

3.背景
將圖片放到
~/.e16/backgrounds
再桌面,滑鼠右鍵,設背景


4.其他設定

再桌面,滑鼠右鍵,enlightenment seting,areas,off關掉邊界功能
再桌面,滑鼠右鍵,enlightenment seting,transparency,glass透明度,滿炫的

又是一個Linux超漂亮桌面,好用又省資源


read more...

2009年9月1日 星期二

ubuntu9.04輕量級桌面fluxbox













1.
sudo apt-get install fluxbox #安裝fluxbox桌面
sudo apt-get install feh # 安装背景的工具

vi ~/.fluxbox/startup加入下面指令,每次登入產生不同桌布背景,目錄只能放圖檔


fbsetbg -F -R /home/yplin66/圖片/ &


2.修正中文字型,fluxbox本身就能顯示中文,只是字型不對
[XXXXXXX]$ fc-list找使用字型

....................
AR PL UKai TW MBE:style=Book
文鼎PL简报宋,AR PL SungtiL GB:style=Regular
文泉驛正黑,WenQuanYi Zen Hei,文泉驿正黑:style=中等,Medium
Nimbus Mono L:style=Regular
FreeSans:style=Oblique,ležeče
Umpush:style=Book
Umpush:style=Bold
Norasi:style=Regular
Bitstream Charter:style=Italic
Petra:style=Regular
.........................
找到

AR PL UKai TW MBE
字型




vi ~/.fluxbox/overlay加入,會蓋掉styles裡的font


menu.frame.font: AR PL UKai TW MBE-15
menu.title.font: AR PL UKai TW MBE-15
window.font: AR PL UKai TW MBE-15
*font: AR PL UKai TW MBE-15




3.

/usr/share/fluxbox/styles有關font移除,上面雖然會蓋掉,最好拿掉


4.修正一下,檔案開啟menu,會跑到gnome桌面
vi /etc/X11/fluxbox/fluxbox-menu,多加參數--no-desktop

[exec] (Nautilus) {/usr/bin/nautilus --no-desktop}

read more...

2009年8月29日 星期六

ubuntu9.04美化GRUB2開機畫面

用synaptic安裝grub2,再把grub解除安裝,重開機可能會grub開機錯誤,拿出live cd重建grub後,grub2就會正常了,參考來源:http://www.xxlinux.com/linux/article/accidence/install/20081231/14759.html

grub2支援背景圖片,中文開機選單,如下:


sudo chmod u+w
/boot/grub/grub.cfg
sudo vi
/boot/grub/grub.cfg
改完
sudo chmod u-w /boot/grub/grub.cfg

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by /usr/sbin/update-grub using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set default=0
set timeout=5
set root=(hd0,4)
search --fs-uuid --set 391ed808-6c1e-4617-9a4e-7efbdb0eb3de
if font /usr/share/grub/unicode.pff ; then
set gfxmode=640x480
insmod gfxterm
insmod vbe

insmod jpeg

terminal gfxterm
background_image /boot/grub/xxxx.jpeg
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/05_debian_theme ###
##set menu_color_normal=cyan/blue
##set menu_color_highlight=white/blue
### END /etc/grub.d/05_debian_theme ###

### BEGIN /etc/grub.d/10_hurd ###
### END /etc/grub.d/10_hurd ###

### BEGIN /etc/grub.d/10_linux ###
set root=(hd0,4)
search --fs-uuid --set 391ed808-6c1e-4617-9a4e-7efbdb0eb3de
....................

read more...

追蹤子彈

原理很簡單
把敵人座標一直傳給子彈class做計算
寫這個遊戲會一段滿長得時間(有空閒在寫),我想寫出來一個RPG的射擊遊戲,能讓機體升級,購買武器,購買機體,可能的話加上劇情
下面是測試畫面(把附機體子彈一直發射,觀看路徑)

read more...

2009年8月21日 星期五

在QT設計多載建構子和介面

A.

要處理不同body(機體),想了好久,才想出要用介面來當成溝通不同機體的橋樑,滿妙的窩,當按一下按鈕就能更換body和同時更換子彈,bullet1是繼承bullet而來,body1是繼承了body而來,並改寫部份程序.
只要我想在增加body2,bullet2,
繼承body,bullet,改寫部份程序,又是另一個機體和武器,並在介面加一些溝通body程式既可
介面是放在bodylist class

下面是呼叫不同body程序,
new bodylist(new body(500,10,4))是產生一個新機體
void Frame::change_body(int i)
{
switch (i)
{
case 0 : this->thisbody=new bodylist(new body(500,10,4));thread->set_bodylist(thisbody);break ;
case 1 : this->thisbody=new bodylist(new body1(500,10,4));thread->set_bodylist(thisbody);break ;
}
}
目前換機體只換子彈大小,後續會改寫子彈路徑,威力和機體形狀

B.多載建構子宣告,注意
body* b和body1* b是不同body,來達成多載

bodylist(body* b,QWidget *parent=0); //多載,利用多載來設定不同機體,注意*parent=0和*parent不同
bodylist(body1* b1,QWidget *parent=0);//多載,利用多載來設定不同機體

C.介面

////增加body,此處介面要記得加上去

#include "bodylist.h"

bodylist::bodylist(body* b ,QWidget *parent)
{
body_0=b; //body初始化
body_id=0; //id
}
bodylist::bodylist(body1* b1 ,QWidget *parent)
{
body_1=b1; //body初始化
body_id=1; //id
}

void bodylist::set_x(int i)
{
switch(body_id)
{
case 0:this->body_0->set_x(i);break;
case 1:this->body_1->set_x(i);break;
}

}
void bodylist::set_y(int i)
{
switch(body_id)
{
case 0:this->body_0->set_y(i);break;
case 1:this->body_1->set_y(i);break;
}

}
.................

body ,bullet 和bodylist介面程式碼

bullet.h
-------------------------------------------------------------------------------
#ifndef BULLET_H
#define BULLET_H

#include <QWidget>

class bullet : public QWidget
{
Q_OBJECT
public:
bullet(int v,int x,int y,int m,QWidget *parent = 0);//建構子,目的初始化物件

//private:
public:
int b_m;
int xx;
int yy;
int bullet_v;

public:
void set_bullet_x(int x1);
void set_bullet_y(int y1);
int get_bullet_x();
int get_bullet_y();
QRectF path();
};

#endif // BULLET_H

bullet.cpp
----------------------------------------------------------------------------------
//用這個class來放bullet的x,y座標
#include "bullet.h"

bullet::bullet(int v,int x,int y,int m,QWidget *parent)
: QWidget(parent)
{
bullet_v=v; //這設bulle速度,不能太快(不要超過目標y軸),擊中目標判斷會錯
xx=x; //初始化子彈x
yy=y; //初始化子彈y
b_m=m; //斜率

}
//設x
void bullet::set_bullet_x(int x1)//一次只能一個
{
this->xx=x1;
}
//設y
void bullet::set_bullet_y(int y1)
{
this->yy=y1;
}
//取得x
int bullet::get_bullet_x()
{
return this->xx;
}
//取得y
int bullet::get_bullet_y()
{
return this->yy;
}
QRectF bullet::path()//enemy路徑運算
{

if(b_m!=0&&b_m!=999&&b_m!=-999)
{
yy=-bullet_v+yy;
xx=-bullet_v/b_m+xx;
}
if(b_m==-999) //這來代表斜率-0
{
yy=yy;
xx=xx-bullet_v;
}
if(b_m==999) //這來代表斜率+0
{
yy=yy;
xx=xx+bullet_v;
}
QRectF r(xx, yy,5.0, 10.0);

return r;//橢圓

}

bullet1.h
---------------------------------------------------------------------------------
#ifndef BULLET1_H
#define BULLET1_H
#include "bullet.h"
#include <QObject>

class bullet1 :public bullet //繼承bullet
{
Q_OBJECT

public:

bullet1(int v,int x,int y,int m );
virtual QRectF path(); //覆寫

};

#endif // BULLET1_H

bullet1.cpp
--------------------------------------------------------------------------------
#include "bullet1.h"
#include "bullet.h"
bullet1::bullet1(int v,int x,int y,int m):bullet(v,x,y,m)//用call 叫bullet建構子
{


}


QRectF bullet1::path()//enemy路徑運算
{

if(b_m!=0&&b_m!=999&&b_m!=-999)
{
yy=-bullet_v+yy;
xx=-bullet_v/b_m+xx;
}
if(b_m==-999) //這來代表斜率-0
{
yy=yy;
xx=xx-bullet_v;
}
if(b_m==999) //這來代表斜率+0
{
yy=yy;
xx=xx+bullet_v;
}
QRectF r(xx, yy,10.0, 10.0);

return r;//橢圓

}

body.h
-------------------------------------------------------------------------
#ifndef BODY_H
#define BODY_H
#include <QWidget>
#include <bullet.h>

class body :public QWidget
{
Q_OBJECT
public:
body(int life ,int v,int no,QWidget *parent = 0);


QPainterPath body_paint1(int move_x,int move_y);

void set_life(int i);
int get_life();
void get_body_fly(int i);
void add_bullet(int i);
void set_x(int i);
void set_y(int i);
QVector<bullet*> *get_bullet_list();
void remove_bullet(int i);
//private:
public:


int body_life;
int xx;
int yy;
int bullet_number;
int x_int;
int y_int;
int bv;
int body_fly;
private:
QVector<bullet*> *bullet_list_Painter;
};

#endif // BODY_H

body.cpp
----------------------------------------------------------------------------------
//一種機體放不同武器,更換機體子彈就會跟著變換武器,要產生機體只要繼承body,改寫方法既可,好棒的想法
#include "body.h"

body::body(int life,int v,int no,QWidget *parent)//機體生命,子彈速度,子彈數


{
//可變初始化,譬如機體升級,life加大,發射數變大,加快
body_life=life; //機體生命
body_fly=0; //bullet1* p1=new bullet1(bv,x_int-12,y_int,999999);
bullet_number=no;//子彈數
bv=v; //子彈速度
bullet_list_Painter=new QVector<bullet*>;

}




QPainterPath body::body_paint1(int move_x,int move_y)
{
xx=move_x;
yy=move_y;

QPainterPath* body;
body=new QPainterPath;
body->moveTo(move_x-2,move_y);

body->lineTo(move_x,move_y-5);
body->lineTo(move_x+2,move_y);
body->lineTo(move_x+2,move_y+5);
body->lineTo(move_x+12,move_y+5);
body->lineTo(move_x+12,move_y+10);
body->lineTo(move_x-12,move_y+10);
body->lineTo(move_x-12,move_y+5);
body->lineTo(move_x-2,move_y+5);

body->lineTo(move_x-2,move_y);



if(body_fly==1)
{
QRectF r0(move_x-14, move_y+2, 2, 10 );
QRectF r1(move_x+12, move_y+2, 2, 10 );
body->addRect(r0);
body->addRect(r1);
}

if(body_fly==2)
{
QRectF r0(move_x-14, move_y+2, 2, 10 );
QRectF r1(move_x+12, move_y+2, 2, 10 );
QRectF r2(move_x-1, move_y+2, 2, 10 );
body->addRect(r0);
body->addRect(r1);
body->addRect(r2);

}

if(body_fly==3)
{
QRectF r0(move_x-24, move_y+2, 10, 10 );
QRectF r1(move_x+12, move_y+2, 10, 10 );
body->addEllipse(r0);;
body->addEllipse(r1);;

}


return *body;
}
void body::get_body_fly(int i)
{
body_fly=i;
}







void body::set_life(int i)
{
this->body_life=i;
}
int body::get_life()
{
return this->body_life;
}
///////////////////////////////
void body::set_x(int i)
{
this->x_int=i;
}
void body::set_y(int i)
{
this->y_int=i;
}

void body::add_bullet(int i)
{
if(i==0)//單一
{
if(bullet_list_Painter->size()<bullet_number)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int,y_int,999999);//速度10斜率無限大的子彈


bullet_list_Painter->append(p);
}

}
if(i==1)//2列
{
if(bullet_list_Painter->size()<bullet_number*2)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int-12,y_int,999999);//速度10斜率無限大的子彈
bullet_list_Painter->append(p);
bullet* p1=new bullet(bv,x_int+12,y_int,999999);//速度10斜率無限大的子彈
bullet_list_Painter->append(p1);
}

}
if(i==2)//3列
{
if(bullet_list_Painter->size()<bullet_number*3)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int,y_int,-2); //速度10,斜率-2的子彈
bullet_list_Painter->append(p);
bullet* p1=new bullet(bv,x_int,y_int,+2); //速度10,斜率2的子彈
bullet_list_Painter->append(p1);
bullet* p2=new bullet(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet_list_Painter->append(p2);

}

}
if(i==3)//5列
{
if(bullet_list_Painter->size()<bullet_number*5)//最多bullet_number顆子彈
{
bullet* p1=new bullet(bv,x_int,y_int,-999); //速度10,斜率-2的子彈
bullet* p2=new bullet(bv,x_int,y_int,-2);
bullet* p3=new bullet(bv,x_int,y_int,+999); //速度10,斜率2的子彈
bullet* p4=new bullet(bv,x_int,y_int,+2);
bullet* p5=new bullet(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet_list_Painter->append(p1);
bullet_list_Painter->append(p2);
bullet_list_Painter->append(p3);
bullet_list_Painter->append(p4);
bullet_list_Painter->append(p5);
}
}

}
void body::remove_bullet(int i)
{
if(!bullet_list_Painter->isEmpty())
{
this->bullet_list_Painter->remove(i);
}
}


QVector<bullet*> *body::get_bullet_list()
{
return bullet_list_Painter;
}

body1.h
-----------------------------------------------------------------------------------
#ifndef BODY1_H
#define BODY1_H
#include "body.h"
#include <bullet1.h>
class body1:public body
{
public:
body1(int life ,int v,int no);

virtual void add_bullet(int i); //覆寫
virtual void remove_bullet(int i); //覆寫
virtual QVector<bullet1*> *get_bullet_list();//覆寫

private:
QVector<bullet1*> *bullet1_list_Painter;
};

#endif // BODY1_H

body1.cpp
-----------------------------------------------------------------------------
#include "body1.h"
#include <iostream>


using namespace std;

body1::body1(int life ,int v,int no):body(life ,v,no)
{
bullet1_list_Painter=new QVector<bullet1*>;

}
void body1::add_bullet(int i)
{
if(i==0)//單一
{

if(bullet1_list_Painter->size()<bullet_number)//最多bullet_number顆子彈
{
bullet1* p=new bullet1(bv,x_int,y_int,999999);//速度10斜率無限大的子彈
bullet1_list_Painter->append(p);
}

}
if(i==1)//2列
{
if(bullet1_list_Painter->size()<bullet_number*2)//最多bullet_number顆子彈
{
bullet1* p=new bullet1(bv,x_int-12,y_int,999999);//速度10斜率無限大的子彈
bullet1_list_Painter->append(p);
bullet1* p1=new bullet1(bv,x_int+12,y_int,999999);//速度10斜率無限大的子彈
bullet1_list_Painter->append(p1);
}

}
if(i==2)//3列
{
if(bullet1_list_Painter->size()<bullet_number*3)//最多bullet_number顆子彈
{
bullet1* p=new bullet1(bv,x_int,y_int,-2); //速度10,斜率-2的子彈
bullet1_list_Painter->append(p);
bullet1* p1=new bullet1(bv,x_int,y_int,+2); //速度10,斜率2的子彈
bullet1_list_Painter->append(p1);
bullet1* p2=new bullet1(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet1_list_Painter->append(p2);

}

}
if(i==3)//5列
{
if(bullet1_list_Painter->size()<bullet_number*5)//最多bullet_number顆子彈
{
bullet1* p1=new bullet1(bv,x_int,y_int,-999); //速度10,斜率-2的子彈
bullet1* p2=new bullet1(bv,x_int,y_int,-2);
bullet1* p3=new bullet1(bv,x_int,y_int,+999); //速度10,斜率2的子彈
bullet1* p4=new bullet1(bv,x_int,y_int,+2);
bullet1* p5=new bullet1(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet1_list_Painter->append(p1);
bullet1_list_Painter->append(p2);
bullet1_list_Painter->append(p3);
bullet1_list_Painter->append(p4);
bullet1_list_Painter->append(p5);
}
}

}
void body1::remove_bullet(int i)
{
if(!bullet1_list_Painter->isEmpty())
{
this->bullet1_list_Painter->remove(i);
}
}


QVector<bullet1*> *body1::get_bullet_list()
{

return bullet1_list_Painter;

}

bodylist.h
-----------------------------------------------------------------
//////////body介面,用這個來放不同body//////////////////////////////////////////////////////////////

#ifndef BODYLIST_H
#define BODYLIST_H

#include <QWidget>
#include <body.h>
#include <body1.h>

class bodylist:public QWidget
{

public:
body* body_0;
body1* body_1;
bodylist(body* b,QWidget *parent=0); //多載,利用多載來設定不同機體,注意*parent=0和*parent不同
bodylist(body1* b1,QWidget *parent=0);//多載,利用多載來設定不同機體
int get_body_life();
void set_x(int i);
void set_y(int y);
void add_bullet(int i);
void get_body_fly(int i);
QVector<QRectF> *bullet_path();
void remove_bullet(int i);
QPainterPath body_paint(int move_x,int move_y);
int get_bullet_number();
private:
int body_id; //識別id

};

#endif // BODYLIST_H

bodylist.cpp
--------------------------------------------------------------------------------
////增加body,此處介面要記得加上去

#include "bodylist.h"

bodylist::bodylist(body* b ,QWidget *parent)
{
body_0=b; //body初始化
body_id=0; //id
}
bodylist::bodylist(body1* b1 ,QWidget *parent)
{
body_1=b1; //body初始化
body_id=1; //id
}

void bodylist::set_x(int i)
{
switch(body_id)
{
case 0:this->body_0->set_x(i);break;
case 1:this->body_1->set_x(i);break;
}

}
void bodylist::set_y(int i)
{
switch(body_id)
{
case 0:this->body_0->set_y(i);break;
case 1:this->body_1->set_y(i);break;
}

}

//取得body life
int bodylist::get_body_life() //取得body life
{
int life;

switch(body_id)
{
case 0:life=this->body_0->get_life();break;
case 1:life=this->body_1->get_life();break;
}
return life;
}
//獲得機體翅膀
void bodylist::get_body_fly(int i)
{
switch(body_id)
{
case 0:this->body_0->get_body_fly(i);break;
case 1:this->body_1->get_body_fly(i);break;
}
}
//加子彈
void bodylist::add_bullet(int i)
{
switch(body_id)
{
case 0:this->body_0->add_bullet(i);break;
case 1:this->body_1->add_bullet(i);break;
}
}
//子彈數
int bodylist::get_bullet_number()
{
int size;
switch(body_id)
{
case 0:size=this->body_0->get_bullet_list()->size();break;
case 1:size=this->body_1->get_bullet_list()->size();break;
}
return size;
}
//移走子彈
void bodylist::remove_bullet(int i)
{
switch(body_id)
{

case 0:
{
this->body_0->remove_bullet(i);
;break;
}

case 1:
{
this->body_1->remove_bullet(i);
;break;
}
}
}
//取得子彈path list,是QRectF list
QVector<QRectF> *bodylist::bullet_path()
{
QVector<QRectF> *list;
list=new QVector<QRectF>;
switch(body_id)
{

case 0:
{
for(int i=0;i<this->body_0->get_bullet_list()->size();i++)
{
list->append(body_0->get_bullet_list()->at(i)->path());
}
;break;
}

case 1:
{
for(int i=0;i<this->body_1->get_bullet_list()->size();i++)
{
list->append(body_1->get_bullet_list()->at(i)->path());
}
;break;
}
}
return list; //回傳子彈的QRectF list
}
QPainterPath bodylist::body_paint(int move_x,int move_y)
{

QPainterPath b;
//b=new QPainterPath;
switch(body_id)
{

case 0:
{
b= this->body_0->body_paint1(move_x,move_y);
;break;
}

case 1:
{
b= this->body_1->body_paint1(move_x,move_y);
;break;
}
}
return b;

}








read more...

2009年8月17日 星期一

Qt的繼承

Qt的繼承,觀念和java是一樣的.
之前機體發射子彈,子彈class是由thread來加,這樣變換機體,還要考慮變換子彈,寫起來程式將會變的很複雜.
我發現應該
子彈class應該是由body class來加,這樣我只要一變換body,子彈會隨著body變換,好處thread class幾乎程式碼不動,我要在寫另一種子彈只要繼承原來子彈class,覆寫裡面程序,就可形成另一個子彈class(body class亦是)

下面bullet1.cpp是繼承bullet.cpp而來,我還沒
覆寫裡面程序,所以bullet1和bullet是一樣的class,注意bullet1幾乎沒程式碼窩

body.h
=============================================================
#ifndef BODY_H
#define BODY_H
#include <QWidget>
#include <bullet.h>
#include <bullet1.h>
class body :public QWidget
{
Q_OBJECT
public:
body(int life ,int v,int no,QWidget *parent = 0);


QPainterPath body_paint1(int move_x,int move_y);

void set_life(int i);
int get_life();
void get_body_fly(int i);
void add_bullet(int i);
void set_x(int i);
void set_y(int i);
QVector<bullet*> *get_bullet_list();
void remove_bullet(int i);
private:
QVector<bullet*> *bullet_list_Painter;
int body_life;
int xx;
int yy;
int bullet_number;
int x_int;
int y_int;
int bv;
int body_fly;


};

#endif // BODY_H

body.cpp
=====================================================================
//一種機體放不同武器,更換機體子彈就會跟著變換武器,要產生機體只要繼承body,改寫方法既可,好棒的想法
#include "body.h"

body::body(int life,int v,int no,QWidget *parent)//機體生命,子彈速度,子彈數


{
//可變初始化,譬如機體升級,life加大,發射數變大,加快
body_life=life; //機體生命
body_fly=0;
bullet_number=no;//子彈數
bv=v; //子彈速度
bullet_list_Painter=new QVector<bullet*>;
}




QPainterPath body::body_paint1(int move_x,int move_y)
{
xx=move_x;
yy=move_y;

QPainterPath* body;
body=new QPainterPath;
body->moveTo(move_x-2,move_y);

body->lineTo(move_x,move_y-5);
body->lineTo(move_x+2,move_y);
body->lineTo(move_x+2,move_y+5);
body->lineTo(move_x+12,move_y+5);
body->lineTo(move_x+12,move_y+10);
body->lineTo(move_x-12,move_y+10);
body->lineTo(move_x-12,move_y+5);
body->lineTo(move_x-2,move_y+5);

body->lineTo(move_x-2,move_y);



if(body_fly==1)
{
QRectF r0(move_x-14, move_y+2, 2, 10 );
QRectF r1(move_x+12, move_y+2, 2, 10 );
body->addRect(r0);
body->addRect(r1);
}

if(body_fly==2)
{
QRectF r0(move_x-14, move_y+2, 2, 10 );
QRectF r1(move_x+12, move_y+2, 2, 10 );
QRectF r2(move_x-1, move_y+2, 2, 10 );
body->addRect(r0);
body->addRect(r1);
body->addRect(r2);

}

if(body_fly==3)
{
QRectF r0(move_x-24, move_y+2, 10, 10 );
QRectF r1(move_x+12, move_y+2, 10, 10 );
body->addEllipse(r0);;
body->addEllipse(r1);;

}


return *body;
}
void body::get_body_fly(int i)
{
body_fly=i;
}







void body::set_life(int i)
{
this->body_life=i;
}
int body::get_life()
{
return this->body_life;
}
///////////////////////////////
void body::set_x(int i)
{
this->x_int=i;
}
void body::set_y(int i)
{
this->y_int=i;
}

void body::add_bullet(int i)
{
if(i==0)//單一
{
if(bullet_list_Painter->size()<bullet_number)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int,y_int,999999);//速度10斜率無限大的子彈


bullet_list_Painter->append(p);
}

}
if(i==1)//2列
{
if(bullet_list_Painter->size()<bullet_number*2)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int-12,y_int,999999);//速度10斜率無限大的子彈
bullet_list_Painter->append(p);
bullet* p1=new bullet(bv,x_int+12,y_int,999999);//速度10斜率無限大的子彈
bullet_list_Painter->append(p1);
}

}
if(i==2)//3列
{
if(bullet_list_Painter->size()<bullet_number*3)//最多bullet_number顆子彈
{
bullet* p=new bullet(bv,x_int,y_int,-2); //速度10,斜率-2的子彈
bullet_list_Painter->append(p);
bullet* p1=new bullet(bv,x_int,y_int,+2); //速度10,斜率2的子彈
bullet_list_Painter->append(p1);
bullet* p2=new bullet(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet_list_Painter->append(p2);

}

}
if(i==3)//5列
{
if(bullet_list_Painter->size()<bullet_number*5)//最多bullet_number顆子彈
{
bullet* p1=new bullet(bv,x_int,y_int,-999); //速度10,斜率-2的子彈
bullet* p2=new bullet(bv,x_int,y_int,-2);
bullet* p3=new bullet(bv,x_int,y_int,+999); //速度10,斜率2的子彈
bullet* p4=new bullet(bv,x_int,y_int,+2);
bullet* p5=new bullet(bv,x_int,y_int,999999);//速度10,斜率無限大的子彈
bullet_list_Painter->append(p1);
bullet_list_Painter->append(p2);
bullet_list_Painter->append(p3);
bullet_list_Painter->append(p4);
bullet_list_Painter->append(p5);
}
}

}
void body::remove_bullet(int i)
{
if(!bullet_list_Painter->isEmpty())
{
this->bullet_list_Painter->remove(i);
}
}


QVector<bullet*> *body::get_bullet_list()
{
return bullet_list_Painter;
}

bullet.h
========================================================================
#ifndef BULLET_H
#define BULLET_H

#include <QWidget>

class bullet : public QWidget
{
Q_OBJECT
public:
bullet(int v,int x,int y,int m,QWidget *parent = 0);//建構子,目的初始化物件

private:
int b_m;
int xx;
int yy;
int bullet_v;

public:
void set_bullet_x(int x1);
void set_bullet_y(int y1);
int get_bullet_x();
int get_bullet_y();
QRectF path();
};

#endif // BULLET_H

bullet.cpp
====================================================================
/用這個class來放bullet的x,y座標
#include "bullet.h"

bullet::bullet(int v,int x,int y,int m,QWidget *parent)
: QWidget(parent)
{
bullet_v=v; //這設bulle速度,不能太快(不要超過目標y軸),擊中目標判斷會錯
xx=x; //初始化子彈x
yy=y; //初始化子彈y
b_m=m; //斜率

}
//設x
void bullet::set_bullet_x(int x1)//一次只能一個
{
this->xx=x1;
}
//設y
void bullet::set_bullet_y(int y1)
{
this->yy=y1;
}
//取得x
int bullet::get_bullet_x()
{
return this->xx;
}
//取得y
int bullet::get_bullet_y()
{
return this->yy;
}
QRectF bullet::path()//enemy路徑運算
{

if(b_m!=0&&b_m!=999&&b_m!=-999)
{
yy=-bullet_v+yy;
xx=-bullet_v/b_m+xx;
}
if(b_m==-999) //這來代表斜率-0
{
yy=yy;
xx=xx-bullet_v;
}
if(b_m==999) //這來代表斜率+0
{
yy=yy;
xx=xx+bullet_v;
}
QRectF r(xx, yy,5.0, 10.0);

return r;//橢圓

}

bullet1.h
=======================================================================
#ifndef BULLET1_H
#define BULLET1_H
#include "bullet.h"
#include <QObject>

class bullet1 :public bullet
{
Q_OBJECT
public:

bullet1(int v,int x,int y,int m );
};

#endif // BULLET1_H

bullet1.cpp
==============================================================
#include "bullet1.h"
#include "bullet.h"
bullet1::bullet1(int v,int x,int y,int m):bullet(v,x,y,m)
{


}


read more...

2009年8月15日 星期六

建構子

class用建構子比較好,目的在初始化物件

bullet* p1=new bullet(10,x_int,y_int,+2);產生斜率+2,速度10,開始位置(
x_int,y_int)
這class可產生不同方向的子彈,用斜率運算來達成

bullet.h
-------------------------------------------------------------------------------------
#ifndef BULLET_H
#define BULLET_H

#include <QWidget>

class bullet : public QWidget
{
Q_OBJECT
public:
bullet(int v,int x,int y,int m,QWidget *parent = 0);//建構子,目的初始化物件

private:
int b_m;
int xx;
int yy;
int bullet_v;
public:
void set_bullet_x(int x1);
void set_bullet_y(int y1);
int get_bullet_x();
int get_bullet_y();
void path();
};

#endif // BULLET_H

bullet.cpp
------------------------------------------------------------------
//用這個class來放bullet的x,y座標
#include "bullet.h"

bullet::bullet(int v,int x,int y,int m,QWidget *parent)
: QWidget(parent)
{
bullet_v=v; //這設bulle速度,不能太快(不要超過目標y軸),擊中目標判斷會錯
xx=x; //初始化子彈x
yy=y; //初始化子彈y
b_m=m; //斜率
}
//設x
void bullet::set_bullet_x(int x1)//一次只能一個
{
this->xx=x1;
}
//設y
void bullet::set_bullet_y(int y1)
{
this->yy=y1;
}
//取得x
int bullet::get_bullet_x()
{
return this->xx;
}
//取得y
int bullet::get_bullet_y()
{
return this->yy;
}
void bullet::path()//enemy路徑運算
{
if(b_m!=0)
{
yy=-bullet_v+yy;
xx=-bullet_v/b_m+xx;
}


}

read more...

2009年8月11日 星期二

在加上一個laser_bullet class(雷射子彈)

原理就是取2點(enemy_king和body這2點),算出斜率

m=(yn-y)/(xn-x);
l*l=
(yn-y)*(yn-y)+(xn-x)*(xn-x); //畢士定理

取得xn和yn,詳細運算在下面程式,最後回傳成QLine



laser_bullet.h

------------------------------------------------------------------------------
#ifndef LASER_BULLET_H
#define LASER_BULLET_H

#include <QWidget>

class laser_bullet : public QWidget
{
public:
laser_bullet(QWidget *parent = 0);
void set_laser_x(int x1);
void set_laser_y(int y1);
void set_laser_x1(int x1);
void set_laser_y1(int y1);
int get_laser_x();
int get_laser_y();
QLine* path();
void set_laser_m();
int get_m();
private:
int xx;
int yy;
int xx1;
int yy1;
int laser_v;
int m;
int laser_length;

};

#endif // LASER_BULLET_H



laser_bullet.cpp
----------------------------------------------------------------------------
#include "laser_bullet.h"
#include <iostream>
#include <math.h>
#include <QLine>
using namespace std;
laser_bullet::laser_bullet(QWidget *parent)
{
laser_v=8;//laser速度
laser_length=50;
xx=0;
yy=0;

m=1;

}
void laser_bullet::set_laser_x(int x1)//一次只能一個
{
this->xx=x1;
}
//設y
void laser_bullet::set_laser_y(int y1)
{
this->yy=y1;
}
//取得x
void laser_bullet::set_laser_x1(int x1)//一次只能一個
{this->xx1=x1;}
void laser_bullet::set_laser_y1(int y1)//一次只能一個
{this->yy1=y1;}

int laser_bullet::get_laser_x()
{
return this->xx;
}
//取得y
int laser_bullet::get_laser_y()
{
return this->yy;
}


void laser_bullet::set_laser_m() //斜率
{
if( xx1-xx!=0 ){ //避免斜率無限大error
this->m=(yy1-yy)/(xx1-xx);
}

//cout << "m=" << m ;
}
int laser_bullet::get_m(){
return this->m;
}
QLine* laser_bullet::path()//enemy路徑運算
{
int xn;
int yn;

if(m<0 && sqrt(1+m*m)!=0) //斜率負時,y增加,x減少
{
yy = yy+laser_v; //y位移量,一定要在yn之前
xx = -laser_v/sqrt(m*m)+xx; //x位移量,一定要在xn之前
yn=yy-m*laser_length/sqrt(1+m*m); //laser長度的y點
xn=xx-laser_length/sqrt(1+m*m); //laser長度的x點

} //斜率正時,y增加,x增加
else
{
yy = yy+laser_v; //y位移量,一定要在yn之前
xx = laser_v/sqrt(m*m)+xx; //x位移量,一定要在xn之前
yn=yy+m*laser_length/sqrt(1+m*m); //laser長度的y點
xn=xx+laser_length/sqrt(1+m*m); //laser長度的x點

}


QLine *p;
p=new QLine; //一定要先初始化否則ERROR
*p=QLine (xx,yy,xn,yn);
return p; //回傳QLine

}

read more...

2009年8月9日 星期日

Qt更換視窗的背景

使用setStyleSheet可輕易更換背景
/new/prefix1/ICON/back-134.gif
這是qrc的路徑,前幾篇文章有教怎樣建立qrc
void Frame::skin1()
{
setStyleSheet("background-image: url(:/new/prefix1/ICON/back-134.gif);");
}

read more...

2009年8月1日 星期六

程式碼

blog不能顯示尖括號,只好寫個sed小程式來取代,要放上程式就要先轉換

目前加上enemy的畫面,enmy是利用qrand()造成亂動畫面,產生一隻enemy就加入QVector,enemy.cpp放著座標

用這個public來設enemy座標
void enemy::set_enemy_x(int x1)//一次只能一個
{
this->enemy_xx=x1;
}
用這個public來取得enemy座標
int enemy::getx()
{
return this->enemy_xx;
}

這個是滿有用的技巧,是當初在玩java時看人用的,套用在C++也通用.

stage.cpp這個是放障礙物的class
bullet.cpp這個是放子彈座標的class
enemy.cpp這個是放敵人的class
whread.cpp這是執行續class
frame.cpp視窗體class


stage.cpp
------------------------------------------------------------------------------------
#include "stage1.h"
#include <QList>
#include <iostream>
#include <QRectF>

using namespace std;
/////////放障礙物mask_lis存著QRectF/////////////////////////////
stage1::stage1()
{
for(int i=0 ; i< 450 ; i += 10)
{
for (int j =0.0 ; j < 40 ; j += 10)
{
QRectF r(i, j, 10.0, 10.0);
mask_list<<r;
}
}
}

QList<QRectF> stage1::get_mask() //回傳mask_list
{
return this->mask_list;
}

void stage1::remove_mask(int i)
{
this->mask_list.removeAt(i);
}


bullet.cpp
-------------------------------------------------------------------------------
//用這個class來放bullet的x,y座標
#include "bullet.h"

bullet::bullet(QWidget *parent)
: QWidget(parent)
{
}
//設x
void bullet::set_bullet_x(int x1)//一次只能一個
{
this->xx=x1;
}
//設y
void bullet::set_bullet_y(int y1)
{
this->yy=y1;
}
//取得x
int bullet::get_bullet_x()
{
return this->xx;
}
//取得y
int bullet::get_bullet_y()
{
return this->yy;
}
enemy.cpp
----------------------------------------------------------------------------
#include "enemy.h"
#include "wthread.h"
#include <QThread>
#include <QPainterPath>
#include <QPainter>
#include <QtGui/QFrame>
#include <math.h>
using namespace std;
enemy::enemy(QWidget *parent) //使用parent回傳才能用this
: QWidget(parent)
{
angle=0;
enemy_yy=0;
}

void enemy::set_enemy_x(int x1)//一次只能一個
{
this->enemy_xx=x1;
}
void enemy::set_enemy_y(int y1)
{
this->enemy_yy=y1;
}
void enemy::set_enemy_v(int a1)//一次只能一個
{
this->enemy_v=a1;
}
void enemy::set_enemy_b(int r1)
{
this->enemy_b=r1;
}

int enemy::getx()
{
return this->enemy_xx;
}
int enemy::gety()
{
return this->enemy_yy;
}
int enemy::getv()
{
return this->enemy_v;
}
int enemy::getb()
{
return this->enemy_b;
}

void enemy::path(int x,int y,int b,int v)//enemy路徑運算
{

enemy_yy = enemy_yy + enemy_v ; //目前enemy的y座標+移動值(速度)
enemy_xx = enemy_xx + enemy_v*enemy_b; //目前enemy的x座標+移動值(速度*方向)
//超出frame範圍後座標
if (enemy_xx >450 ){enemy_xx= 0;}
if (enemy_xx <0 ){enemy_xx=450;}
if (enemy_xx >450 ){enemy_xx= 0;}
if (enemy_xx <0 ){enemy_xx=450;}
if (enemy_yy >360 ){enemy_yy= 0;}
if (enemy_yy <0 ){enemy_yy=360;}
if (enemy_yy >360 ){enemy_yy= 0;}
if (enemy_yy <0 ){enemy_yy=360;}
//rand()%10/6/



}
whread.cpp
------------------------------------------------------------------------
#include "wthread.h"
#include <QThread>
#include <QPainterPath>
#include <QPainter>
#include <QtGui/QFrame>
#include <enemy.h>
#include <math.h>
using namespace std;
Wthread::Wthread(QWidget *parent )
: QThread(parent)
{
//可變動變數
bullet_v =10 ;//bullet速度,不能超過enemy高度
enemy_max_v=2 ;//enemy最大速度
thread_s =50;//執行續秒數


//一定要先初始化否則ERROR
bullet_list_Painter=new QVector<bullet*>;
enemy_list_Painter=new QList<enemy*>;

enemy_y=0;
this->set_enemy(new enemy,100,100,1,-1);//產生enemy
this->set_enemy(new enemy,200,300,1,1);//產生enemy
this->set_enemy(new enemy,340,150,1,1);//產生enemy
this->set_enemy(new enemy,170,250,1,-1);//產生enemy

}
//執行續要處裡的事,每100毫秒,做一次信號
void Wthread::run() {

while(true) {
//處裡bullet
int y;
int x;
bullet_p=new QPainterPath;//100ms存一次QpainterPath,更新path
if(bullet_list_Painter->size()==0){ bullet_list_Painter->clear();}
for( int i= 0 ; i < bullet_list_Painter->size(); i++)//處裡QList的子彈
{
y=bullet_list_Painter->at(i)->get_bullet_y()-bullet_v;
x=bullet_list_Painter->at(i)->get_bullet_x()-0;
if( y < -20 ){this->remove_bullet(i);} //超出畫面就移走
else
{
QPainterPath* p=new QPainterPath;
p->addRoundRect(x,y,5.0,5.0,10);
bullet_list_Painter->at(i)->set_bullet_x(x); //新的取代掉,多判斷是避免例外
bullet_list_Painter->at(i)->set_bullet_y(y);
emit bullet_xy(x,y,i);
this->bullet_path(x,y); //子彈
}
}

//處裡enemy
enemy_p=new QPainterPath;

for( int i= 0 ; i < enemy_list_Painter->length(); i++)//處裡QList的enemy
{
int x=enemy_list_Painter->at(i)->getx();//y
int y=enemy_list_Painter->at(i)->gety();//y
int v=enemy_list_Painter->at(i)->getv();//y
int b=enemy_list_Painter->at(i)->getb();//y
enemy_p->addRect(x,y,20,20);
emit e_path(x,y,v,b); //目前信號參數x,y,v,b沒用到
enemy_list_Painter->at(i)->disconnect();
//讓enemy能亂動
if (qrand()%1000000>900000)
{
if (qrand()%1000000>500000)
{
enemy_list_Painter->at(i)->set_enemy_b(-1);//設往負x方向
}
else
{
enemy_list_Painter->at(i)->set_enemy_b(1);//設往正x方向
}
}
//讓enemy速度發生變化
if (qrand()%1000000>900000)
{
if (qrand()%1000000>500000)
{
enemy_list_Painter->at(i)->set_enemy_v(qrand()%enemy_max_v+1);//設往負x方向
}
else
{
enemy_list_Painter->at(i)->set_enemy_v(qrand()%enemy_max_v+1);//設往正x方向
}
}


}
emit setxy(1,1,1,enemy_p, bullet_p);

msleep(thread_s);
}
}
Wthread::~Wthread()
{

}
void Wthread::set_x(int i) //設定子彈x座標,按一下滑鼠
{
this->x_int=i;
}

void Wthread::add_bullet() //加一個子彈QPainterPath
{
if(bullet_list_Painter->size()<4)//最多4顆子彈
{
bullet* p=new bullet;
p->set_bullet_x(x_int);
p->set_bullet_y(300);
bullet_list_Painter->append(p);
}
}
void Wthread::bullet_path(int x,int y)//子彈形狀,從這可改子彈形狀
{
QRectF r(x, y,5.0, 10.0);//橢圓
bullet_p->addEllipse(r);
//bullet_p->addRoundRect(x,y,5.0,5.0,100);
}
void Wthread::remove_bullet(int i) //從QList移走子彈
{
if(!bullet_list_Painter->isEmpty())
{
bullet_list_Painter->remove(i);
}
}

//這是產生enemy的函式
void Wthread::set_enemy(enemy* e,int x,int y,int v,int b)
{
e->set_enemy_x(x); //enemy一定要在之前就宣告否則呼叫2次會error
e->set_enemy_y(y);
e->set_enemy_v(v);
e->set_enemy_b(b);
connect(this, SIGNAL(e_path(int,int,int,int)),e,SLOT(path(int,int,int,int)));
enemy_list_Painter->append(e);//加一隻enemy
}
frame.cpp
--------------------------------------------------------------------------------
#include "frame.h"
#include "ui_frame.h"
#include <QPainter>
#include <math.h>
#include <QMouseEvent>
#include <stage1.h>
#include <QVector>
#include "wthread.h"
using namespace std;

//static const QSize resultSize(5, 5);

Frame::Frame(QWidget *parent) :
QFrame(parent),
m_ui(new Ui::Frame)
{
//初始化設定
m_ui->setupUi(this);
step_value=0;
move_x=0;
move_y=300;
//setMouseTracking(true);//設一開始追蹤mouse
bullet_path=new QPainterPath; //一定要先初始化否則ERROR
enemy_path=new QPainterPath;
thread=new Wthread; //thread 物件化
connect(thread, SIGNAL(setxy(int , int ,int ,QPainterPath*,QPainterPath*)),this, SLOT(setShape(int ,int ,int ,QPainterPath*,QPainterPath*)));
connect(thread, SIGNAL(bullet_xy(int,int,int)),this,SLOT(hit_mask(int ,int, int)));
thread->start();
}

Frame::~Frame()
{
delete m_ui;
}

void Frame::changeEvent(QEvent *e)
{
QFrame::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
//SLOT,更新繪圖的值(子彈)
void Frame::setShape(int id,int y,int w,QPainterPath *enemy_path1,QPainterPath *bullet_path1)
{
bullet_path=bullet_path1;
enemy_path=enemy_path1;
update();
}
//繪圖宣告
void Frame::paintEvent(QPaintEvent *event)
{

QPainter painter(this);

///////////////////////////////////機台///////////////////////////
//設定筆刷
painter.setBrush(QColor(112, 186, 39));
//畫出機台
painter.drawPath(this->body_paint(new QPainterPath));
//////////////畫出
//設定筆刷
// painter.setBrush(QColor(112, 186, 39));
painter.drawPath(this->mask_pain(new QPainterPath));
///////////////////////////////////子彈////////////////////////////

painter.setBrush(QColor(212, 16, 39));
painter.drawPath(*bullet_path);
////////////////////////enemy//////////////////////////////////////

painter.setBrush(QColor(12, 16, 139));
painter.drawPath(*enemy_path);

}

//畫出機台
QPainterPath Frame::body_paint(QPainterPath* body)
{

body->addRect(move_x, move_y, 10, 20);
return *body;
}
//畫出障礙
QPainterPath Frame::mask_pain(QPainterPath* mask)
{
//stage1 stage;//放障礙物的class
for (int i = 0; i < stage.get_mask().length(); i++)
{
stage.get_mask().at(i);
mask->addRect(stage.get_mask().at(i));
}
return *mask;
}
//移動機台,繪圖

void Frame::move_body_paint(int x,int y)
{
move_x=x;
move_y=y;
update();
}
//回傳目前body的x,y值
int Frame::get_body_x()
{
return this->move_x;
}
int Frame::get_body_y()
{
return this->move_y;
}

void Frame::mouseMoveEvent(QMouseEvent *e)//滑鼠事件move
{
//move_x=e-> globalX();
//update();
int xx=e->x();
//int xx=e-> globalX(); //x
int yy=e->y(); //y
this->move_body_paint(xx,move_y);

}
void Frame::mousePressEvent(QMouseEvent *event)//滑鼠事件press
{
//按下按鈕,就產生一個子彈QPainterPath加入thread的bullet_list
thread->set_x(this->get_body_x());
thread->add_bullet();

}

void Frame::hit_mask(int bx,int by,int id) //打中障礙物移走障礙
{
for (int i = 0; i < stage.get_mask().length(); i++)
{
int x = stage.get_mask().at(i).x();
int y = stage.get_mask().at(i).y();
if ( x-4 < bx && bx <= x+6 && y-5 < by && by <= y+5 )
{
thread->wait(10);//否則有例外,可能有需要根據情形設定,例外是因為移走QList元素,thread正在處裡QList元素,有可能同步,先讓thread等一下
thread->remove_bullet(id);

stage.remove_mask(i);


}
}


}
--------------------------------------------------end-----------------------


read more...

2009年7月28日 星期二

目前的程式畫面


打中障礙物後,已經能清掉障礙了,子彈改成可複數.
read more...

2009年7月24日 星期五

準備用Qt來寫個小遊戲

目前寫好了機台Body和子彈,子彈移動是用執行續來做,機台是用抓滑鼠移動x座標

抓滑鼠移動(移動body)
void Frame::mouseMoveEvent(QMouseEvent *e)//滑鼠事件move
抓滑鼠按下(發射子彈)
void Frame::mousePressEvent(QMouseEvent *event)//滑鼠事件press

執行結果:

read more...

2009年7月18日 星期六

寫個Qt動畫

無聊,把之前寫的xmms2播放介面來弄個動畫

read more...

Slax - 你專屬的小系統


undefined

slax是一個微型Linux系統滿方便的,可安裝在大姆哥隨身碟,大姆哥就是一套好用的OS,便利性100%,好玩的是,可整套OS放在ram來執行,更可怕的是軟件都不用安裝,這對不太懂電腦的,真是太方便了,剛試一下wine gba模擬器竟然成功,不錯,這樣以後要出門只要帶大姆哥OS,XD..............
read more...

2009年7月10日 星期五

Qt讓畫圖達到旋轉


這是在QMatrix的說明中的一段,我們拿來套用,QMatrix可讓QPainter,上下左右或者旋轉

double pi = 3.14;

// double a = pi/180 * 185.0; //改變角度
double a = pi/180 * shapex;
double sina = sin(a);
double cosa = cos(a);

QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0);
QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0);
QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0);

QMatrix matrix;
matrix = scalingMatrix * rotationMatrix * translationMatrix;


painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
painter.drawRect(0, 0, 100, 100);

painter.setMatrix(matrix); //這會改變painter的matrix

painter.setFont(QFont("Helvetica", 24));
painter.setPen(QPen(Qt::black, 1));
painter.drawText(20, 10, "QMatrix");

來看結果:


read more...

2009年7月5日 星期日

Qt製作動畫




太高興了,搞了快2天才知道動畫原理,我新增了一個QThread的Class,然後要在此Class作一個信號槽(SIGNAL),連到
QWidget的SlOT,一直沒注意到應該跟QTimer類似,才想很久才想通,當然用QTimer也可達到動畫效果,只是我要用QThread來達成,彈性較大

Wthread.h
--------------------------------------------------------------------------

#ifndef WTHREAD_H
#define WTHREAD_H

#include
class Wthread:public QThread
{
Q_OBJECT
public:
Wthread(QObject *parent = 0);
~Wthread();
//信號
signals:
void setxy(int i1, int i2,int i3,int i4);
protected:
void run();
};

#endif // WTHREAD_H
----------------------------------------------------------------------------
Wthread.cpp
----------------------------------------------------------------

#include "wthread.h"
#include
Wthread::Wthread(QObject *parent)
: QThread(parent)
{




}
void Wthread::run() {

for(int i = 10; i 300; i += 10) {

emit setxy(i,i,i,i);
msleep(1000);
}

}
Wthread::~Wthread()
{

}
--------------------------------------------------------------------
Widget.cpp
----------------------------------------------------------------------------
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
#include
#include "wthread.h"
Widget::Widget(QWidget *parent)
: QWidget(parent), ui(new Ui::Widget)
{

ui->setupUi(this);
setShape(20,20,120,120);
step_value=0;
//qRegisterMetaType("i1");
connect(&thread, SIGNAL(setxy(int , int ,int ,int )),this, SLOT(setShape(int ,int ,int ,int )));


}

Widget::~Widget()
{
delete ui;

}
//重畫
void Widget::setShape(int x,int y,int w,int h)
{
shapex=x;
shapey=y;
shape_width=w;
shape_height=h;
update();
}

void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPainterPath path;
painter.setBrush(QColor(122, 163, 39));
path.addRect(shapex, shapey, shape_width, shape_height);
painter.drawPath(path);

}

void Widget::on_pushButton_pressed()
{
step_value= step_value+10;
setShape(20,20,120+step_value,120+step_value);
}

void Widget::on_pushButton_2_pressed()
{
step_value= step_value-10;
setShape(20,20,120+step_value,120+step_value);
}

void Widget::on_pushButton_3_pressed()
{
thread.start();
}







又作一個,做在QFrame裡面



重前面幾段,就可完成一個動畫,又能一邊控制物件移動,就可用Qt來作簡單的遊戲了,來看下面的例子


read more...

2009年7月4日 星期六

Qt畫圖



畫圖很簡單,問題是我要按一下按鈕就會重劃,找很久,才知道他的原理,首先要重新宣告paintEvent,要把處裡畫圖寫到這裡面,然後用update(),重劃
下面就是一個例子,setShape(int x,int y,int w,int h)是一個重劃函式,給了一個座標,和寬高,update來達成重劃矩形
paintEvent則重新宣告來畫圖(這裡是畫矩形)

void Widget::setShape(int x,int y,int w,int h)
{
shapex=x;
shapey=y;
shape_width=w;
shape_height=h;
update();
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPainterPath path;
painter.setBrush(QColor(122, 163, 39));
path.addRect(shapex, shapey, shape_width, shape_height);
painter.drawPath(path);
painter.restore();
}
------------------------slot函數-----------------------------------
void Widget::on_pushButton_pressed()
{
step_value= step_value+10;
setShape(20,20,120+step_value,120+step_value);
}

void Widget::on_pushButton_2_pressed()
{
step_value= step_value-10;
setShape(20,20,120+step_value,120+step_value);
}
------------------------------------------------------------------------
下面就是一個簡單例子,每當我按一次放大,長寬會加10,縮小則減10



read more...

2009年6月30日 星期二

用Qt Creator設計元件隨視窗改變大小

設計元件中layouts,就是我們要的,先把這些擺好,譬如要擺一組6個水平button,就加一個horizontal Layout,再把6個button丟進去,OK後在主frame按右鍵->Lay out -> Lay out in a grid這樣就大功告成.(如下圖)





























read more...

2009年6月28日 星期日

用Qt Creator加上背景圖案

1.必須先增加Resource file.(New->Qt->Resource file)
2.在
Resource file點Add->Add Prefix->Add->Add Files->選背景圖
3.File->Save All
4.mainwindow.ui右下視窗有->styleSheet->Add Resource->background-images(小箭頭)->Reload一下
->選圖檔,大功告成

把背景換成kitty的圖案

read more...

2009年6月25日 星期四

xmms2_v2.1播放介面發佈

修正一些BUG,可聽線上廣播

下載xmms2_v2.1播放介面(ubuntu9.04)

下載xmms2_v2.1播放介面(debian5.0)->因為debian qt還不是最新,拉桿會失效,只能用滑鼠滾輪



畫面




read more...

2009年6月19日 星期五

Qt寫的Xmms2播放介面第一版發布.

最新的,看起來有點像樣了,可聽線上廣播,新增時鐘,可調音量,多增一個列表區,面板和按鈕作一些調整,還沒上傳,等在調整好一些在上傳,debian5.0,和ubuntu9.04測試OK,其他版LINUX應該也OK.






























































據續上篇,把空格檔名無法加入xmms2 list,和一些小BUG解掉,增加暫停,添加功能,自動從使用者展開檔案tree,滑鼠小提示.
添加歌曲改成用下面,空格檔名就能加到xmms2 list,試很久才成功
---------------------------------------------------------------------------------
QProcess *myP = new QProcess(this); //宣告QProcess
QStringList arguments;
arguments << "add" <<>

---------------------------------------------------------------------------------
取得user name我在網路上找到,修改一下user_name.replace("\n",""),必須去掉換行,試很久才成功
--------------------------------------------------------------------------------

char text[255];
//FILE *name;
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
pclose(name);
user_name=text; //使用者name
user_name=user_name.replace("\n",""); //要把換行去掉
user_path = "/home/"+user_name.replace("\n",""); //使用者目錄
----------------------------------------------------------------------------------

開放原碼:

xmms2播放介面下載(new)


來幾張執行結果:















































這張是加了Icon比較漂亮(要先取得Qt執行檔位置,加icon才不會跑掉)

strPath = QApplication::applicationDirPath();//Qt取得執行檔位置

















把介面弄好看一些,加上顯示正在播放歌曲,即時時間,這部份要寫接收信號,搞很久才成功,進度顯示棒還沒完成,比之前那版好多了.
















進度棒也寫好了,主要是處理接收xmms2 status 的信號後,要如何得到一首歌共幾秒,和目前播到第幾秒,QRegExp 是Qt正規表示很像bash shell的正規表示,然後用replace換掉就可得到想要的.





void MainWindow::progressBar_step_set(QString str)
{
QString strx1=str;
QString strx2=str;
QRegExp rx1(" of .*$");
QRegExp rx2("^.* of ");
QString timer_set_str=strx1.replace(rx1,"");
QString timer_max_str=strx2.replace(rx2,"");
QRegExp mrx(":.*$");
QRegExp srx("^.*:");
QString m_timer_set_str=timer_set_str;
QString s_timer_set_str=timer_set_str;
QString msrt_set=m_timer_set_str.replace(mrx,""); //minute_set
QString ssrt_set=s_timer_set_str.replace(srx,""); //second_set
QString m_timer_max_str=timer_max_str;
QString s_timer_max_str=timer_max_str;
QString msrt_max=m_timer_max_str.replace(mrx,""); //minute_max
QString ssrt_max=s_timer_max_str.replace(srx,""); //second_max
int m_set = m_timer_set_str.toInt();
int s_set = s_timer_set_str.toInt();
int m_max = m_timer_max_str.toInt();
int s_max = s_timer_max_str.toInt();
int max_value = m_max*60 + s_max; //整首歌有幾秒
int set_value = m_set*60 + s_set; //正播到第幾秒
ui->progressBar->setValue(set_value);
ui->progressBar->setMaximum(max_value);


}

加上音量控制,還發現只要把串流加到xmms2,就可聽線上廣播(下一版的重點).


read more...

2009年6月17日 星期三

用Qt寫的,初步堪用xmms2的播放介面.

據續上篇,終於初步完成堪用的xmms2播放介面(要先安裝xmms2,才能用,我只是用Qt去執行xmms2的指令),還有BUG要解,若是列表沒音樂按播放會關掉視窗,若是當按或目錄有空格用xmms2 add加不上列表,還有介面太醜要美化.
mainwindow.cpp程式碼(部落格因為不能顯示<>很麻煩,將程式碼放到下面超連結)

http://sites.google.com/site/yplinshouye/Home/xmms2_gui

執行結果:



read more...