当前位置:网站首页>Developing NES games with C language (cc65) 08. Background collision
Developing NES games with C language (cc65) 08. Background collision
2022-07-28 12:23:00 【firseve】
There are some differences between the background and the spirit , We can't read PPU Bytes in .
Let's make a map first , The size of each block is 16x16, Filling the whole background requires 240 An array of bytes .X from 0-15,Y from 0-14. We put the array into RAM in , To prevent it from being modified .
The operation will be demonstrated later , First look at the array structure .
const unsigned char c2 [] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
1 Represents a block in the game

To detect collisions , You just need to X and Y Remove the low byte of , Then put it together , Explain the method later
X>>4 take X Move right 4 position ,Y & 0xF0 take Y It's low 4 A into 0, Compare it with the values in the array one by one ,1 Collision ,0 No,
I will test the elves 4 A direction , And modify it during collision U D L R 4 Attribute values ( Is whether there is a collision in the up, down, left and right directions of the spirit )
I'll do it first X Move , Check for collisions , If it collides, jump , Move again Y, Check for collisions , If it collides, skip , That is, do not move . Look at the function bg_collision()
This code should be streamlined , Because every frame will have XY The change of , I will modify the code to achieve speed change . Every frame 1 Pixels are too slow , It will cause a bad game experience .
I'm used to using Tiled Map Make game levels , It is very simple to use , And you can export csv file , This makes it easy to make C Array of . But this tool cannot import NES Format .chr file , So I have to redo all types of map blocks . It's very simple , We just need 2 A species , Blank blocks and ordinary map blocks .
Let me use it first. NES Screen Tool Make blocks , Then draw 2x2 The block . It seems that this approach is a little stupid , But when there are dozens of tiles in your game , It will be very happy 了
Because there is no way to export nametable Pictures of the , So I screenshot and then in GIMP edit , Save as metatile.png
With tileset How to import Tiled Map In the map editor . because nes Screen tools double pixels , So the size of each picture is 32x32
( Translation notes : It means that the screenshot image of the editor is relatively large , No 8x8 Of , It is 32x32 Of , That's why the above situation )
Edit the level map , export csv file .

SV The file is converted to C Arrays are a small thing , But I still made a python3 Script to achieve . I will C Array import my code , Then use an array of pointers to point to the previous array .
#include “CSV/c1.c”
#include “CSV/c2.c”
#include “CSV/c3.c”
#include “CSV/c4.c”
const unsigned char * const All_Collision_Maps[] = {c1,c2,c3,c4};
Now? , I wrote some code , Will a 2×2 Block by block print to the screen , A big loop is used , It's used inside. vram_put() Method .
Use vram_put() You need to close the screen before , write in PPU So from left to right , Then the next line starts from left to right . In this way, you can easily write the whole screen .
Start Press the key to switch to the next map display
When you press right ,X The location will be +1, And then test 4 Collision points , If there is a collision, it will end moving to the right (X Meeting -1)
( Translation notes : The author uses The points after coordinate transformation are used to detect collision , In case of collision, it will retreat to change , If there is no collision , The spirit will move according to the changed point -- That is to redraw the new location of the spirit )
test : Hit the wall in the direction , Collision can take effect . Press Start Button , Background changes , The collision is still valid , because RAM Loaded a new map collision detection .
Be careful : I moved the whole background down by scrolling the screen 1 Pixel .Y scroll=ff(-1), Because sprites always move down one pixel , Therefore, the background must be moved down as a whole 1 Pixel .
https://github.com/nesdoug/09_BG_Collide/blob/master/collide.c
https://github.com/nesdoug/09_BG_Collide
The code of the map loading part is not well written , Because you can only paint one kind of map block , And the property sheet will not be modified , Later I will introduce a better loading method (11、Metatiles), Before that, let me talk about rolling .
边栏推荐
- Loongarch Godson architecture document collection
- SQL injection LESS18 (header injection + error injection)
- Training mode and practice of digital applied talents in Colleges and Universities under the integration of industry and education
- Lua makes a deep copy of table
- Force buckle 7_ 1672. Total assets of the richest customers
- 用C语言开发NES游戏(CC65)11、Metatiles
- Style conversion model style_ Transformer project instance pytorch implementation
- Rest style
- What if the instruction set umi2 is missing? PTK installation cannot be carried out
- Some knowledge concepts
猜你喜欢

Ruiji takeout - day01

Laravel form data validation

Untiy controls the playback speed of animation

用C语言开发NES游戏(CC65)10、游戏循环

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

LyScript 获取上一条与下一条指令

云原生机器学习落地难?灵雀云助力企业快速应用 MLOps

“蔚来杯“2022牛客暑期多校训练营2

Test platform (V) knowledge points supplement

Interpretation of the paper: attention mechanism in medical images
随机推荐
用C语言开发NES游戏(CC65)10、游戏循环
Full analysis of seven classical regression analysis methods
PHP获取本周所有日期或者最近七天所有日期
Unity 一键替换场景中的物体
Huawei releases harmonyos 3 and all scene new products, and the smart experience goes further
Some knowledge concepts
REST风格
Google Earth engine (GEE) -- problems in the use of coordinate projection and reduceresolution functions in image downscaling
【vulnhub】Raven2
QT writing IOT management platform 42 data query export print
Alexnet - paper analysis and reproduction
The principle and use of the wrap file of tolua
What if the instruction set umi2 is missing? PTK installation cannot be carried out
用C语言开发NES游戏(CC65)11、Metatiles
Style conversion model style_ Transformer project instance pytorch implementation
php保留两位小数的几种方法介绍
2022.07.10 summer training personal qualifying (V)
云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
[leetcode] 8. binary search · binary search
CentOS 7 install MySQL 5.7 & uninstall MySQL 5.7