Wednesday, June 16, 2021

【GAMEMAKER】Day and Night State

 Information about object: obj_balls

Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

//this is just an object that emits ball particles that bounce around
//not applicable to our example. It just simulates having objects in the room

balls = 3
for (m = 0; m < balls; m += 1) {
 ball_x[m] = random(room_width-32)
 ball_y[m] = random(room_height-32)
 ball_dir[m] = random(360)
 ball_sp[m] = floor(random(5))+1
}

alarm[0] = random(room_speed*5)+1
Alarm Event for alarm 0:
execute code:

ball_x[balls] = random(room_width-32)
ball_y[balls] = random(room_height-32)
ball_dir[balls] = random(360)
ball_sp[balls] = floor(random(5))+1
balls += 1
 
alarm[0] = random(room_speed*5)+1
Step Event:
execute code:

for (m = 0; m < balls; m += 1) {
 ball_x1[m] = ball_x[m] + lengthdir_x(ball_sp[m],ball_dir[m])
 ball_y1[m] = ball_y[m] + lengthdir_y(ball_sp[m],ball_dir[m])
 if ball_x1[m] <= 0 || ball_x1[m]+32 >= room_width { ball_dir[m] = 180-ball_dir[m] }
 if ball_y1[m] <= 0 || ball_y1[m]+32 >= room_height { ball_dir[m] = -ball_dir[m] }
 while ball_dir[m] < 0 { ball_dir[m] += 360 }
 while ball_dir[m] >= 360 { ball_dir[m] -= 360 }
 ball_x[m] += lengthdir_x(ball_sp[m],ball_dir[m])
 ball_y[m] += lengthdir_y(ball_sp[m],ball_dir[m])
}
Draw Event:
execute code:

draw_set_halign(fa_center)
draw_set_valign(fa_middle)
for (m = 0; m < balls; m += 1) {
 draw_sprite(spr_ball,0,ball_x[m],ball_y[m])
 draw_text(ball_x[m]+16,ball_y[m]+16,string(ball_dir[m])+"#"+string(ball_sp[m]))
}

Information about object: obj_oday
Sprite:
Solid: false
Visible: true
Depth: -10000
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

//notice, the object's depth is less than the depth of other objects
//this is so the rectangle is drawn in front of everything

curhr = current_hour //initialize the hour variable (whatever works for you)
//valid hours are >= 0 and < 24. Only integers (no decimals or fractions)

alpha = .6 //default value (for hours from 0 to 6)
if curhr = 7 { alpha = .4 }
if curhr = 8 { alpha = .2 }
if curhr >= 9 { alpha = 0 }
if curhr = 18 { alpha = .2 }
if curhr = 19 { alpha = .4 }
if curhr >= 20 { alpha = .6 } //each of the caches


alarm[0] = room_speed * 2 //set the alarm to go off in 2 seconds
Alarm Event for alarm 0:
execute code:

//every 2 seconds, the hour will increment

curhr += 1 //increment the hour
if curhr >= 24 { curhr = 0 } //cap at 24 (start over)

alarm[0] = room_speed * 2 //loop the alarm (every 2 seconds it goes off)
Step Event:
execute code:

alpha = .6 //default value (for hours from 0 to 6)
if curhr = 7 { alpha = .4 }
if curhr = 8 { alpha = .2 }
if curhr >= 9 { alpha = 0 }
if curhr = 18 { alpha = .2 }
if curhr = 19 { alpha = .4 }
if curhr >= 20 { alpha = .6 } //cache each hour

room_caption = "1) fps: "+string(fps) +"/50 | Hr: "+string(curhr)+"/24"
//just displays the fps and hour in the room caption
//you probably want to delete this for your game
Draw Event:
execute code:

draw_set_alpha(alpha) //set the drawing alpha
draw_rectangle(0,0,room_width,room_height,0) //draw a rectangle over the room
//this is the part that is seen
draw_set_alpha(1) //return to normal drawing alpha
Key Press Event for <Enter> Key:
execute code:

//here we allow the user to change the hour using Enter key

var cr;
cr = get_integer("hour",curhr)
if cr >= 0 && cr < 24 { curhr = cr }
Key Press Event for <Space> Key:
execute code:

room_goto_next() //to see the other example, using backgrounds

Information about object: obj_bday
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

//notice the background (number 1, not 0).
//good idea to stretch the background, rather than tile

curhr = current_hour //initialize the hour variable (whatever works for you)
//valid hours are >= 0 and < 24. Only integers (no decimals or fractions)

alpha = .6 //default value (for hours from 0 to 6)
if curhr = 7 { alpha = .4 }
if curhr = 8 { alpha = .2 }
if curhr >= 9 { alpha = 0 }
if curhr = 18 { alpha = .2 }
if curhr = 19 { alpha = .4 }
if curhr >= 20 { alpha = .6 } //each of the caches

background_alpha[1] = alpha //set the background's alpha to the alpha
//this is the part that is seen

alarm[0] = room_speed * 2 //set the alarm to go off in 2 seconds
Alarm Event for alarm 0:
execute code:

//every 2 seconds, the hour will increment

curhr += 1 //increment the hour
if curhr >= 24 { curhr = 0 } //cap at 24 (start over)

alarm[0] = room_speed * 2 //loop the alarm (every 2 seconds it goes off)
Step Event:
execute code:

alpha = .6 //default value (for hours from 0 to 6)
if curhr = 7 { alpha = .4 }
if curhr = 8 { alpha = .2 }
if curhr >= 9 { alpha = 0 }
if curhr = 18 { alpha = .2 }
if curhr = 19 { alpha = .4 }
if curhr >= 20 { alpha = .6 } //cache each hour

background_alpha[1] = alpha //set the background's alpha to the alpha
//this is the part that is seen

room_caption = "1) fps: "+string(fps) +"/50 | Hr: "+string(curhr)+"/24"
//just displays the fps and hour in the room caption
//you probably want to delete this for your game
Key Press Event for <Enter> Key:
execute code:

//here we allow the user to change the hour using Enter key

var cr;
cr = get_integer("hour",curhr)
if cr >= 0 && cr < 24 { curhr = cr }
Key Press Event for <Space> Key:
execute code:

room_goto_previous() //to see the other example, using a rectangle

No comments:

Post a Comment

End of Summer Sale ☀️😎

20% OFF Inside!🤯 ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏...